Coverage for tests/test_cliUtilSplitCommas.py: 35%
46 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-28 10:10 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-28 10:10 +0000
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22"""Unit tests for the daf_butler shared CLI options.
23"""
25import unittest
26from unittest.mock import MagicMock
28import click
29from lsst.daf.butler.cli.utils import LogCliRunner, clickResultMsg, split_commas
31mock = MagicMock()
34@click.command()
35@click.option("--list-of-values", "-l", multiple=True, callback=split_commas)
36def cli(list_of_values):
37 """Run mocked command line."""
38 mock(list_of_values)
41class SplitCommasTestCase(unittest.TestCase):
42 """Test the split commas utility."""
44 def setUp(self):
45 self.runner = LogCliRunner()
47 def test_separate(self):
48 """Test the split_commas callback by itself."""
49 ctx = "unused"
50 param = "unused"
51 self.assertEqual(split_commas(ctx, param, ("one,two", "three,four")), ("one", "two", "three", "four"))
52 self.assertEqual(split_commas(ctx, param, None), tuple())
54 def test_single(self):
55 """Test the split_commas callback in an option with one value."""
56 result = self.runner.invoke(cli, ["-l", "one"])
57 self.assertEqual(result.exit_code, 0, msg=clickResultMsg(result))
58 mock.assert_called_with(("one",))
60 def test_multiple(self):
61 """Test the split_commas callback in an option with two single
62 values.
63 """
64 result = self.runner.invoke(cli, ["-l", "one", "-l", "two"])
65 self.assertEqual(result.exit_code, 0, msg=clickResultMsg(result))
66 mock.assert_called_with(("one", "two"))
68 def test_singlePair(self):
69 """Test the split_commas callback in an option with one pair of
70 values.
71 """
72 result = self.runner.invoke(cli, ["-l", "one,two"])
73 self.assertEqual(result.exit_code, 0, msg=clickResultMsg(result))
74 mock.assert_called_with(("one", "two"))
76 def test_multiplePair(self):
77 """Test the split_commas callback in an option with two pairs of
78 values.
79 """
80 result = self.runner.invoke(cli, ["-l", "one,two", "-l", "three,four"])
81 self.assertEqual(result.exit_code, 0, msg=clickResultMsg(result))
82 mock.assert_called_with(("one", "two", "three", "four"))
84 def test_none(self):
85 """Test that passing None does not fail and returns None, producing an
86 empty tuple in the command function call.
87 """
88 result = self.runner.invoke(cli, [])
89 self.assertEqual(result.exit_code, 0, msg=clickResultMsg(result))
90 mock.assert_called_with(())
92 def test_parens(self):
93 """Test that split commas understands ``[a, b]``."""
94 for test, expected in (
95 ("single", ("single",)),
96 ("a,b", ("a", "b")),
97 ("[a,b]", ("[a,b]",)),
98 ("a[1,2],b", ("a[1,2]", "b")),
99 ):
100 result = split_commas(None, None, test)
101 self.assertEqual(result, expected)
103 # These should warn because it's likely a typo.
104 for test, expected in (
105 ("a[1,b[2,3],c", ("a[1,b[2,3]", "c")),
106 ("a[1,b,c", ("a[1,b,c",)),
107 ("a[1,b", ("a[1,b",)),
108 ("a1,b]", ("a1", "b]")),
109 ):
110 with self.assertWarns(UserWarning, msg=f"Testing {test!r}"):
111 result = split_commas(None, None, test)
112 self.assertEqual(result, expected)
115if __name__ == "__main__":
116 unittest.main()