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 dataset-type CLI option. 

23""" 

24 

25import click 

26import click.testing 

27import unittest 

28import yaml 

29 

30from lsst.daf.butler.cli.opt import config_file_option, config_option, dataset_type_option 

31 

32 

33class DatasetTypeTestCase(unittest.TestCase): 

34 

35 @staticmethod 

36 @click.command() 

37 @dataset_type_option(help="the dataset type") 

38 def cli(dataset_type): 

39 click.echo(dataset_type, nl=False) 

40 

41 def test_single(self): 

42 """test a single argument""" 

43 runner = click.testing.CliRunner() 

44 result = runner.invoke(DatasetTypeTestCase.cli, ["--dataset-type", "one"]) 

45 self.assertEqual(result.exit_code, 0) 

46 self.assertEqual(result.stdout, "['one']") 

47 

48 def test_multiple(self): 

49 """test multiple arguments, using the long and short option names""" 

50 runner = click.testing.CliRunner() 

51 result = runner.invoke(DatasetTypeTestCase.cli, ["--dataset-type", "one", "-d", "two"]) 

52 self.assertEqual(result.exit_code, 0) 

53 self.assertEqual(result.stdout, "['one', 'two']") 

54 

55 def test_singlePair(self): 

56 """test a single comma-separated value pair""" 

57 runner = click.testing.CliRunner() 

58 result = runner.invoke(DatasetTypeTestCase.cli, ["--dataset-type", "one,two"]) 

59 self.assertEqual(result.exit_code, 0) 

60 self.assertEqual(result.stdout, "['one', 'two']") 

61 

62 def test_multiplePair(self): 

63 """test multiple comma-separated value pairs""" 

64 runner = click.testing.CliRunner() 

65 result = runner.invoke(DatasetTypeTestCase.cli, ["--dataset-type", "one,two", "-d", "three,four"]) 

66 self.assertEqual(result.exit_code, 0) 

67 self.assertEqual(result.stdout, "['one', 'two', 'three', 'four']") 

68 

69 def test_help(self): 

70 """test capture of the help text""" 

71 runner = click.testing.CliRunner() 

72 result = runner.invoke(DatasetTypeTestCase.cli, ["--help"]) 

73 self.assertEqual(result.exit_code, 0) 

74 self.assertIn("the dataset type", result.stdout) 

75 

76 

77class ConfigTestCase(unittest.TestCase): 

78 

79 @staticmethod 

80 @click.command() 

81 @config_option(help="foo bar baz") 

82 def cli(config): 

83 click.echo(yaml.dump(config), nl=False) 

84 

85 def test_basic(self): 

86 """test arguments""" 

87 runner = click.testing.CliRunner() 

88 result = runner.invoke(ConfigTestCase.cli, ["--config", "a=1", "-c", "b=2,c=3"]) 

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

90 self.assertEqual(yaml.safe_load(result.stdout), dict(a="1", b="2", c="3")) 

91 

92 def test_missing(self): 

93 @click.command() 

94 @config_option(required=True) 

95 def cli(config): 

96 pass 

97 runner = click.testing.CliRunner() 

98 result = runner.invoke(cli, []) 

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

100 self.assertIn('Missing option "-c" / "--config"', result.output) 

101 

102 def test_help(self): 

103 """test capture of the help text""" 

104 runner = click.testing.CliRunner() 

105 result = runner.invoke(ConfigTestCase.cli, ["--help"]) 

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

107 self.assertIn("foo bar baz", result.stdout) 

108 

109 

110class ConfigFileTestCase(unittest.TestCase): 

111 

112 @staticmethod 

113 @click.command() 

114 @config_file_option(help="foo bar baz") 

115 def cli(config_file): 

116 click.echo(config_file, nl=False) 

117 

118 def test_basic(self): 

119 """test arguments""" 

120 runner = click.testing.CliRunner() 

121 result = runner.invoke(ConfigFileTestCase.cli, ["--config-file", "path/to/file"]) 

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

123 self.assertEqual("path/to/file", result.stdout) 

124 

125 def test_missing(self): 

126 @click.command() 

127 @config_file_option(required=True) 

128 def cli(config): 

129 pass 

130 runner = click.testing.CliRunner() 

131 result = runner.invoke(cli, []) 

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

133 self.assertIn('Missing option "-C" / "--config-file"', result.output) 

134 

135 def test_help(self): 

136 """test capture of the help text""" 

137 runner = click.testing.CliRunner() 

138 result = runner.invoke(ConfigFileTestCase.cli, ["--help"]) 

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

140 self.assertIn("foo bar baz", result.stdout) 

141 

142 

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

144 unittest.main()