Coverage for tests/test_cliCmdQueryDimensionRecords.py: 42%
63 statements
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-19 12:13 +0000
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-19 12:13 +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/>.
22"""Unit tests for daf_butler CLI query-collections command.
23"""
25import os
26import unittest
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
43TESTDIR = os.path.abspath(os.path.dirname(__file__))
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]"
50class QueryDimensionRecordsTest(unittest.TestCase, ButlerTestHelper):
52 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.queryDimensionRecords"
54 configFile = os.path.join(TESTDIR, "config/basic/butler.yaml")
55 storageClassFactory = StorageClassFactory()
57 expectedColumnNames = (
58 "instrument",
59 "id",
60 "physical_filter",
61 "name",
62 "day_obs",
63 "exposure_time",
64 "target_name",
65 "observation_reason",
66 "science_program",
67 "zenith_angle",
68 "region",
69 f"timespan{timespan_columns}",
70 )
72 def setUp(self):
73 self.root = makeTestTempDir(TESTDIR)
74 self.testRepo = MetricTestRepo(
75 self.root, configFile=os.path.join(TESTDIR, "config/basic/butler.yaml")
76 )
77 self.runner = LogCliRunner()
79 def tearDown(self):
80 removeTestTempDir(self.root)
82 def testBasic(self):
83 result = self.runner.invoke(butlerCli, ["query-dimension-records", self.root, "visit"])
84 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
85 rows = array(
86 (
87 (
88 "DummyCamComp",
89 "423",
90 "d-r",
91 "fourtwentythree",
92 "None",
93 "None",
94 "None",
95 "None",
96 "None",
97 "None",
98 "None",
99 "None .. None",
100 ),
101 (
102 "DummyCamComp",
103 "424",
104 "d-r",
105 "fourtwentyfour",
106 "None",
107 "None",
108 "None",
109 "None",
110 "None",
111 "None",
112 "None",
113 "None .. None",
114 ),
115 )
116 )
117 expected = AstropyTable(rows, names=self.expectedColumnNames)
118 self.assertAstropyTablesEqual(readTable(result.output), expected)
120 def testWhere(self):
121 result = self.runner.invoke(
122 butlerCli,
123 [
124 "query-dimension-records",
125 self.root,
126 "visit",
127 "--where",
128 "instrument='DummyCamComp' AND visit.name='fourtwentythree'",
129 ],
130 )
131 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
132 rows = array(
133 (
134 (
135 "DummyCamComp",
136 "423",
137 "d-r",
138 "fourtwentythree",
139 "None",
140 "None",
141 "None",
142 "None",
143 "None",
144 "None",
145 "None",
146 "None .. None",
147 ),
148 )
149 )
150 expected = AstropyTable(rows, names=self.expectedColumnNames)
151 self.assertAstropyTablesEqual(readTable(result.output), expected)
153 def testCollection(self):
155 butler = Butler(self.root, run="foo")
157 # try replacing the testRepo's butler with the one with the "foo" run.
158 self.testRepo.butler = butler
160 self.testRepo.butler.registry.insertDimensionData(
161 "visit",
162 {
163 "instrument": "DummyCamComp",
164 "id": 425,
165 "name": "fourtwentyfive",
166 "physical_filter": "d-r",
167 },
168 )
169 self.testRepo.addDataset(dataId={"instrument": "DummyCamComp", "visit": 425}, run="foo")
171 # verify getting records from the "ingest/run" collection
172 result = self.runner.invoke(
173 butlerCli,
174 [
175 "query-dimension-records",
176 self.root,
177 "visit",
178 "--collections",
179 "ingest/run",
180 "--datasets",
181 "test_metric_comp",
182 ],
183 )
184 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
185 rows = array(
186 (
187 (
188 "DummyCamComp",
189 "423",
190 "d-r",
191 "fourtwentythree",
192 "None",
193 "None",
194 "None",
195 "None",
196 "None",
197 "None",
198 "None",
199 "None .. None",
200 ),
201 (
202 "DummyCamComp",
203 "424",
204 "d-r",
205 "fourtwentyfour",
206 "None",
207 "None",
208 "None",
209 "None",
210 "None",
211 "None",
212 "None",
213 "None .. None",
214 ),
215 )
216 )
217 expected = AstropyTable(rows, names=self.expectedColumnNames)
218 self.assertAstropyTablesEqual(readTable(result.output), expected)
220 # verify getting records from the "foo" collection
221 result = self.runner.invoke(
222 butlerCli,
223 [
224 "query-dimension-records",
225 self.root,
226 "visit",
227 "--collections",
228 "foo",
229 "--datasets",
230 "test_metric_comp",
231 ],
232 )
233 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
234 rows = array(
235 (
236 (
237 "DummyCamComp",
238 "425",
239 "d-r",
240 "fourtwentyfive",
241 "None",
242 "None",
243 "None",
244 "None",
245 "None",
246 "None",
247 "None",
248 "None .. None",
249 ),
250 )
251 )
252 expected = AstropyTable(rows, names=self.expectedColumnNames)
253 self.assertAstropyTablesEqual(readTable(result.output), expected)
255 def testSkymap(self):
256 butler = Butler(self.root, run="foo")
257 # try replacing the testRepo's butler with the one with the "foo" run.
258 self.testRepo.butler = butler
260 skymapRecord = {"name": "example_skymap", "hash": (50).to_bytes(8, byteorder="little")}
261 self.testRepo.butler.registry.insertDimensionData("skymap", skymapRecord)
263 result = self.runner.invoke(butlerCli, ["query-dimension-records", self.root, "skymap"])
264 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
266 rows = array((("example_skymap", "0x3200000000000000", "None", "None", "None")))
267 expected = AstropyTable(rows, names=["name", "hash", "tract_max", "patch_nx_max", "patch_ny_max"])
268 self.assertAstropyTablesEqual(readTable(result.output), expected)
271if __name__ == "__main__": 271 ↛ 272line 271 didn't jump to line 272, because the condition on line 271 was never true
272 unittest.main()