Coverage for tests/test_cliCmdQueryDimensionRecords.py: 40%

63 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-01-11 02:31 -0800

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 

28import astropy 

29from astropy.table import Table as AstropyTable 

30from astropy.utils.introspection import minversion 

31from lsst.daf.butler import Butler, StorageClassFactory 

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

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

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

35 ButlerTestHelper, 

36 MetricTestRepo, 

37 makeTestTempDir, 

38 readTable, 

39 removeTestTempDir, 

40) 

41from numpy import array 

42 

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

44 

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

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

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

48 

49 

50class QueryDimensionRecordsTest(unittest.TestCase, ButlerTestHelper): 

51 

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

53 

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

55 storageClassFactory = StorageClassFactory() 

56 

57 expectedColumnNames = ( 

58 "instrument", 

59 "id", 

60 "physical_filter", 

61 "name", 

62 "day_obs", 

63 "seq_num", 

64 "exposure_time", 

65 "target_name", 

66 "observation_reason", 

67 "science_program", 

68 "azimuth", 

69 "zenith_angle", 

70 "region", 

71 f"timespan{timespan_columns}", 

72 ) 

73 

74 def setUp(self): 

75 self.root = makeTestTempDir(TESTDIR) 

76 self.testRepo = MetricTestRepo( 

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

78 ) 

79 self.runner = LogCliRunner() 

80 

81 def tearDown(self): 

82 removeTestTempDir(self.root) 

83 

84 def testBasic(self): 

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

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

87 rows = array( 

88 ( 

89 ( 

90 "DummyCamComp", 

91 "423", 

92 "d-r", 

93 "fourtwentythree", 

94 "None", 

95 "None", 

96 "None", 

97 "None", 

98 "None", 

99 "None", 

100 "None", 

101 "None", 

102 "None", 

103 "None .. None", 

104 ), 

105 ( 

106 "DummyCamComp", 

107 "424", 

108 "d-r", 

109 "fourtwentyfour", 

110 "None", 

111 "None", 

112 "None", 

113 "None", 

114 "None", 

115 "None", 

116 "None", 

117 "None", 

118 "None", 

119 "None .. None", 

120 ), 

121 ) 

122 ) 

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

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

125 

126 def testWhere(self): 

127 result = self.runner.invoke( 

128 butlerCli, 

129 [ 

130 "query-dimension-records", 

131 self.root, 

132 "visit", 

133 "--where", 

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

135 ], 

136 ) 

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

138 rows = array( 

139 ( 

140 ( 

141 "DummyCamComp", 

142 "423", 

143 "d-r", 

144 "fourtwentythree", 

145 "None", 

146 "None", 

147 "None", 

148 "None", 

149 "None", 

150 "None", 

151 "None", 

152 "None", 

153 "None", 

154 "None .. None", 

155 ), 

156 ) 

157 ) 

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

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

160 

161 def testCollection(self): 

162 

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

164 

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

166 self.testRepo.butler = butler 

167 

168 self.testRepo.butler.registry.insertDimensionData( 

169 "visit", 

170 { 

171 "instrument": "DummyCamComp", 

172 "id": 425, 

173 "name": "fourtwentyfive", 

174 "physical_filter": "d-r", 

175 }, 

176 ) 

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

178 

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

180 result = self.runner.invoke( 

181 butlerCli, 

182 [ 

183 "query-dimension-records", 

184 self.root, 

185 "visit", 

186 "--collections", 

187 "ingest/run", 

188 "--datasets", 

189 "test_metric_comp", 

190 ], 

191 ) 

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

193 rows = array( 

194 ( 

195 ( 

196 "DummyCamComp", 

197 "423", 

198 "d-r", 

199 "fourtwentythree", 

200 "None", 

201 "None", 

202 "None", 

203 "None", 

204 "None", 

205 "None", 

206 "None", 

207 "None", 

208 "None", 

209 "None .. None", 

210 ), 

211 ( 

212 "DummyCamComp", 

213 "424", 

214 "d-r", 

215 "fourtwentyfour", 

216 "None", 

217 "None", 

218 "None", 

219 "None", 

220 "None", 

221 "None", 

222 "None", 

223 "None", 

224 "None", 

225 "None .. None", 

226 ), 

227 ) 

228 ) 

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

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

231 

232 # verify getting records from the "foo" collection 

233 result = self.runner.invoke( 

234 butlerCli, 

235 [ 

236 "query-dimension-records", 

237 self.root, 

238 "visit", 

239 "--collections", 

240 "foo", 

241 "--datasets", 

242 "test_metric_comp", 

243 ], 

244 ) 

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

246 rows = array( 

247 ( 

248 ( 

249 "DummyCamComp", 

250 "425", 

251 "d-r", 

252 "fourtwentyfive", 

253 "None", 

254 "None", 

255 "None", 

256 "None", 

257 "None", 

258 "None", 

259 "None", 

260 "None", 

261 "None", 

262 "None .. None", 

263 ), 

264 ) 

265 ) 

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

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

268 

269 def testSkymap(self): 

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

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

272 self.testRepo.butler = butler 

273 

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

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

276 

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

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

279 

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

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

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

283 

284 

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

286 unittest.main()