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# 

2# This file is part of ap_verify. 

3# 

4# Developed for the LSST Data Management System. 

5# This product includes software developed by the LSST Project 

6# (http://www.lsst.org). 

7# See the COPYRIGHT file at the top-level directory of this distribution 

8# for details of code ownership. 

9# 

10# This program is free software: you can redistribute it and/or modify 

11# it under the terms of the GNU General Public License as published by 

12# the Free Software Foundation, either version 3 of the License, or 

13# (at your option) any later version. 

14# 

15# This program is distributed in the hope that it will be useful, 

16# but WITHOUT ANY WARRANTY; without even the implied warranty of 

17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

18# GNU General Public License for more details. 

19# 

20# You should have received a copy of the GNU General Public License 

21# along with this program. If not, see <http://www.gnu.org/licenses/>. 

22# 

23 

24import shlex 

25import unittest 

26 

27import lsst.utils.tests 

28import lsst.ap.verify.ap_verify as ap_verify 

29import lsst.ap.verify.testUtils 

30 

31 

32class CommandLineTestSuite(lsst.ap.verify.testUtils.DataTestCase): 

33 # DataTestCase's test dataset is needed for successful parsing of the --dataset argument 

34 

35 def _parseString(self, commandLine, parser=None): 

36 """Tokenize and parse a command line string. 

37 

38 Parameters 

39 ---------- 

40 commandLine : `str` 

41 a string containing Unix-style command line arguments, but not the 

42 name of the program 

43 parser : `argparse.ArgumentParser`, optional 

44 the parser to use. Defaults to ``ap_verify``'s primary parser. 

45 

46 Returns 

47 ------- 

48 parsed : `argparse.Namespace` 

49 The parsed command line. 

50 """ 

51 if not parser: 

52 parser = ap_verify._ApVerifyParser() 

53 

54 return parser.parse_args(shlex.split(commandLine)) 

55 

56 def testMissingMain(self): 

57 """Verify that a command line consisting missing required arguments is rejected. 

58 """ 

59 args = '--id "visit=54123" --output tests/output/foo' 

60 with self.assertRaises(SystemExit): 

61 self._parseString(args) 

62 

63 def testMissingIngest(self): 

64 """Verify that a command line consisting missing required arguments is rejected. 

65 """ 

66 args = '--dataset %s' % CommandLineTestSuite.testDataset 

67 with self.assertRaises(SystemExit): 

68 self._parseString(args, ap_verify._IngestOnlyParser()) 

69 

70 def testMinimumMain(self): 

71 """Verify that a command line consisting only of required arguments parses correctly. 

72 """ 

73 args = '--dataset %s --output tests/output/foo' % CommandLineTestSuite.testDataset 

74 parsed = self._parseString(args) 

75 self.assertEqual(parsed.dataset.datasetRoot, 

76 lsst.utils.getPackageDir(CommandLineTestSuite.testDataset)) 

77 self.assertEqual(parsed.output, "tests/output/foo") 

78 self.assertEqual(parsed.dataIds, []) 

79 

80 def testMinimumIngest(self): 

81 """Verify that a command line consisting only of required arguments parses correctly. 

82 """ 

83 args = '--dataset %s --output tests/output/foo' % CommandLineTestSuite.testDataset 

84 parsed = self._parseString(args, ap_verify._IngestOnlyParser()) 

85 self.assertEqual(parsed.dataset.datasetRoot, 

86 lsst.utils.getPackageDir(CommandLineTestSuite.testDataset)) 

87 self.assertEqual(parsed.output, "tests/output/foo") 

88 

89 def testDataId(self): 

90 """Verify that a command line consisting only of required arguments and 

91 --id parses correctly. 

92 """ 

93 args = '--dataset %s --output tests/output/foo --id "visit=54123" --id "filter=x"' \ 

94 % CommandLineTestSuite.testDataset 

95 parsed = self._parseString(args) 

96 self.assertEqual(parsed.dataIds, ["visit=54123", "filter=x"]) 

97 

98 def testMixedDataId(self): 

99 """Verify that a command line with both --id and --data-query parses correctly. 

100 """ 

101 args = '--dataset %s --output tests/output/foo --id "visit=54123" -d "filter=x"' \ 

102 % CommandLineTestSuite.testDataset 

103 parsed = self._parseString(args) 

104 self.assertEqual(parsed.dataIds, ["visit=54123", "filter=x"]) 

105 

106 def testEmptyDataId(self): 

107 """Test that an --id argument may be not followed by a data ID. 

108 """ 

109 minArgs = f'--dataset {self.testDataset} --output tests/output/foo' 

110 

111 # Nothing after --id 

112 parsed = self._parseString(minArgs + ' --id') 

113 self.assertEqual(parsed.dataIds, []) 

114 

115 # --dataset immediately after --id 

116 parsed = self._parseString('--id ' + minArgs) 

117 self.assertEqual(parsed.dataIds, []) 

118 

119 def testBadKeyMain(self): 

120 """Verify that a command line with unsupported arguments is rejected. 

121 """ 

122 args = '--dataset %s --output tests/output/foo --id "visit=54123" --clobber' \ 

123 % CommandLineTestSuite.testDataset 

124 with self.assertRaises(SystemExit): 

125 self._parseString(args) 

126 

127 def testBadKeyIngest(self): 

128 """Verify that a command line with unsupported arguments is rejected. 

129 """ 

130 args = '--dataset %s --output tests/output/foo --id "visit=54123"' \ 

131 % CommandLineTestSuite.testDataset 

132 with self.assertRaises(SystemExit): 

133 self._parseString(args, ap_verify._IngestOnlyParser()) 

134 

135 def testGen23Selector(self): 

136 """Verify that all combinations of --gen2 and --gen3 behave 

137 as expected. 

138 """ 

139 minArgs = f'--dataset {self.testDataset} --output tests/output/foo ' 

140 

141 # Default is Gen 3 

142 parsedDefault = self._parseString(minArgs) 

143 self.assertTrue(parsedDefault.useGen3) 

144 

145 parsedGen2 = self._parseString(minArgs + '--gen2') 

146 self.assertFalse(parsedGen2.useGen3) 

147 

148 parsedGen3 = self._parseString(minArgs + '--gen3') 

149 self.assertTrue(parsedGen3.useGen3) 

150 

151 with self.assertRaises(SystemExit): 

152 self._parseString(minArgs + '--gen2 --gen3') 

153 

154 

155class MemoryTester(lsst.utils.tests.MemoryTestCase): 

156 pass 

157 

158 

159def setup_module(module): 

160 lsst.utils.tests.init() 

161 

162 

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

164 lsst.utils.tests.init() 

165 unittest.main()