Coverage for tests/test_cliCmdQueryDimensionRecords.py: 36%

59 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-27 03:00 -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 os 

32import unittest 

33 

34from astropy.table import Table as AstropyTable 

35from lsst.daf.butler import Butler, StorageClassFactory 

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

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

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

39 ButlerTestHelper, 

40 MetricTestRepo, 

41 makeTestTempDir, 

42 readTable, 

43 removeTestTempDir, 

44) 

45from numpy import array 

46 

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

48 

49 

50class QueryDimensionRecordsTest(unittest.TestCase, ButlerTestHelper): 

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

52 

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

54 

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

56 storageClassFactory = StorageClassFactory() 

57 

58 expectedColumnNames = ( 

59 "instrument", 

60 "id", 

61 "day_obs", 

62 "physical_filter", 

63 "name", 

64 "seq_num", 

65 "exposure_time", 

66 "target_name", 

67 "observation_reason", 

68 "science_program", 

69 "azimuth", 

70 "zenith_angle", 

71 "region", 

72 "timespan (TAI)", 

73 ) 

74 

75 def setUp(self): 

76 self.root = makeTestTempDir(TESTDIR) 

77 self.testRepo = MetricTestRepo( 

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

79 ) 

80 self.runner = LogCliRunner() 

81 

82 def tearDown(self): 

83 removeTestTempDir(self.root) 

84 

85 def testBasic(self): 

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

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

88 rows = array( 

89 ( 

90 ( 

91 "DummyCamComp", 

92 "423", 

93 "20200101", 

94 "d-r", 

95 "fourtwentythree", 

96 "None", 

97 "None", 

98 "None", 

99 "None", 

100 "None", 

101 "None", 

102 "None", 

103 "None", 

104 "[2020-01-01T08:00:00, 2020-01-01T08:00:36)", 

105 ), 

106 ( 

107 "DummyCamComp", 

108 "424", 

109 "20200101", 

110 "d-r", 

111 "fourtwentyfour", 

112 "None", 

113 "None", 

114 "None", 

115 "None", 

116 "None", 

117 "None", 

118 "None", 

119 "None", 

120 "None", 

121 ), 

122 ) 

123 ) 

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

125 got = readTable(result.output) 

126 self.assertAstropyTablesEqual(got, expected) 

127 

128 def testWhere(self): 

129 result = self.runner.invoke( 

130 butlerCli, 

131 [ 

132 "query-dimension-records", 

133 self.root, 

134 "visit", 

135 "--where", 

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

137 ], 

138 ) 

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

140 rows = array( 

141 ( 

142 ( 

143 "DummyCamComp", 

144 "423", 

145 "20200101", 

146 "d-r", 

147 "fourtwentythree", 

148 "None", 

149 "None", 

150 "None", 

151 "None", 

152 "None", 

153 "None", 

154 "None", 

155 "None", 

156 "[2020-01-01T08:00:00, 2020-01-01T08:00:36)", 

157 ), 

158 ) 

159 ) 

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

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

162 

163 def testCollection(self): 

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

165 

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

167 self.testRepo.butler = butler 

168 

169 self.testRepo.butler.registry.insertDimensionData( 

170 "visit", 

171 { 

172 "instrument": "DummyCamComp", 

173 "id": 425, 

174 "name": "fourtwentyfive", 

175 "physical_filter": "d-r", 

176 "day_obs": 20200101, 

177 }, 

178 ) 

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

180 

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

182 result = self.runner.invoke( 

183 butlerCli, 

184 [ 

185 "query-dimension-records", 

186 self.root, 

187 "visit", 

188 "--collections", 

189 "ingest/run", 

190 "--datasets", 

191 "test_metric_comp", 

192 ], 

193 ) 

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

195 rows = array( 

196 ( 

197 ( 

198 "DummyCamComp", 

199 "423", 

200 "20200101", 

201 "d-r", 

202 "fourtwentythree", 

203 "None", 

204 "None", 

205 "None", 

206 "None", 

207 "None", 

208 "None", 

209 "None", 

210 "None", 

211 "[2020-01-01T08:00:00, 2020-01-01T08:00:36)", 

212 ), 

213 ( 

214 "DummyCamComp", 

215 "424", 

216 "20200101", 

217 "d-r", 

218 "fourtwentyfour", 

219 "None", 

220 "None", 

221 "None", 

222 "None", 

223 "None", 

224 "None", 

225 "None", 

226 "None", 

227 "None", 

228 ), 

229 ) 

230 ) 

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

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

233 

234 # verify getting records from the "foo" collection 

235 result = self.runner.invoke( 

236 butlerCli, 

237 [ 

238 "query-dimension-records", 

239 self.root, 

240 "visit", 

241 "--collections", 

242 "foo", 

243 "--datasets", 

244 "test_metric_comp", 

245 ], 

246 ) 

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

248 rows = array( 

249 ( 

250 ( 

251 "DummyCamComp", 

252 "425", 

253 "20200101", 

254 "d-r", 

255 "fourtwentyfive", 

256 "None", 

257 "None", 

258 "None", 

259 "None", 

260 "None", 

261 "None", 

262 "None", 

263 "None", 

264 "None", 

265 ), 

266 ) 

267 ) 

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

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

270 

271 def testSkymap(self): 

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

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

274 self.testRepo.butler = butler 

275 

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

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

278 

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

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

281 

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

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

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

285 

286 

287if __name__ == "__main__": 

288 unittest.main()