Coverage for tests/test_cliUtilSplitCommas.py: 41%

46 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-26 02:47 -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 software is dual licensed under the GNU General Public License and also 

10# under a 3-clause BSD license. Recipients may choose which of these licenses 

11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt, 

12# respectively. If you choose the GPL option then the following text applies 

13# (but note that there is still no warranty even if you opt for BSD instead): 

14# 

15# This program is free software: you can redistribute it and/or modify 

16# it under the terms of the GNU General Public License as published by 

17# the Free Software Foundation, either version 3 of the License, or 

18# (at your option) any later version. 

19# 

20# This program is distributed in the hope that it will be useful, 

21# but WITHOUT ANY WARRANTY; without even the implied warranty of 

22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

23# GNU General Public License for more details. 

24# 

25# You should have received a copy of the GNU General Public License 

26# along with this program. If not, see <http://www.gnu.org/licenses/>. 

27 

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

29""" 

30 

31import unittest 

32from unittest.mock import MagicMock 

33 

34import click 

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

36 

37mock = MagicMock() 

38 

39 

40@click.command() 

41@click.option("--list-of-values", "-l", multiple=True, callback=split_commas) 

42def cli(list_of_values): 

43 """Run mocked command line.""" 

44 mock(list_of_values) 

45 

46 

47class SplitCommasTestCase(unittest.TestCase): 

48 """Test the split commas utility.""" 

49 

50 def setUp(self): 

51 self.runner = LogCliRunner() 

52 

53 def test_separate(self): 

54 """Test the split_commas callback by itself.""" 

55 ctx = "unused" 

56 param = "unused" 

57 self.assertEqual(split_commas(ctx, param, ("one,two", "three,four")), ("one", "two", "three", "four")) 

58 self.assertEqual(split_commas(ctx, param, None), ()) 

59 

60 def test_single(self): 

61 """Test the split_commas callback in an option with one value.""" 

62 result = self.runner.invoke(cli, ["-l", "one"]) 

63 self.assertEqual(result.exit_code, 0, msg=clickResultMsg(result)) 

64 mock.assert_called_with(("one",)) 

65 

66 def test_multiple(self): 

67 """Test the split_commas callback in an option with two single 

68 values. 

69 """ 

70 result = self.runner.invoke(cli, ["-l", "one", "-l", "two"]) 

71 self.assertEqual(result.exit_code, 0, msg=clickResultMsg(result)) 

72 mock.assert_called_with(("one", "two")) 

73 

74 def test_singlePair(self): 

75 """Test the split_commas callback in an option with one pair of 

76 values. 

77 """ 

78 result = self.runner.invoke(cli, ["-l", "one,two"]) 

79 self.assertEqual(result.exit_code, 0, msg=clickResultMsg(result)) 

80 mock.assert_called_with(("one", "two")) 

81 

82 def test_multiplePair(self): 

83 """Test the split_commas callback in an option with two pairs of 

84 values. 

85 """ 

86 result = self.runner.invoke(cli, ["-l", "one,two", "-l", "three,four"]) 

87 self.assertEqual(result.exit_code, 0, msg=clickResultMsg(result)) 

88 mock.assert_called_with(("one", "two", "three", "four")) 

89 

90 def test_none(self): 

91 """Test that passing None does not fail and returns None, producing an 

92 empty tuple in the command function call. 

93 """ 

94 result = self.runner.invoke(cli, []) 

95 self.assertEqual(result.exit_code, 0, msg=clickResultMsg(result)) 

96 mock.assert_called_with(()) 

97 

98 def test_parens(self): 

99 """Test that split commas understands ``[a, b]``.""" 

100 for test, expected in ( 

101 ("single", ("single",)), 

102 ("a,b", ("a", "b")), 

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

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

105 ): 

106 result = split_commas(None, None, test) 

107 self.assertEqual(result, expected) 

108 

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

110 for test, expected in ( 

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

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

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

114 ("a1,b]", ("a1", "b]")), 

115 ): 

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

117 result = split_commas(None, None, test) 

118 self.assertEqual(result, expected) 

119 

120 

121if __name__ == "__main__": 

122 unittest.main()