Coverage for tests/test_cliUtilSplitCommas.py: 48%
40 statements
« prev ^ index » next coverage.py v6.4.1, created at 2022-06-17 02:08 -0700
« prev ^ index » next coverage.py v6.4.1, created at 2022-06-17 02:08 -0700
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 mock(list_of_values)
40class SplitCommasTestCase(unittest.TestCase):
41 def setUp(self):
42 self.runner = LogCliRunner()
44 def test_separate(self):
45 """test the split_commas callback by itself"""
46 ctx = "unused"
47 param = "unused"
48 self.assertEqual(
49 split_commas(ctx, param, ("one,two", "three,four")), ("one", "two", "three", "four") # noqa E231
50 )
51 self.assertEqual(split_commas(ctx, param, None), None)
53 def test_single(self):
54 """test the split_commas callback in an option with one value"""
55 result = self.runner.invoke(cli, ["-l", "one"])
56 self.assertEqual(result.exit_code, 0, msg=clickResultMsg(result))
57 mock.assert_called_with(("one",))
59 def test_multiple(self):
60 """test the split_commas callback in an option with two single
61 values"""
62 result = self.runner.invoke(cli, ["-l", "one", "-l", "two"])
63 self.assertEqual(result.exit_code, 0, msg=clickResultMsg(result))
64 mock.assert_called_with(("one", "two"))
66 def test_singlePair(self):
67 """test the split_commas callback in an option with one pair of
68 values"""
69 result = self.runner.invoke(cli, ["-l", "one,two"])
70 self.assertEqual(result.exit_code, 0, msg=clickResultMsg(result))
71 mock.assert_called_with(("one", "two"))
73 def test_multiplePair(self):
74 """test the split_commas callback in an option with two pairs of
75 values"""
76 result = self.runner.invoke(cli, ["-l", "one,two", "-l", "three,four"])
77 self.assertEqual(result.exit_code, 0, msg=clickResultMsg(result))
78 mock.assert_called_with(("one", "two", "three", "four"))
80 def test_none(self):
81 """test that passing None does not fail and returns None, producing an
82 empty tuple in the command function call."""
83 result = self.runner.invoke(cli, [])
84 self.assertEqual(result.exit_code, 0, msg=clickResultMsg(result))
85 mock.assert_called_with(())
88if __name__ == "__main__": 88 ↛ 89line 88 didn't jump to line 89, because the condition on line 88 was never true
89 unittest.main()