Coverage for tests/test_cliCmdQueryDimensionRecords.py: 42%

63 statements  

« prev     ^ index     » next       coverage.py v6.4, created at 2022-05-26 09:34 +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 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 "visit_system", 

62 "name", 

63 "day_obs", 

64 "exposure_time", 

65 "target_name", 

66 "observation_reason", 

67 "science_program", 

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 "1", 

93 "fourtwentythree", 

94 "None", 

95 "None", 

96 "None", 

97 "None", 

98 "None", 

99 "None", 

100 "None", 

101 "None .. None", 

102 ), 

103 ( 

104 "DummyCamComp", 

105 "424", 

106 "d-r", 

107 "1", 

108 "fourtwentyfour", 

109 "None", 

110 "None", 

111 "None", 

112 "None", 

113 "None", 

114 "None", 

115 "None", 

116 "None .. None", 

117 ), 

118 ) 

119 ) 

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

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

122 

123 def testWhere(self): 

124 result = self.runner.invoke( 

125 butlerCli, 

126 [ 

127 "query-dimension-records", 

128 self.root, 

129 "visit", 

130 "--where", 

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

132 ], 

133 ) 

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

135 rows = array( 

136 ( 

137 ( 

138 "DummyCamComp", 

139 "423", 

140 "d-r", 

141 "1", 

142 "fourtwentythree", 

143 "None", 

144 "None", 

145 "None", 

146 "None", 

147 "None", 

148 "None", 

149 "None", 

150 "None .. None", 

151 ), 

152 ) 

153 ) 

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

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

156 

157 def testCollection(self): 

158 

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

160 

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

162 self.testRepo.butler = butler 

163 

164 self.testRepo.butler.registry.insertDimensionData( 

165 "visit", 

166 { 

167 "instrument": "DummyCamComp", 

168 "id": 425, 

169 "name": "fourtwentyfive", 

170 "physical_filter": "d-r", 

171 "visit_system": 1, 

172 }, 

173 ) 

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

175 

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

177 result = self.runner.invoke( 

178 butlerCli, 

179 [ 

180 "query-dimension-records", 

181 self.root, 

182 "visit", 

183 "--collections", 

184 "ingest/run", 

185 "--datasets", 

186 "test_metric_comp", 

187 ], 

188 ) 

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

190 rows = array( 

191 ( 

192 ( 

193 "DummyCamComp", 

194 "423", 

195 "d-r", 

196 "1", 

197 "fourtwentythree", 

198 "None", 

199 "None", 

200 "None", 

201 "None", 

202 "None", 

203 "None", 

204 "None", 

205 "None .. None", 

206 ), 

207 ( 

208 "DummyCamComp", 

209 "424", 

210 "d-r", 

211 "1", 

212 "fourtwentyfour", 

213 "None", 

214 "None", 

215 "None", 

216 "None", 

217 "None", 

218 "None", 

219 "None", 

220 "None .. None", 

221 ), 

222 ) 

223 ) 

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

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

226 

227 # verify getting records from the "foo" collection 

228 result = self.runner.invoke( 

229 butlerCli, 

230 [ 

231 "query-dimension-records", 

232 self.root, 

233 "visit", 

234 "--collections", 

235 "foo", 

236 "--datasets", 

237 "test_metric_comp", 

238 ], 

239 ) 

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

241 rows = array( 

242 ( 

243 ( 

244 "DummyCamComp", 

245 "425", 

246 "d-r", 

247 "1", 

248 "fourtwentyfive", 

249 "None", 

250 "None", 

251 "None", 

252 "None", 

253 "None", 

254 "None", 

255 "None", 

256 "None .. None", 

257 ), 

258 ) 

259 ) 

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

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

262 

263 def testSkymap(self): 

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

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

266 self.testRepo.butler = butler 

267 

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

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

270 

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

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

273 

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

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

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

277 

278 

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

280 unittest.main()