Coverage for tests/test_cliUtilSplitCommas.py: 36%

48 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-15 01:59 -0800

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/>. 

21 

22"""Unit tests for the daf_butler shared CLI options. 

23""" 

24 

25import unittest 

26from unittest.mock import MagicMock 

27 

28import click 

29from lsst.daf.butler.cli.utils import LogCliRunner, clickResultMsg, split_commas 

30 

31mock = MagicMock() 

32 

33 

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) 

38 

39 

40class SplitCommasTestCase(unittest.TestCase): 

41 def setUp(self): 

42 self.runner = LogCliRunner() 

43 

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) 

52 

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",)) 

58 

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")) 

65 

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")) 

72 

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")) 

79 

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(()) 

86 

87 def test_parens(self): 

88 """Test that split commas understands [a,b]""" 

89 for test, expected in ( 

90 ("single", ("single",)), 

91 ("a,b", ("a", "b")), 

92 ("[a,b]", ("[a,b]",)), 

93 ("a[1,2],b", ("a[1,2]", "b")), 

94 ): 

95 result = split_commas(None, None, test) 

96 self.assertEqual(result, expected) 

97 

98 # These should warn because it's likely a typo. 

99 for test, expected in ( 

100 ("a[1,b[2,3],c", ("a[1,b[2,3]", "c")), 

101 ("a[1,b,c", ("a[1,b,c",)), 

102 ("a[1,b", ("a[1,b",)), 

103 ("a1,b]", ("a1", "b]")), 

104 ): 

105 with self.assertWarns(UserWarning, msg=f"Testing {test!r}"): 

106 result = split_commas(None, None, test) 

107 self.assertEqual(result, expected) 

108 

109 

110if __name__ == "__main__": 110 ↛ 111line 110 didn't jump to line 111, because the condition on line 110 was never true

111 unittest.main()