Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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 click 

26import click.testing 

27import functools 

28import unittest 

29 

30from lsst.daf.butler.cli.utils import split_kv 

31 

32 

33class Suite(unittest.TestCase): 

34 

35 def test_single(self): 

36 self.assertEqual(split_kv("context", "param", "first=1"), {"first": "1"}) 

37 

38 def test_multiple(self): 

39 self.assertEqual(split_kv("context", "param", "first=1,second=2"), {"first": "1", "second": "2"}) 

40 

41 def test_wrongSeparator(self): 

42 with self.assertRaises(click.ClickException): 

43 split_kv("context", "param", "first-1") 

44 

45 def test_missingSeparator(self): 

46 with self.assertRaises(click.ClickException): 

47 split_kv("context", "param", "first 1") 

48 

49 def test_duplicateKeys(self): 

50 with self.assertRaises(click.ClickException): 

51 split_kv("context", "param", "first=1,first=2") 

52 

53 def test_dashSeparator(self): 

54 self.assertEqual(split_kv("context", "param", "first-1,second-2", "-"), {"first": "1", "second": "2"}) 

55 

56 def test_cli(self): 

57 @click.command() 

58 @click.option("--value", callback=split_kv, multiple=True) 

59 def cli(value): 

60 click.echo(value) 

61 runner = click.testing.CliRunner() 

62 

63 result = runner.invoke(cli, ["--value", "first=1"]) 

64 self.assertEqual(result.exit_code, 0) 

65 self.assertEqual(result.stdout, "{'first': '1'}\n") 

66 

67 result = runner.invoke(cli, ["--value", "first=1,second=2"]) 

68 self.assertEqual(result.exit_code, 0) 

69 self.assertEqual(eval(result.stdout), {'first': '1', 'second': '2'}) 

70 

71 result = runner.invoke(cli, ["--value", "first=1", "--value", "second=2"]) 

72 self.assertEqual(result.exit_code, 0) 

73 self.assertEqual(eval(result.stdout), {'first': '1', 'second': '2'}) 

74 

75 # double separator "==" should fail: 

76 result = runner.invoke(cli, ["--value", "first==1"]) 

77 self.assertEqual(result.exit_code, 1) 

78 self.assertEqual(result.output, 

79 "Error: Missing or invalid key-value separator in value 'first==1'\n") 

80 

81 def test_separatorDash(self): 

82 def split_kv_dash(context, param, values): 

83 return split_kv(context, param, values, separator="-") 

84 

85 @click.command() 

86 @click.option("--value", callback=split_kv_dash, multiple=True) 

87 def cli(value): 

88 click.echo(value) 

89 

90 runner = click.testing.CliRunner() 

91 result = runner.invoke(cli, ["--value", "first-1"]) 

92 self.assertEqual(result.exit_code, 0) 

93 self.assertEqual(result.stdout, "{'first': '1'}\n") 

94 

95 def test_separatorFunctoolsDash(self): 

96 @click.command() 

97 @click.option("--value", callback=functools.partial(split_kv, separator="-"), multiple=True) 

98 def cli(value): 

99 click.echo(value) 

100 runner = click.testing.CliRunner() 

101 result = runner.invoke(cli, ["--value", "first-1", "--value", "second-2"]) 

102 self.assertEqual(result.exit_code, 0) 

103 self.assertEqual(eval(result.stdout), {'first': '1', 'second': '2'}) 

104 

105 def test_separatorSpace(self): 

106 @click.command() 

107 @click.option("--value", callback=functools.partial(split_kv, separator=" "), multiple=True) 

108 def cli(value): 

109 click.echo(value) 

110 runner = click.testing.CliRunner() 

111 result = runner.invoke(cli, ["--value", "first 1"]) 

112 self.assertEqual(str(result.exception), 

113 "' ' is not a supported separator for key-value pairs.") 

114 

115 def test_separatorComma(self): 

116 @click.command() 

117 @click.option("--value", callback=functools.partial(split_kv, separator=","), multiple=True) 

118 def cli(value): 

119 click.echo(value) 

120 runner = click.testing.CliRunner() 

121 result = runner.invoke(cli, ["--value", "first,1"]) 

122 self.assertEqual(str(result.exception), 

123 "',' is not a supported separator for key-value pairs.") 

124 

125 

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

127 unittest.main()