Coverage for tests/test_cliOption.py : 40%

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/>.
22"""Unit tests for the daf_butler dataset-type CLI option.
23"""
25import click
26import click.testing
27import unittest
28import yaml
30from lsst.daf.butler.tests import CliOptionTestBase
31from lsst.daf.butler.registry import CollectionType
32from lsst.daf.butler.cli.opt import (collection_type_option, config_file_option, config_option,
33 dataset_type_option, directory_argument, glob_parameter, verbose_option)
34from lsst.daf.butler.cli.utils import clickResultMsg, ParameterType
37class CollectionTypeTestCase(CliOptionTestBase):
39 optionClass = collection_type_option
41 def setUp(self):
42 super().setUp()
43 CollectionTypeTestCase.collectionType = None
45 @staticmethod
46 @click.command()
47 @collection_type_option()
48 def cli(collection_type):
49 CollectionTypeTestCase.collectionType = collection_type
51 def verify(self, result, verifyArgs):
52 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
53 self.assertEqual(CollectionTypeTestCase.collectionType, verifyArgs)
55 def test_run(self):
56 self.run_test(CollectionTypeTestCase.cli, ["--collection-type", "RUN"],
57 self.verify, CollectionType.RUN)
59 def test_chained(self):
60 self.run_test(CollectionTypeTestCase.cli, ["--collection-type", "CHAINED"],
61 self.verify, CollectionType.CHAINED)
63 def test_tagged(self):
64 self.run_test(CollectionTypeTestCase.cli, ["--collection-type", "TAGGED"],
65 self.verify, CollectionType.TAGGED)
67 def test_default(self):
68 self.run_test(CollectionTypeTestCase.cli, [],
69 self.verify, None)
71 def test_caseInsensitive(self):
72 self.run_test(CollectionTypeTestCase.cli, ["--collection-type", "TaGGeD"],
73 self.verify, CollectionType.TAGGED)
75 def test_help(self):
76 self.help_test()
77 self.custom_help_test()
80class ConfigTestCase(CliOptionTestBase):
82 optionClass = config_option
84 @staticmethod
85 @click.command()
86 @config_option()
87 def cli(config):
88 click.echo(yaml.dump(config), nl=False)
90 def test_basic(self):
91 """test arguments"""
92 result = self.runner.invoke(ConfigTestCase.cli, ["--config", "a=1", "-c", "b=2,c=3"])
93 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}")
94 self.assertEqual(yaml.safe_load(result.stdout), dict(a="1", b="2", c="3"))
96 def test_missing(self):
97 @click.command()
98 @config_option(required=True)
99 def cli(config):
100 pass
102 result = self.runner.invoke(cli, [])
103 self.assertNotEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}")
104 self.assertIn('Missing option "-c" / "--config"', result.output)
106 def test_help(self):
107 self.help_test()
108 self.custom_help_test()
111class ConfigFileTestCase(CliOptionTestBase):
113 optionClass = config_file_option
115 @staticmethod
116 @click.command()
117 @config_file_option()
118 def cli(config_file):
119 click.echo(config_file, nl=False)
121 def test_basic(self):
122 """test arguments"""
123 result = self.runner.invoke(ConfigFileTestCase.cli, ["--config-file", "path/to/file"])
124 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}")
125 self.assertEqual("path/to/file", result.stdout)
127 def test_missing(self):
128 @click.command()
129 @config_file_option(required=True)
130 def cli(config):
131 pass
132 result = self.runner.invoke(cli, [])
133 self.assertNotEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}")
134 self.assertIn('Missing option "-C" / "--config-file"', result.output)
136 def test_help(self):
137 self.help_test()
138 self.custom_help_test()
141class DatasetTypeTestCase(CliOptionTestBase):
143 optionClass = dataset_type_option
145 @staticmethod
146 @click.command()
147 @dataset_type_option(help="the dataset type")
148 def cli(dataset_type):
149 click.echo(dataset_type, nl=False)
151 def verify(self, result, verifyArgs):
152 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
153 self.assertEqual(result.stdout, verifyArgs)
155 def test_single(self):
156 """test a single argument"""
157 self.run_test(DatasetTypeTestCase.cli, ["--dataset-type", "one"], self.verify, "['one']")
159 def test_multiple(self):
160 """test multiple arguments, using the long and short option names"""
161 self.run_test(DatasetTypeTestCase.cli, ["--dataset-type", "one", "-d", "two"],
162 self.verify, "['one', 'two']")
164 def test_singlePair(self):
165 """test a single comma-separated value pair"""
166 self.run_test(DatasetTypeTestCase.cli, ["--dataset-type", "one,two"],
167 self.verify, "['one', 'two']")
169 def test_multiplePair(self):
170 """test multiple comma-separated value pairs"""
171 self.run_test(DatasetTypeTestCase.cli, ["--dataset-type", "one,two", "-d", "three,four"],
172 self.verify, "['one', 'two', 'three', 'four']")
174 def test_help(self):
175 # dataset_type_option does not have default help
176 self.custom_help_test()
179class DirectoryArgumentTestCase(CliOptionTestBase):
181 optionClass = directory_argument
183 def test_required(self):
184 """test arguments"""
185 @click.command()
186 @directory_argument(required=True)
187 def cli(directory):
188 click.echo(directory, nl=False)
189 result = self.runner.invoke(cli, ["this_dir"])
190 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
191 self.assertEqual("this_dir", result.stdout)
192 result = self.runner.invoke(cli, [])
193 self.assertNotEqual(result.exit_code, 0, clickResultMsg(result))
194 self.assertIn('Missing argument "DIRECTORY"', result.stdout)
196 def test_notRequired(self):
197 """test arguments"""
198 @click.command()
199 @directory_argument()
200 def cli(directory):
201 click.echo(directory, nl=False)
202 result = self.runner.invoke(cli, ["this_dir"])
203 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
204 self.assertEqual("this_dir", result.stdout)
205 result = self.runner.invoke(cli, [])
206 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
207 self.assertEqual("", result.stdout)
209 def test_help(self):
210 # directory_argument does not have default help.
211 self.custom_help_test()
214class GlobTestCase(CliOptionTestBase):
216 optionClass = glob_parameter
218 def verify(self, result, verifyArgs):
219 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}")
220 self.assertIn(verifyArgs, result.stdout)
222 def verifyMissing(self, result, verifyArgs):
223 self.assertNotEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}")
224 self.assertIn(verifyArgs, result.stdout)
226 def test_glob_argument(self):
227 """test argument"""
228 @click.command()
229 @glob_parameter(parameterType=ParameterType.ARGUMENT)
230 def cli(glob):
231 if glob is None:
232 glob = "None"
233 print(glob)
235 self.run_test(cli, ["foo*"], self.verify, "foo*")
236 self.run_test(cli, [], self.verify, "None")
238 def test_glob_argument_required(self):
239 """test with argument required"""
240 @click.command()
241 @glob_parameter(parameterType=ParameterType.ARGUMENT, required=True)
242 def cli(glob):
243 print(glob)
245 self.run_test(cli, ["foo*"], self.verify, "foo*")
246 self.run_test(cli, [], self.verifyMissing, 'Error: Missing argument "GLOB"')
248 def test_glob_option(self):
249 """test option"""
250 @click.command()
251 @glob_parameter()
252 def cli(glob):
253 if glob is None:
254 glob = "None"
255 print(glob)
257 self.run_test(cli, ["--glob", "foo*"], self.verify, "foo*")
258 self.run_test(cli, [], self.verify, "None")
260 def test_glob_option_required(self):
261 """test with argument required"""
262 @click.command()
263 @glob_parameter(parameterType=ParameterType.ARGUMENT, required=True)
264 def cli(glob):
265 print(glob)
267 self.run_test(cli, ["foo*"], self.verify, "foo*")
268 self.run_test(cli, [], self.verifyMissing, 'Error: Missing argument "GLOB"')
270 def test_glob_argument_multiple(self):
271 """test with multiple argument values"""
272 @click.command()
273 @glob_parameter(parameterType=ParameterType.ARGUMENT, multiple=True)
274 def cli(glob):
275 print(glob)
277 self.run_test(cli, ["foo*", "bar", "b?z"], self.verify, "('foo*', 'bar', 'b?z')")
279 def test_glob_option_multiple(self):
280 """test with multiple option values"""
281 @click.command()
282 @glob_parameter(multiple=True)
283 def cli(glob):
284 print(glob)
286 self.run_test(cli, ["--glob", "foo*", "--glob", "bar", "--glob", "b?z"], self.verify,
287 "('foo*', 'bar', 'b?z')")
289 def test_help(self):
290 self.help_test()
291 self.custom_help_test()
294class VerboseTestCase(CliOptionTestBase):
296 optionClass = verbose_option
298 @staticmethod
299 @click.command()
300 @verbose_option()
301 def cli(verbose):
302 print(verbose)
304 def verify(self, result, verifyArgs):
305 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}")
306 self.assertIn(verifyArgs, result.stdout)
308 def test_verbose(self):
309 """test arguments"""
310 self.run_test(self.cli, ["--verbose"], self.verify, "True")
312 def test_notVerbose(self):
313 """test arguments"""
314 self.run_test(self.cli, [], self.verify, "False")
316 def test_help(self):
317 self.help_test()
318 self.custom_help_test()
321if __name__ == "__main__": 321 ↛ 322line 321 didn't jump to line 322, because the condition on line 321 was never true
322 unittest.main()