Coverage for tests/test_cliCmdQueryDimensionRecords.py: 39%

61 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-27 09:44 +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 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 os 

32import unittest 

33 

34import astropy 

35from astropy.table import Table as AstropyTable 

36from astropy.utils.introspection import minversion 

37from lsst.daf.butler import Butler, StorageClassFactory 

38from lsst.daf.butler.cli.butler import cli as butlerCli 

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

40from lsst.daf.butler.tests.utils import ( 

41 ButlerTestHelper, 

42 MetricTestRepo, 

43 makeTestTempDir, 

44 readTable, 

45 removeTestTempDir, 

46) 

47from numpy import array 

48 

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

50 

51# Astropy changed the handling of numpy columns in v5.1 so anything 

52# greater than 5.0 (two digit version) does not need the annotated column. 

53timespan_columns = "" if minversion(astropy, "5.1") else " [2]" 

54 

55 

56class QueryDimensionRecordsTest(unittest.TestCase, ButlerTestHelper): 

57 """Test the query-dimension-records command-line.""" 

58 

59 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.queryDimensionRecords" 

60 

61 configFile = os.path.join(TESTDIR, "config/basic/butler.yaml") 

62 storageClassFactory = StorageClassFactory() 

63 

64 expectedColumnNames = ( 

65 "instrument", 

66 "id", 

67 "physical_filter", 

68 "name", 

69 "day_obs", 

70 "seq_num", 

71 "exposure_time", 

72 "target_name", 

73 "observation_reason", 

74 "science_program", 

75 "azimuth", 

76 "zenith_angle", 

77 "region", 

78 f"timespan{timespan_columns}", 

79 ) 

80 

81 def setUp(self): 

82 self.root = makeTestTempDir(TESTDIR) 

83 self.testRepo = MetricTestRepo( 

84 self.root, configFile=os.path.join(TESTDIR, "config/basic/butler.yaml") 

85 ) 

86 self.runner = LogCliRunner() 

87 

88 def tearDown(self): 

89 removeTestTempDir(self.root) 

90 

91 def testBasic(self): 

92 result = self.runner.invoke(butlerCli, ["query-dimension-records", self.root, "visit"]) 

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

94 rows = array( 

95 ( 

96 ( 

97 "DummyCamComp", 

98 "423", 

99 "d-r", 

100 "fourtwentythree", 

101 "None", 

102 "None", 

103 "None", 

104 "None", 

105 "None", 

106 "None", 

107 "None", 

108 "None", 

109 "None", 

110 "None .. None", 

111 ), 

112 ( 

113 "DummyCamComp", 

114 "424", 

115 "d-r", 

116 "fourtwentyfour", 

117 "None", 

118 "None", 

119 "None", 

120 "None", 

121 "None", 

122 "None", 

123 "None", 

124 "None", 

125 "None", 

126 "None .. None", 

127 ), 

128 ) 

129 ) 

130 expected = AstropyTable(rows, names=self.expectedColumnNames) 

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

132 

133 def testWhere(self): 

134 result = self.runner.invoke( 

135 butlerCli, 

136 [ 

137 "query-dimension-records", 

138 self.root, 

139 "visit", 

140 "--where", 

141 "instrument='DummyCamComp' AND visit.name='fourtwentythree'", 

142 ], 

143 ) 

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

145 rows = array( 

146 ( 

147 ( 

148 "DummyCamComp", 

149 "423", 

150 "d-r", 

151 "fourtwentythree", 

152 "None", 

153 "None", 

154 "None", 

155 "None", 

156 "None", 

157 "None", 

158 "None", 

159 "None", 

160 "None", 

161 "None .. None", 

162 ), 

163 ) 

164 ) 

165 expected = AstropyTable(rows, names=self.expectedColumnNames) 

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

167 

168 def testCollection(self): 

169 butler = Butler.from_config(self.root, run="foo") 

170 

171 # try replacing the testRepo's butler with the one with the "foo" run. 

172 self.testRepo.butler = butler 

173 

174 self.testRepo.butler.registry.insertDimensionData( 

175 "visit", 

176 { 

177 "instrument": "DummyCamComp", 

178 "id": 425, 

179 "name": "fourtwentyfive", 

180 "physical_filter": "d-r", 

181 }, 

182 ) 

183 self.testRepo.addDataset(dataId={"instrument": "DummyCamComp", "visit": 425}, run="foo") 

184 

185 # verify getting records from the "ingest/run" collection 

186 result = self.runner.invoke( 

187 butlerCli, 

188 [ 

189 "query-dimension-records", 

190 self.root, 

191 "visit", 

192 "--collections", 

193 "ingest/run", 

194 "--datasets", 

195 "test_metric_comp", 

196 ], 

197 ) 

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

199 rows = array( 

200 ( 

201 ( 

202 "DummyCamComp", 

203 "423", 

204 "d-r", 

205 "fourtwentythree", 

206 "None", 

207 "None", 

208 "None", 

209 "None", 

210 "None", 

211 "None", 

212 "None", 

213 "None", 

214 "None", 

215 "None .. None", 

216 ), 

217 ( 

218 "DummyCamComp", 

219 "424", 

220 "d-r", 

221 "fourtwentyfour", 

222 "None", 

223 "None", 

224 "None", 

225 "None", 

226 "None", 

227 "None", 

228 "None", 

229 "None", 

230 "None", 

231 "None .. None", 

232 ), 

233 ) 

234 ) 

235 expected = AstropyTable(rows, names=self.expectedColumnNames) 

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

237 

238 # verify getting records from the "foo" collection 

239 result = self.runner.invoke( 

240 butlerCli, 

241 [ 

242 "query-dimension-records", 

243 self.root, 

244 "visit", 

245 "--collections", 

246 "foo", 

247 "--datasets", 

248 "test_metric_comp", 

249 ], 

250 ) 

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

252 rows = array( 

253 ( 

254 ( 

255 "DummyCamComp", 

256 "425", 

257 "d-r", 

258 "fourtwentyfive", 

259 "None", 

260 "None", 

261 "None", 

262 "None", 

263 "None", 

264 "None", 

265 "None", 

266 "None", 

267 "None", 

268 "None .. None", 

269 ), 

270 ) 

271 ) 

272 expected = AstropyTable(rows, names=self.expectedColumnNames) 

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

274 

275 def testSkymap(self): 

276 butler = Butler.from_config(self.root, run="foo") 

277 # try replacing the testRepo's butler with the one with the "foo" run. 

278 self.testRepo.butler = butler 

279 

280 skymapRecord = {"name": "example_skymap", "hash": (50).to_bytes(8, byteorder="little")} 

281 self.testRepo.butler.registry.insertDimensionData("skymap", skymapRecord) 

282 

283 result = self.runner.invoke(butlerCli, ["query-dimension-records", self.root, "skymap"]) 

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

285 

286 rows = array(("example_skymap", "0x3200000000000000", "None", "None", "None")) 

287 expected = AstropyTable(rows, names=["name", "hash", "tract_max", "patch_nx_max", "patch_ny_max"]) 

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

289 

290 

291if __name__ == "__main__": 

292 unittest.main()