Coverage for tests/test_cliUtilSplitKv.py: 25%

Shortcuts 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

139 statements  

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 functools import partial 

27from unittest.mock import MagicMock 

28 

29import click 

30from lsst.daf.butler.cli.utils import LogCliRunner, clickResultMsg, split_kv 

31 

32 

33class SplitKvTestCase(unittest.TestCase): 

34 """Tests that call split_kv directly.""" 

35 

36 def test_single_dict(self): 

37 """Test that a single kv pair converts to a dict.""" 

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

39 

40 def test_single_tuple(self): 

41 """Test that a single kv pair converts to a tuple when 

42 return_type=tuple.""" 

43 self.assertEqual(split_kv("context", "param", "first=1", return_type=tuple), (("first", "1"),)) 

44 

45 def test_multiple_dict(self): 

46 """Test that multiple comma separated kv pairs convert to a dict.""" 

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

48 

49 def test_multiple_tuple(self): 

50 """Test that multiple comma separated kv pairs convert to a tuple when 

51 return_type=tuple.""" 

52 self.assertEqual( 

53 split_kv("context", "param", "first=1,second=2", return_type=tuple), 

54 (("first", "1"), ("second", "2")), 

55 ) 

56 

57 def test_unseparated(self): 

58 """Test that a value without a key converts to a kv pair with an empty 

59 string key.""" 

60 self.assertEqual( 

61 split_kv("context", "param", "first,second=2", unseparated_okay=True), 

62 {"": "first", "second": "2"}, 

63 ) 

64 

65 def test_notMultiple(self): 

66 """Test that multiple values are rejected if multiple=False.""" 

67 with self.assertRaisesRegex( 

68 click.ClickException, 

69 "Could not parse key-value pair " 

70 "'first=1,second=2' using separator '=', with multiple values not " 

71 "allowed.", 

72 ): 

73 split_kv("context", "param", "first=1,second=2", multiple=False) 

74 

75 def test_wrongSeparator(self): 

76 """Test that an input with the wrong separator raises.""" 

77 with self.assertRaises(click.ClickException): 

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

79 

80 def test_missingSeparator(self): 

81 """Test that an input with no separator raises when 

82 unseparated_okay=False (this is the default value).""" 

83 with self.assertRaises(click.ClickException): 

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

85 

86 def test_unseparatedOkay(self): 

87 """Test that that the default key is used for values without a 

88 separator when unseparated_okay=True.""" 

89 self.assertEqual(split_kv("context", "param", "foo", unseparated_okay=True), {"": "foo"}) 

90 

91 def test_unseparatedOkay_list(self): 

92 """Test that that the default key is used for values without a 

93 separator when unseparated_okay=True and the return_type is tuple.""" 

94 self.assertEqual( 

95 split_kv("context", "param", "foo,bar", unseparated_okay=True, return_type=tuple), 

96 (("", "foo"), ("", "bar")), 

97 ) 

98 

99 def test_unseparatedOkay_defaultKey(self): 

100 """Test that that the default key can be set and is used for values 

101 without a separator when unseparated_okay=True.""" 

102 self.assertEqual( 

103 split_kv("context", "param", "foo", unseparated_okay=True, default_key=...), {...: "foo"} 

104 ) 

105 

106 def test_dashSeparator(self): 

107 """Test that specifying a separator is accepted and converts arguments 

108 to a dict. 

109 """ 

110 self.assertEqual( 

111 split_kv("context", "param", "first-1,second-2", separator="-"), {"first": "1", "second": "2"} 

112 ) 

113 

114 def test_reverseKv(self): 

115 self.assertEqual( 

116 split_kv( 

117 "context", 

118 "param", 

119 "first=1,second", 

120 unseparated_okay=True, 

121 default_key="key", 

122 reverse_kv=True, 

123 ), 

124 {"1": "first", "second": "key"}, 

125 ) 

126 

127 

128class SplitKvCmdTestCase(unittest.TestCase): 

129 """Tests using split_kv with a command.""" 

130 

131 def setUp(self): 

132 self.runner = LogCliRunner() 

133 

134 def test_cli(self): 

135 mock = MagicMock() 

136 

137 @click.command() 

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

139 def cli(value): 

140 mock(value) 

141 

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

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

144 mock.assert_called_with({"first": "1"}) 

145 

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

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

148 mock.assert_called_with({"first": "1", "second": "2"}) 

149 

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

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

152 mock.assert_called_with({"first": "1", "second": "2"}) 

153 

154 # double separator "==" should fail: 

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

156 self.assertEqual(result.exit_code, 1) 

157 self.assertEqual( 

158 result.output, 

159 "Error: Could not parse key-value pair 'first==1' using separator '=', with " 

160 "multiple values allowed.\n", 

161 ) 

