-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest_cliUtilSplitCommas.py
122 lines (102 loc) · 4.5 KB
/
test_cliUtilSplitCommas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# This file is part of daf_butler.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (http://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# This software is dual licensed under the GNU General Public License and also
# under a 3-clause BSD license. Recipients may choose which of these licenses
# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
# respectively. If you choose the GPL option then the following text applies
# (but note that there is still no warranty even if you opt for BSD instead):
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Unit tests for the daf_butler shared CLI options."""
import unittest
from unittest.mock import MagicMock
import click
from lsst.daf.butler.cli.utils import LogCliRunner, clickResultMsg, split_commas
mock = MagicMock()
@click.command()
@click.option("--list-of-values", "-l", multiple=True, callback=split_commas)
def cli(list_of_values):
"""Run mocked command line."""
mock(list_of_values)
class SplitCommasTestCase(unittest.TestCase):
"""Test the split commas utility."""
def setUp(self):
self.runner = LogCliRunner()
def test_separate(self):
"""Test the split_commas callback by itself."""
ctx = "unused"
param = "unused"
self.assertEqual(split_commas(ctx, param, ("one,two", "three,four")), ("one", "two", "three", "four"))
self.assertEqual(split_commas(ctx, param, None), ())
def test_single(self):
"""Test the split_commas callback in an option with one value."""
result = self.runner.invoke(cli, ["-l", "one"])
self.assertEqual(result.exit_code, 0, msg=clickResultMsg(result))
mock.assert_called_with(("one",))
def test_multiple(self):
"""Test the split_commas callback in an option with two single
values.
"""
result = self.runner.invoke(cli, ["-l", "one", "-l", "two"])
self.assertEqual(result.exit_code, 0, msg=clickResultMsg(result))
mock.assert_called_with(("one", "two"))
def test_singlePair(self):
"""Test the split_commas callback in an option with one pair of
values.
"""
result = self.runner.invoke(cli, ["-l", "one,two"])
self.assertEqual(result.exit_code, 0, msg=clickResultMsg(result))
mock.assert_called_with(("one", "two"))
def test_multiplePair(self):
"""Test the split_commas callback in an option with two pairs of
values.
"""
result = self.runner.invoke(cli, ["-l", "one,two", "-l", "three,four"])
self.assertEqual(result.exit_code, 0, msg=clickResultMsg(result))
mock.assert_called_with(("one", "two", "three", "four"))
def test_none(self):
"""Test that passing None does not fail and returns None, producing an
empty tuple in the command function call.
"""
result = self.runner.invoke(cli, [])
self.assertEqual(result.exit_code, 0, msg=clickResultMsg(result))
mock.assert_called_with(())
def test_parens(self):
"""Test that split commas understands ``[a, b]``."""
for test, expected in (
("single", ("single",)),
("a,b", ("a", "b")),
("[a,b]", ("[a,b]",)),
("a[1,2],b", ("a[1,2]", "b")),
):
result = split_commas(None, None, test)
self.assertEqual(result, expected)
# These should warn because it's likely a typo.
for test, expected in (
("a[1,b[2,3],c", ("a[1,b[2,3]", "c")),
("a[1,b,c", ("a[1,b,c",)),
("a[1,b", ("a[1,b",)),
("a1,b]", ("a1", "b]")),
):
with self.assertWarns(UserWarning, msg=f"Testing {test!r}"):
result = split_commas(None, None, test)
self.assertEqual(result, expected)
if __name__ == "__main__":
unittest.main()