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

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 butler instrument CLI option. 

23""" 

24 

25import click 

26import click.testing 

27import unittest 

28 

29from lsst.daf.butler.cli.utils import ParameterType 

30from lsst.daf.butler.tests import CliOptionTestBase 

31from lsst.obs.base.cli.opt import instrument_parameter 

32 

33 

34class InstrumentTestCase(CliOptionTestBase): 

35 

36 optionClass = instrument_parameter 

37 

38 def verify(self, result, verifyArgs): 

39 self.assertEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}") 

40 self.assertIn(verifyArgs, result.stdout) 

41 

42 def verifyMissing(self, result, verifyArgs): 

43 self.assertNotEqual(result.exit_code, 0, f"output: {result.output} exception: {result.exception}") 

44 self.assertIn(verifyArgs, result.stdout) 

45 

46 def test_argument(self): 

47 """test argument""" 

48 @click.command() 

49 @instrument_parameter(parameterType=ParameterType.ARGUMENT) 

50 def cli(instrument): 

51 if instrument is None: 

52 instrument = "None" 

53 print(instrument) 

54 

55 self.run_test(cli, ["foo*"], self.verify, "foo*") 

56 self.run_test(cli, [], self.verify, "None") 

57 

58 def test_argument_required(self): 

59 """test with argument required""" 

60 @click.command() 

61 @instrument_parameter(parameterType=ParameterType.ARGUMENT, required=True) 

62 def cli(instrument): 

63 print(instrument) 

64 

65 self.run_test(cli, ["foo*"], self.verify, "foo*") 

66 self.run_test(cli, [], self.verifyMissing, 'Error: Missing argument "INSTRUMENT"') 

67 

68 def test_option(self): 

69 """test option""" 

70 @click.command() 

71 @instrument_parameter() 

72 def cli(instrument): 

73 if instrument is None: 

74 instrument = "None" 

75 print(instrument) 

76 

77 self.run_test(cli, ["--instrument", "foo*"], self.verify, "foo*") 

78 self.run_test(cli, [], self.verify, "None") 

79 

80 def test_option_required(self): 

81 """test with argument required""" 

82 @click.command() 

83 @instrument_parameter(parameterType=ParameterType.ARGUMENT, required=True) 

84 def cli(instrument): 

85 print(instrument) 

86 

87 self.run_test(cli, ["foo"], self.verify, "foo") 

88 self.run_test(cli, [], self.verifyMissing, 'Error: Missing argument "INSTRUMENT"') 

89 

90 def test_argument_multiple(self): 

91 """test with multiple argument values""" 

92 @click.command() 

93 @instrument_parameter(parameterType=ParameterType.ARGUMENT, multiple=True) 

94 def cli(instrument): 

95 print(instrument) 

96 

97 self.run_test(cli, ["foo*", "bar", "b?z"], self.verify, "('foo*', 'bar', 'b?z')") 

98 

99 def test_option_multiple(self): 

100 """test with multiple option values""" 

101 @click.command() 

102 @instrument_parameter(multiple=True) 

103 def cli(instrument): 

104 print(instrument) 

105 

106 self.run_test(cli, ["--instrument", "foo*", "--instrument", "bar", "--instrument", "b?z"], 

107 self.verify, "('foo*', 'bar', 'b?z')") 

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