162 

163 def test_choice(self): 

164 choices = ["FOO", "BAR", "BAZ"] 

165 mock = MagicMock() 

166 

167 @click.command() 

168 @click.option( 

169 "--metasyntactic-var", 

170 callback=partial( 

171 split_kv, 

172 unseparated_okay=True, 

173 choice=click.Choice(choices, case_sensitive=False), 

174 normalize=True, 

175 ), 

176 ) 

177 def cli(metasyntactic_var): 

178 mock(metasyntactic_var) 

179 

180 # check a valid choice without a kv separator 

181 result = self.runner.invoke(cli, ["--metasyntactic-var", "FOO"]) 

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

183 mock.assert_called_with({"": "FOO"}) 

184 

185 # check a valid choice with a kv separator 

186 result = self.runner.invoke(cli, ["--metasyntactic-var", "lsst.daf.butler=BAR"]) 

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

188 mock.assert_called_with({"lsst.daf.butler": "BAR"}) 

189 

190 # check that invalid choices with and without kv separators fail & 

191 # return a non-zero exit code. 

192 for val in ("BOZ", "lsst.daf.butler=BOZ"): 

193 result = self.runner.invoke(cli, ["--metasyntactic-var", val]) 

194 self.assertNotEqual(result.exit_code, 0, msg=clickResultMsg(result)) 

195 

196 # check value normalization (lower case "foo" should become "FOO") 

197 result = self.runner.invoke(cli, ["--metasyntactic-var", "lsst.daf.butler=foo"]) 

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

199 mock.assert_called_with({"lsst.daf.butler": "FOO"}) 

200 

201 def test_separatorDash(self): 

202 def split_kv_dash(context, param, values): 

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

204 

205 mock = MagicMock() 

206 

207 @click.command() 

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

209 def cli(value): 

210 mock(value) 

211 

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

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

214 mock.assert_called_with({"first": "1"}) 

215 

216 def test_separatorFunctoolsDash(self): 

217 mock = MagicMock() 

218 

219 @click.command() 

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

221 def cli(value): 

222 mock(value) 

223 

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

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

226 mock.assert_called_with({"first": "1", "second": "2"}) 

227 

228 def test_separatorSpace(self): 

229 @click.command() 

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

231 def cli(value): 

232 pass 

233 

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

235 self.assertEqual(str(result.exception), "' ' is not a supported separator for key-value pairs.") 

236 

237 def test_separatorComma(self): 

238 @click.command() 

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

240 def cli(value): 

241 pass 

242 

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

244 self.assertEqual(str(result.exception), "',' is not a supported separator for key-value pairs.") 

245 

246 def test_normalizeWithoutChoice(self): 

247 """Test that normalize=True without Choice fails gracefully. 

248 

249 Normalize uses values in the provided Choice to create the normalized 

250 value. Without a provided Choice, it can't normalize. Verify that this 

251 does not cause a crash or other bad behavior, it just doesn't normalize 

252 anything. 

253 """ 

254 mock = MagicMock() 

255 

256 @click.command() 

257 @click.option("--value", callback=partial(split_kv, normalize=True)) 

258 def cli(value): 

259 mock(value) 

260 

261 result = self.runner.invoke(cli, ["--value", "foo=bar"]) 

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

263 mock.assert_called_with(dict(foo="bar")) 

264 

265 def test_addToDefaultValue(self): 

266 """Verify that if add_to_default is True that passed-in values are 

267 added to the default value set in the option. 

268 """ 

269 mock = MagicMock() 

270 

271 @click.command() 

272 @click.option( 

273 "--value", 

274 callback=partial(split_kv, add_to_default=True, unseparated_okay=True), 

275 default=["INFO"], 

276 multiple=True, 

277 ) 

278 def cli(value): 

279 mock(value) 

280 

281 result = self.runner.invoke(cli, ["--value", "lsst.daf.butler=DEBUG"]) 

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

283 mock.assert_called_with({"": "INFO", "lsst.daf.butler": "DEBUG"}) 

284 

285 def test_replaceDefaultValue(self): 

286 """Verify that if add_to_default is False (this is the default value), 

287 that passed-in values replace any default value, even if keys are 

288 different. 

289 """ 

290 mock = MagicMock() 

291 

292 @click.command() 

293 @click.option( 

294 "--value", callback=partial(split_kv, unseparated_okay=True), default=["INFO"], multiple=True 

295 ) 

296 def cli(value): 

297 mock(value) 

298 

299 result = self.runner.invoke(cli, ["--value", "lsst.daf.butler=DEBUG"]) 

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

301 mock.assert_called_with({"lsst.daf.butler": "DEBUG"}) 

302 

303 

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

305 unittest.main()