Coverage for tests/test_cliCmdQueryDatasetTypes.py: 26%

90 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-26 02:47 -0700

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 software is dual licensed under the GNU General Public License and also 

10# under a 3-clause BSD license. Recipients may choose which of these licenses 

11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt, 

12# respectively. If you choose the GPL option then the following text applies 

13# (but note that there is still no warranty even if you opt for BSD instead): 

14# 

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

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

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

18# (at your option) any later version. 

19# 

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

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

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

23# GNU General Public License for more details. 

24# 

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

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

27 

28"""Unit tests for daf_butler CLI query-collections command. 

29""" 

30 

31import unittest 

32 

33from astropy.table import Table as AstropyTable 

34from lsst.daf.butler.cli.butler import cli 

35from lsst.daf.butler.cli.cmd import query_dataset_types 

36from lsst.daf.butler.cli.utils import LogCliRunner, clickResultMsg 

37from lsst.daf.butler.tests import CliCmdTestBase 

38from lsst.daf.butler.tests.utils import ButlerTestHelper, readTable 

39from numpy import array 

40 

41 

42class QueryDatasetTypesCmdTest(CliCmdTestBase, unittest.TestCase): 

43 """Test the query-dataset-types command line.""" 

44 

45 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.queryDatasetTypes" 

46 

47 @staticmethod 

48 def defaultExpected(): 

49 return dict(repo=None, verbose=False, glob=()) 

50 

51 @staticmethod 

52 def command(): 

53 return query_dataset_types 

54 

55 def test_minimal(self): 

56 """Test only required parameters.""" 

57 self.run_test(["query-dataset-types", "here"], self.makeExpected(repo="here")) 

58 

59 def test_requiredMissing(self): 

60 """Test that if the required parameter is missing it fails""" 

61 self.run_missing(["query-dataset-types"], r"Error: Missing argument ['\"]REPO['\"].") 

62 

63 def test_all(self): 

64 """Test all parameters.""" 

65 self.run_test( 

66 ["query-dataset-types", "here", "--verbose", "foo*"], 

67 self.makeExpected(repo="here", verbose=True, glob=("foo*",)), 

68 ) 

69 self.run_test( 

70 ["query-dataset-types", "here", "--verbose", "foo*"], 

71 self.makeExpected(repo="here", verbose=True, glob=("foo*",)), 

72 ) 

73 

74 

75class QueryDatasetTypesScriptTest(ButlerTestHelper, unittest.TestCase): 

76 """Test the query-dataset-types script interface.""" 

77 

78 def testQueryDatasetTypes(self): 

79 self.maxDiff = None 

80 datasetName = "test" 

81 instrumentDimension = "instrument" 

82 visitDimension = "visit" 

83 storageClassName = "StructuredDataDict" 

84 expectedNotVerbose = AstropyTable((("test",),), names=("name",)) 

85 runner = LogCliRunner() 

86 with runner.isolated_filesystem(): 

87 result = runner.invoke(cli, ["create", "here"]) 

88 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

89 # Create the dataset type. 

90 result = runner.invoke( 

91 cli, 

92 [ 

93 "register-dataset-type", 

94 "here", 

95 datasetName, 

96 storageClassName, 

97 instrumentDimension, 

98 visitDimension, 

99 ], 

100 ) 

101 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

102 # Okay to create it again identically. 

103 result = runner.invoke( 

104 cli, 

105 [ 

106 "register-dataset-type", 

107 "here", 

108 datasetName, 

109 storageClassName, 

110 instrumentDimension, 

111 visitDimension, 

112 ], 

113 ) 

114 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

115 # Not okay to create a different version of it. 

116 result = runner.invoke( 

117 cli, ["register-dataset-type", "here", datasetName, storageClassName, instrumentDimension] 

118 ) 

119 self.assertNotEqual(result.exit_code, 0, clickResultMsg(result)) 

120 # Not okay to try to create a component dataset type. 

121 result = runner.invoke( 

122 cli, ["register-dataset-type", "here", "a.b", storageClassName, instrumentDimension] 

123 ) 

124 self.assertNotEqual(result.exit_code, 0, clickResultMsg(result)) 

