Coverage for tests/test_cliCmdQueryDimensionRecords.py: 39%

61 statements  

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

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 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.queryDimensionRecords" 

52 

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

54 storageClassFactory = StorageClassFactory() 

55 

56 expectedColumnNames = ( 

57 "instrument", 

58 "id", 

59 "physical_filter", 

60 "name", 

61 "day_obs", 

62 "seq_num", 

63 "exposure_time", 

64 "target_name", 

65 "observation_reason", 

66 "science_program", 

67 "azimuth", 

68 "zenith_angle", 

69 "region", 

70 f"timespan{timespan_columns}", 

71 ) 

72 

73 def setUp(self): 

74 self.root = makeTestTempDir(TESTDIR) 

75 self.testRepo = MetricTestRepo( 

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

77 ) 

78 self.runner = LogCliRunner() 

79 

80 def tearDown(self): 

81 removeTestTempDir(self.root) 

82 

83 def testBasic(self): 

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

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

86 rows = array( 

87 ( 

88 ( 

89 "DummyCamComp", 

90 "423", 

91 "d-r", 

92 "fourtwentythree", 

93 "None", 

94 "None", 

95 "None", 

96 "None", 

97 "None", 

98 "None", 

99 "None", 

100 "None", 

101 "None", 

102 "None .. None", 

103 ), 

104 ( 

105 "DummyCamComp", 

106 "424", 

107 "d-r", 

108 "fourtwentyfour", 

109 "None", 

110 "None", 

111 "None", 

112 "None", 

113 "None", 

114 "None", 

115 "None", 

116 "None", 

117 "None", 

118 "None .. None", 

119 ), 

120 ) 

121 ) 

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

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

124 

125 def testWhere(self): 

126 result = self.runner.invoke( 

127 butlerCli, 

128 [ 

129 "query-dimension-records", 

130 self.root, 

131 "visit", 

132 "--where", 

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

134 ], 

135 ) 

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

137 rows = array( 

138 ( 

139 ( 

140 "DummyCamComp", 

141 "423", 

142 "d-r", 

143 "fourtwentythree", 

144 "None", 

145 "None", 

146 "None", 

147 "None", 

148 "None", 

149 "None", 

150 "None", 

151 "None", 

152 "None", 

153 "None .. None", 

154 ), 

155 ) 

156 ) 

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

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

159 

160 def testCollection(self): 

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

162 

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

164 self.testRepo.butler = butler 

165 

166 self.testRepo.butler.registry.insertDimensionData( 

167 "visit", 

168 { 

169 "instrument": "DummyCamComp", 

170 "id": 425, 

171 "name": "fourtwentyfive", 

172 "physical_filter": "d-r", 

173 }, 

174 ) 

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

176 

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

178 result = self.runner.invoke( 

179 butlerCli, 

180 [ 

181 "query-dimension-records", 

182 self.root, 

183 "visit", 

184 "--collections", 

185 "ingest/run", 

186 "--datasets", 

187 "test_metric_comp", 

188 ], 

189 ) 

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

191 rows = array( 

192 ( 

193 ( 

194 "DummyCamComp", 

195 "423", 

196 "d-r", 

197 "fourtwentythree", 

198 "None", 

199 "None", 

200 "None", 

201 "None", 

202 "None", 

203 "None", 

204 "None", 

205 "None", 

206 "None", 

207 "None .. None", 

208 ), 

209 ( 

210 "DummyCamComp", 

211 "424", 

212 "d-r", 

213 "fourtwentyfour", 

214 "None", 

215 "None", 

216 "None", 

217 "None", 

218 "None", 

219 "None", 

220 "None", 

221 "None", 

222 "None", 

223 "None .. None", 

224 ), 

225 ) 

226 ) 

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

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

229 

230 # verify getting records from the "foo" collection 

231 result = self.runner.invoke( 

232 butlerCli, 

233 [ 

234 "query-dimension-records", 

235 self.root, 

236 "visit", 

237 "--collections", 

238 "foo", 

239 "--datasets", 

240 "test_metric_comp", 

241 ], 

242 ) 

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

244 rows = array( 

245 ( 

246 ( 

247 "DummyCamComp", 

248 "425", 

249 "d-r", 

250 "fourtwentyfive", 

251 "None", 

252 "None", 

253 "None", 

254 "None", 

255 "None", 

256 "None", 

257 "None", 

258 "None", 

259 "None", 

260 "None .. None", 

261 ), 

262 ) 

263 ) 

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

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

266 

267 def testSkymap(self): 

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

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

270 self.testRepo.butler = butler 

271 

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

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

274 

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

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

277 

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

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

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

281 

282 

283if __name__ == "__main__": 

284 unittest.main()