Coverage for tests/test_cliCmdQueryCollections.py: 37%

Shortcuts 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

92 statements  

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 

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

45 

46 @staticmethod 

47 def defaultExpected(): 

48 return dict( 

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

50 ) 

51 

52 @staticmethod 

53 def command(): 

54 return query_collections 

55 

56 def test_minimal(self): 

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

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

59 

60 def test_all(self): 

61 """Test all parameters""" 

62 self.run_test( 

63 ["query-collections", "here", "foo*", "--collection-type", "TAGGED", "--collection-type", "RUN"], 

64 self.makeExpected( 

65 repo="here", 

66 glob=("foo*",), 

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

68 chains="TABLE", 

69 ), 

70 ) 

71 

72 

73class QueryCollectionsScriptTest(ButlerTestHelper, unittest.TestCase): 

74 def setUp(self): 

75 self.runner = LogCliRunner() 

76 

77 def testGetCollections(self): 

78 run = "ingest/run" 

79 tag = "tag" 

80 with self.runner.isolated_filesystem(): 

81 butlerCfg = Butler.makeRepo("here") 

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

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

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

85 

86 # Verify collections that were created are found by 

87 # query-collections. 

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

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

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

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

92 

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

94 # name matches with the specified pattern are returned. 

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

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

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

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

99 

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

101 # that type are returned. 

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

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

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

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

106 

107 

108class ChainedCollectionsTest(ButlerTestHelper, unittest.TestCase): 

109 def setUp(self): 

110 self.runner = LogCliRunner() 

111 

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

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

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

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

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

117 

118 def testChained(self): 

119 with self.runner.isolated_filesystem(): 

120 

121 # Create a butler and add some chained collections: 

122 butlerCfg = Butler.makeRepo("here") 

123 

124 butler1 = Butler(butlerCfg, writeable=True) 

125 

126 # Replace datastore functions with mocks: 

127 DatastoreMock.apply(butler1) 

128 

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

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

131 registry1 = butler1.registry 

132 registry1.registerRun("run1") 

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

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

135 

136 # Create the collection chain 

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

138 self.assertChain( 

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

140 ) 

141 

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

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

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

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

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

147 # the command line interface. 

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

149 

150 # self.assertEqual(result.exit_code, 0, clickResultMsg(result)) 

151 expected = Table( 

152 array( 

153 ( 

154 ("imported_g", "RUN"), 

155 ("imported_r", "RUN"), 

156 ("run1", "RUN"), 

157 ("tag1", "TAGGED"), 

158 ("calibration1", "CALIBRATION"), 

159 ("chain2", "CHAINED"), 

160 (" calibration1", "CALIBRATION"), 

161 (" run1", "RUN"), 

162 ("chain1", "CHAINED"), 

163 (" tag1", "TAGGED"), 

164 (" run1", "RUN"), 

165 (" chain2", "CHAINED"), 

166 (" calibration1", "CALIBRATION"), 

167 (" run1", "RUN"), 

168 ) 

169 ), 

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

171 ) 

172 self.assertAstropyTablesEqual(table, expected) 

173 

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

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

176 expected = Table( 

177 array( 

178 ( 

179 ("imported_g", "RUN", ""), 

180 ("imported_r", "RUN", ""), 

181 ("run1", "RUN", ""), 

182 ("tag1", "TAGGED", ""), 

183 ("calibration1", "CALIBRATION", ""), 

184 ("chain2", "CHAINED", "[calibration1, run1]"), 

185 ("chain1", "CHAINED", "[tag1, run1, chain2]"), 

186 ) 

187 ), 

188 names=("Name", "Type", "Definition"), 

189 ) 

190 table = readTable(result.output) 

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

192 

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

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

195 expected = Table( 

196 array( 

197 ( 

198 ("imported_g", "RUN"), 

199 ("imported_r", "RUN"), 

200 ("run1", "RUN"), 

201 ("tag1", "TAGGED"), 

202 ("calibration1", "CALIBRATION"), 

203 ("calibration1", "CALIBRATION"), 

204 ("run1", "RUN"), 

205 ("tag1", "TAGGED"), 

206 ("run1", "RUN"), 

207 ("calibration1", "CALIBRATION"), 

208 ) 

209 ), 

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

211 ) 

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

213 

214 # Add a couple more run collections for chain testing 

215 registry1.registerRun("run2") 

216 registry1.registerRun("run3") 

217 registry1.registerRun("run4") 

218 

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

220 

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

222 

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

224 

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

226 

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

228 

229 self.assertChain( 

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

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

232 ) 

233 

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

235 

236 

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

238 unittest.main()