125 # check not-verbose output: 

126 result = runner.invoke(cli, ["query-dataset-types", "here"]) 

127 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

128 self.assertAstropyTablesEqual(readTable(result.output), expectedNotVerbose) 

129 # check glob output: 

130 result = runner.invoke(cli, ["query-dataset-types", "here", "t*"]) 

131 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

132 self.assertAstropyTablesEqual(readTable(result.output), expectedNotVerbose) 

133 # check verbose output: 

134 result = runner.invoke(cli, ["query-dataset-types", "here", "--verbose"]) 

135 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

136 expected = AstropyTable( 

137 array( 

138 ( 

139 "test", 

140 "['band', 'instrument', 'day_obs', 'physical_filter', 'visit']", 

141 storageClassName, 

142 ) 

143 ), 

144 names=("name", "dimensions", "storage class"), 

145 ) 

146 self.assertAstropyTablesEqual(readTable(result.output), expected) 

147 

148 # Now remove and check that it was removed 

149 # First a non-existent one 

150 result = runner.invoke(cli, ["remove-dataset-type", "here", "unreal"]) 

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

152 

153 # Now one we now has been registered 

154 result = runner.invoke(cli, ["remove-dataset-type", "here", datasetName]) 

155 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

156 

157 # and check that it has gone 

158 result = runner.invoke(cli, ["query-dataset-types", "here"]) 

159 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

160 self.assertIn("No results", result.output) 

161 

162 def testRemoveDatasetTypes(self): 

163 self.maxDiff = None 

164 datasetName = "test" 

165 instrumentDimension = "instrument" 

166 visitDimension = "visit" 

167 storageClassName = "StructuredDataDict" 

168 runner = LogCliRunner() 

169 with runner.isolated_filesystem(): 

170 result = runner.invoke(cli, ["create", "here"]) 

171 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

172 for name in ( 

173 datasetName, 

174 "testA", 

175 "testB", 

176 "testC", 

177 "testD", 

178 "other", 

179 "another", 

180 "option", 

181 "option2", 

182 "placeholder", 

183 ): 

184 # Create the dataset type. 

185 result = runner.invoke( 

186 cli, 

187 [ 

188 "register-dataset-type", 

189 "here", 

190 name, 

191 storageClassName, 

192 instrumentDimension, 

193 visitDimension, 

194 ], 

195 ) 

196 

197 # Check wildcard / literal combination. 

198 result = runner.invoke(cli, ["remove-dataset-type", "here", "*other", "testA"]) 

199 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

200 self.assertDatasetTypes( 

201 runner, 

202 "*", 

203 ( 

204 "option", 

205 "option2", 

206 "placeholder", 

207 "test", 

208 "testB", 

209 "testC", 

210 "testD", 

211 ), 

212 ) 

213 

214 # Check literal / literal combination. 

215 result = runner.invoke(cli, ["remove-dataset-type", "here", "option", "testB"]) 

216 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

217 self.assertDatasetTypes( 

218 runner, 

219 "*", 

220 ( 

221 "option2", 

222 "placeholder", 

223 "test", 

224 "testC", 

225 "testD", 

226 ), 

227 ) 

228 

229 # Check wildcard. 

230 result = runner.invoke(cli, ["remove-dataset-type", "here", "test*"]) 

231 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

232 self.assertDatasetTypes( 

233 runner, 

234 "*", 

235 ( 

236 "option2", 

237 "placeholder", 

238 ), 

239 ) 

240 

241 # Check literal. 

242 result = runner.invoke(cli, ["remove-dataset-type", "here", "option2"]) 

243 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

244 self.assertDatasetTypes(runner, "*", ("placeholder",)) 

245 

246 def assertDatasetTypes(self, runner: LogCliRunner, query: str, expected: tuple[str, ...]) -> None: 

247 result = runner.invoke(cli, ["query-dataset-types", "here", query]) 

248 self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

249 expected = AstropyTable( 

250 (expected,), 

251 names=("name",), 

252 ) 

253 self.assertAstropyTablesEqual(readTable(result.output), expected) 

254 

255 

256if __name__ == "__main__": 

257 unittest.main()