Coverage for tests/test_cliCmdQueryCollections.py: 28%

97 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-23 09:30 +0000

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 daf_butler CLI query-collections command. 

23""" 

24 

25import os 

26import unittest 

27 

28from astropy.table import Table 

29from lsst.daf.butler import Butler, CollectionType 

30from lsst.daf.butler.cli.butler import cli 

31from lsst.daf.butler.cli.cmd import query_collections 

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

33from lsst.daf.butler.script import queryCollections 

34from lsst.daf.butler.tests import CliCmdTestBase, DatastoreMock 

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

36from numpy import array 

37 

38TESTDIR = os.path.abspath(os.path.dirname(__file__)) 

39 

40 

41class QueryCollectionsCmdTest(CliCmdTestBase, unittest.TestCase): 

42 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.queryCollections" 

43 

44 @staticmethod 

45 def defaultExpected(): 

46 return dict( 

47 repo=None, collection_type=tuple(CollectionType.__members__.values()), chains="TABLE", glob=() 

48 ) 

49 

50 @staticmethod 

51 def command(): 

52 return query_collections 

53 

54 def test_minimal(self): 

55 """Test only required parameters, and omit optional parameters.""" 

56 self.run_test(["query-collections", "here", "--chains", "TABLE"], self.makeExpected(repo="here")) 

57 

58 def test_all(self): 

59 """Test all parameters""" 

60 self.run_test( 

61 [ 

62 "query-collections", 

63 "here", 

64 "foo*", 

65 "--collection-type", 

66 "TAGGED", 

67 "--collection-type", 

68 "RUN", 

69 "--chains", 

70 "TABLE", 

71 ], 

72 self.makeExpected( 

73 repo="here", 

74 glob=("foo*",), 

75 collection_type=(CollectionType.TAGGED, CollectionType.RUN), 

76 chains="TABLE", 

77 ), 

78 ) 

79 

80 

81class QueryCollectionsScriptTest(ButlerTestHelper, unittest.TestCase): 

82 def setUp(self): 

83 self.runner = LogCliRunner() 

84 

85 def testGetCollections(self): 

86 run = "ingest/run" 

87 tag = "tag" 

88 with self.runner.isolated_filesystem(): 

89 butlerCfg = Butler.makeRepo("here") 

90 # the purpose of this call is to create some collections 

91 butler = Butler(butlerCfg, run=run, collections=[tag], writeable=True) 

92 butler.registry.registerCollection(tag, CollectionType.TAGGED) 

93 

94 # Verify collections that were created are found by 

95 # query-collections. 

96 result = self.runner.invoke(cli, ["query-collections", "here"]) 

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

98 expected = Table((("ingest/run", "tag"), ("RUN", "TAGGED")), names=("Name", "Type")) 

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

100 

101 # Verify that with a glob argument, that only collections whose 

102 # name matches with the specified pattern are returned. 

103 result = self.runner.invoke(cli, ["query-collections", "here", "t*"]) 

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

105 expected = Table((("tag",), ("TAGGED",)), names=("Name", "Type")) 

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

107 

108 # Verify that with a collection type argument, only collections of 

109 # that type are returned. 

110 result = self.runner.invoke(cli, ["query-collections", "here", "--collection-type", "RUN"]) 

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

112 expected = Table((("ingest/run",), ("RUN",)), names=("Name", "Type")) 

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

114 

115 

116class ChainedCollectionsTest(ButlerTestHelper, unittest.TestCase): 

117 def setUp(self): 

118 self.runner = LogCliRunner() 

119 

120 def assertChain(self, args: list[str], expected: str): 

121 """Run collection-chain and check the expected result""" 

122 result = self.runner.invoke(cli, ["collection-chain", "here", *args]) 

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

124 self.assertEqual(result.output.strip(), expected, clickResultMsg(result)) 

125 

126 def testChained(self): 

127 with self.runner.isolated_filesystem(): 

128 # Create a butler and add some chained collections: 

129 butlerCfg = Butler.makeRepo("here") 

130 

131 butler1 = Butler(butlerCfg, writeable=True) 

132 

133 # Replace datastore functions with mocks: 

134 DatastoreMock.apply(butler1) 

135 

136 butler1.import_(filename=os.path.join(TESTDIR, "data", "registry", "base.yaml")) 

137 butler1.import_(filename=os.path.join(TESTDIR, "data", "registry", "datasets.yaml")) 

138 registry1 = butler1.registry 

139 registry1.registerRun("run1") 

140 registry1.registerCollection("tag1", CollectionType.TAGGED) 

141 registry1.registerCollection("calibration1", CollectionType.CALIBRATION) 

142 

143 # Create the collection chain 

144 self.assertChain(["chain2", "calibration1", "run1"], "[calibration1, run1]") 

145 self.assertChain( 

146 ["--mode", "redefine", "chain1", "tag1", "run1", "chain2"], "[tag1, run1, chain2]" 

147 ) 

148 

149 # Use the script function to test the query-collections TREE 

150 # option, because the astropy.table.Table.read method, which we are 

151 # using for verification elsewhere in this file, seems to strip 

152 # leading whitespace from columns. This makes it impossible to test 

153 # the nested TREE output of the query-collections subcommand from 

154 # the command line interface. 

155 table = queryCollections("here", glob=(), collection_type=CollectionType.all(), chains="TREE") 

156 

157 expected = Table( 

158 array( 

159 ( 

160 ("calibration1", "CALIBRATION"), 

161 ("chain1", "CHAINED"), 

162 (" tag1", "TAGGED"), 

163 (" run1", "RUN"), 

164 (" chain2", "CHAINED"), 

165 (" calibration1", "CALIBRATION"), 

166 (" run1", "RUN"), 

167 ("chain2", "CHAINED"), 

168 (" calibration1", "CALIBRATION"), 

169 (" run1", "RUN"), 

170 ("imported_g", "RUN"), 

171 ("imported_r", "RUN"), 

172 ("run1", "RUN"), 

173 ("tag1", "TAGGED"), 

174 ) 

175 ), 

176 names=("Name", "Type"), 

177 ) 

178 self.assertAstropyTablesEqual(table, expected) 

179 

180 # Test table with inverse == True 

181 table = queryCollections( 

182 "here", 

183 glob=(), 

184 collection_type=CollectionType.all(), 

185 chains="INVERSE-TREE", 

186 ) 

187 expected = Table( 

188 array( 

189 ( 

190 ("calibration1", "CALIBRATION"), 

191 (" chain2", "CHAINED"), 

192 (" chain1", "CHAINED"), 

193 ("chain1", "CHAINED"), 

194 ("chain2", "CHAINED"), 

195 (" chain1", "CHAINED"), 

196 ("imported_g", "RUN"), 

197 ("imported_r", "RUN"), 

198 ("run1", "RUN"), 

199 (" chain1", "CHAINED"), 

200 (" chain2", "CHAINED"), 

201 (" chain1", "CHAINED"), 

202 ("tag1", "TAGGED"), 

203 (" chain1", "CHAINED"), 

204 ) 

205 ), 

206 names=("Name", "Type"), 

207 ) 

208 self.assertAstropyTablesEqual(table, expected) 

209 

210 result = self.runner.invoke(cli, ["query-collections", "here", "--chains", "TABLE"]) 

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

212 expected = Table( 

213 array( 

214 ( 

215 ("calibration1", "CALIBRATION", ""), 

216 ("chain1", "CHAINED", "chain2"), 

217 ("", "", "run1"), 

218 ("", "", "tag1"), 

219 ("chain2", "CHAINED", "calibration1"), 

220 ("", "", "run1"), 

221 ("imported_g", "RUN", ""), 

222 ("imported_r", "RUN", ""), 

223 ("run1", "RUN", ""), 

224 ("tag1", "TAGGED", ""), 

225 ) 

226 ), 

227 names=("Name", "Type", "Children"), 

228 ) 

229 table = readTable(result.output) 

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

231 

232 result = self.runner.invoke(cli, ["query-collections", "here", "--chains", "INVERSE-TABLE"]) 

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

234 expected = Table( 

235 array( 

236 ( 

237 ("calibration1", "CALIBRATION", "chain2"), 

238 ("chain1", "CHAINED", ""), 

239 ("chain2", "CHAINED", "chain1"), 

240 ("imported_g", "RUN", ""), 

241 ("imported_r", "RUN", ""), 

242 ("run1", "RUN", "chain1"), 

243 ("", "", "chain2"), 

244 ("tag1", "TAGGED", "chain1"), 

245 ) 

246 ), 

247 names=("Name", "Type", "Parents"), 

248 ) 

249 table = readTable(result.output) 

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

251 

252 result = self.runner.invoke(cli, ["query-collections", "here", "--chains", "FLATTEN"]) 

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

254 expected = Table( 

255 array( 

256 ( 

257 ("calibration1", "CALIBRATION"), 

258 ("imported_g", "RUN"), 

259 ("imported_r", "RUN"), 

260 ("run1", "RUN"), 

261 ("tag1", "TAGGED"), 

262 ) 

263 ), 

264 names=("Name", "Type"), 

265 ) 

266 self.assertAstropyTablesEqual(readTable(result.output), expected, unorderedRows=True) 

267 

268 # Add a couple more run collections for chain testing 

269 registry1.registerRun("run2") 

270 registry1.registerRun("run3") 

271 registry1.registerRun("run4") 

272 

273 self.assertChain(["--mode", "pop", "chain1"], "[run1, chain2]") 

274 

275 self.assertChain(["--mode", "extend", "chain1", "run2", "run3"], "[run1, chain2, run2, run3]") 

276 

277 self.assertChain(["--mode", "remove", "chain1", "chain2", "run2"], "[run1, run3]") 

278 

279 self.assertChain(["--mode", "prepend", "chain1", "chain2", "run2"], "[chain2, run2, run1, run3]") 

280 

281 self.assertChain(["--mode", "pop", "chain1", "1", "3"], "[chain2, run1]") 

282 

283 self.assertChain( 

284 ["--mode", "redefine", "chain1", "chain2", "run2", "run3,run4", "--flatten"], 

285 "[calibration1, run1, run2, run3, run4]", 

286 ) 

287 

288 self.assertChain(["--mode", "pop", "chain1", "--", "-1", "-3"], "[calibration1, run1, run3]") 

289 

290 

291if __name__ == "__main__": 

292 unittest.main()