Coverage for tests/test_cliCmdQueryCollections.py: 28%

98 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-02 02:16 -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 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 

27from typing import List 

28 

29from astropy.table import Table 

30from lsst.daf.butler import Butler, CollectionType 

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

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

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

34from lsst.daf.butler.script import queryCollections 

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

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

37from numpy import array 

38 

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

40 

41 

42class QueryCollectionsCmdTest(CliCmdTestBase, unittest.TestCase): 

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

44 

45 @staticmethod 

46 def defaultExpected(): 

47 return dict( 

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

49 ) 

50 

51 @staticmethod 

52 def command(): 

53 return query_collections 

54 

55 def test_minimal(self): 

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

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

58 

59 def test_all(self): 

60 """Test all parameters""" 

61 self.run_test( 

62 [ 

63 "query-collections", 

64 "here", 

65 "foo*", 

66 "--collection-type", 

67 "TAGGED", 

68 "--collection-type", 

69 "RUN", 

70 "--chains", 

71 "TABLE", 

72 ], 

73 self.makeExpected( 

74 repo="here", 

75 glob=("foo*",), 

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

77 chains="TABLE", 

78 ), 

79 ) 

80 

81 

82class QueryCollectionsScriptTest(ButlerTestHelper, unittest.TestCase): 

83 def setUp(self): 

84 self.runner = LogCliRunner() 

85 

86 def testGetCollections(self): 

87 run = "ingest/run" 

88 tag = "tag" 

89 with self.runner.isolated_filesystem(): 

90 butlerCfg = Butler.makeRepo("here") 

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

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

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

94 

95 # Verify collections that were created are found by 

96 # query-collections. 

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

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

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

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

101 

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

103 # name matches with the specified pattern are returned. 

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

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

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

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

108 

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

110 # that type are returned. 

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

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

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

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

115 

116 

117class ChainedCollectionsTest(ButlerTestHelper, unittest.TestCase): 

118 def setUp(self): 

119 self.runner = LogCliRunner() 

120 

121 def assertChain(self, args: List[str], expected: str): 

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

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

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

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

126 

127 def testChained(self): 

128 with self.runner.isolated_filesystem(): 

129 # Create a butler and add some chained collections: 

130 butlerCfg = Butler.makeRepo("here") 

131 

132 butler1 = Butler(butlerCfg, writeable=True) 

133 

134 # Replace datastore functions with mocks: 

135 DatastoreMock.apply(butler1) 

136 

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

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

139 registry1 = butler1.registry 

140 registry1.registerRun("run1") 

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

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

143 

144 # Create the collection chain 

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

146 self.assertChain( 

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

148 ) 

149 

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

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

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

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

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

155 # the command line interface. 

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

157 

158 expected = Table( 

159 array( 

160 ( 

161 ("calibration1", "CALIBRATION"), 

162 ("chain1", "CHAINED"), 

163 (" tag1", "TAGGED"), 

164 (" run1", "RUN"), 

165 (" chain2", "CHAINED"), 

166 (" calibration1", "CALIBRATION"), 

167 (" run1", "RUN"), 

168 ("chain2", "CHAINED"), 

169 (" calibration1", "CALIBRATION"), 

170 (" run1", "RUN"), 

171 ("imported_g", "RUN"), 

172 ("imported_r", "RUN"), 

173 ("run1", "RUN"), 

174 ("tag1", "TAGGED"), 

175 ) 

176 ), 

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

178 ) 

179 self.assertAstropyTablesEqual(table, expected) 

180 

181 # Test table with inverse == True 

182 table = queryCollections( 

183 "here", 

184 glob=(), 

185 collection_type=CollectionType.all(), 

186 chains="INVERSE-TREE", 

187 ) 

188 expected = Table( 

189 array( 

190 ( 

191 ("calibration1", "CALIBRATION"), 

192 (" chain2", "CHAINED"), 

193 (" chain1", "CHAINED"), 

194 ("chain1", "CHAINED"), 

195 ("chain2", "CHAINED"), 

196 (" chain1", "CHAINED"), 

197 ("imported_g", "RUN"), 

198 ("imported_r", "RUN"), 

199 ("run1", "RUN"), 

200 (" chain1", "CHAINED"), 

201 (" chain2", "CHAINED"), 

202 (" chain1", "CHAINED"), 

203 ("tag1", "TAGGED"), 

204 (" chain1", "CHAINED"), 

205 ) 

206 ), 

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

208 ) 

209 self.assertAstropyTablesEqual(table, expected) 

210 

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

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

213 expected = Table( 

214 array( 

215 ( 

216 ("calibration1", "CALIBRATION", ""), 

217 ("chain1", "CHAINED", "chain2"), 

218 ("", "", "run1"), 

219 ("", "", "tag1"), 

220 ("chain2", "CHAINED", "calibration1"), 

221 ("", "", "run1"), 

222 ("imported_g", "RUN", ""), 

223 ("imported_r", "RUN", ""), 

224 ("run1", "RUN", ""), 

225 ("tag1", "TAGGED", ""), 

226 ) 

227 ), 

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

229 ) 

230 table = readTable(result.output) 

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

232 

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

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

235 expected = Table( 

236 array( 

237 ( 

238 ("calibration1", "CALIBRATION", "chain2"), 

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

240 ("chain2", "CHAINED", "chain1"), 

241 ("imported_g", "RUN", ""), 

242 ("imported_r", "RUN", ""), 

243 ("run1", "RUN", "chain1"), 

244 ("", "", "chain2"), 

245 ("tag1", "TAGGED", "chain1"), 

246 ) 

247 ), 

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

249 ) 

250 table = readTable(result.output) 

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

252 

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

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

255 expected = Table( 

256 array( 

257 ( 

258 ("calibration1", "CALIBRATION"), 

259 ("imported_g", "RUN"), 

260 ("imported_r", "RUN"), 

261 ("run1", "RUN"), 

262 ("tag1", "TAGGED"), 

263 ) 

264 ), 

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

266 ) 

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

268 

269 # Add a couple more run collections for chain testing 

270 registry1.registerRun("run2") 

271 registry1.registerRun("run3") 

272 registry1.registerRun("run4") 

273 

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

275 

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

277 

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

279 

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

281 

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

283 

284 self.assertChain( 

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

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

287 ) 

288 

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

290 

291 

292if __name__ == "__main__": 

293 unittest.main()