Coverage for tests / test_cliCmdQueryDimensionRecords.py: 33%
61 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-30 08:41 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-30 08:41 +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 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/>.
28"""Unit tests for daf_butler CLI query-collections command."""
30import os
31import unittest
33from astropy.table import Table as AstropyTable
34from numpy import array
36from lsst.daf.butler import Butler, StorageClassFactory
37from lsst.daf.butler.cli.butler import cli as butlerCli
38from lsst.daf.butler.cli.utils import LogCliRunner, clickResultMsg
39from lsst.daf.butler.tests.utils import (
40 ButlerTestHelper,
41 MetricTestRepo,
42 makeTestTempDir,
43 readTable,
44 removeTestTempDir,
45)
47TESTDIR = os.path.abspath(os.path.dirname(__file__))
50class QueryDimensionRecordsTest(unittest.TestCase, ButlerTestHelper):
51 """Test the query-dimension-records command-line."""
53 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.queryDimensionRecords"
55 configFile = os.path.join(TESTDIR, "config/basic/butler.yaml")
56 storageClassFactory = StorageClassFactory()
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 )
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.enterContext(self.testRepo.butler)
81 self.runner = LogCliRunner()
83 def tearDown(self):
84 removeTestTempDir(self.root)
86 def testBasic(self):
87 result = self.runner.invoke(butlerCli, ["query-dimension-records", self.root, "visit"])
88 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
89 rows = array(
90 (
91 (
92 "DummyCamComp",
93 "423",
94 "20200101",
95 "d-r",
96 "fourtwentythree",
97 "None",
98 "None",
99 "None",
100 "None",
101 "None",
102 "None",
103 "None",
104 "None",
105 "[2020-01-01T08:00:00, 2020-01-01T08:00:36)",
106 ),
107 (
108 "DummyCamComp",
109 "424",
110 "20200101",
111 "d-r",
112 "fourtwentyfour",
113 "None",
114 "None",
115 "None",
116 "None",
117 "None",
118 "None",
119 "None",
120 "None",
121 "None",
122 ),
123 )
124 )
125 expected = AstropyTable(rows, names=self.expectedColumnNames)
126 got = readTable(result.output)
127 self.assertAstropyTablesEqual(got, expected)
129 def testWhere(self):
130 result = self.runner.invoke(
131 butlerCli,
132 [
133 "query-dimension-records",
134 self.root,
135 "visit",
136 "--where",
137 "instrument='DummyCamComp' AND visit.name='fourtwentythree'",
138 ],
139 )
140 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
141 rows = array(
142 (
143 (
144 "DummyCamComp",
145 "423",
146 "20200101",
147 "d-r",
148 "fourtwentythree",
149 "None",
150 "None",
151 "None",
152 "None",
153 "None",
154 "None",
155 "None",
156 "None",
157 "[2020-01-01T08:00:00, 2020-01-01T08:00:36)",
158 ),
159 )
160 )
161 expected = AstropyTable(rows, names=self.expectedColumnNames)
162 self.assertAstropyTablesEqual(readTable(result.output), expected)
164 def testCollection(self):
165 butler = Butler.from_config(self.root, run="foo")
166 self.enterContext(butler)
168 # try replacing the testRepo's butler with the one with the "foo" run.
169 self.testRepo.butler = butler
171 self.testRepo.butler.registry.insertDimensionData(
172 "visit",
173 {
174 "instrument": "DummyCamComp",
175 "id": 425,
176 "name": "fourtwentyfive",
177 "physical_filter": "d-r",
178 "day_obs": 20200101,
179 },
180 )
181 self.testRepo.addDataset(dataId={"instrument": "DummyCamComp", "visit": 425}, run="foo")
183 # verify getting records from the "ingest/run" collection
184 result = self.runner.invoke(
185 butlerCli,
186 [
187 "query-dimension-records",
188 self.root,
189 "visit",
190 "--collections",
191 "ingest/run",
192 "--datasets",
193 "test_metric_comp",
194 ],
195 )
196 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
197 rows = array(
198 (
199 (
200 "DummyCamComp",
201 "423",
202 "20200101",
203 "d-r",
204 "fourtwentythree",
205 "None",
206 "None",
207 "None",
208 "None",
209 "None",
210 "None",
211 "None",
212 "None",
213 "[2020-01-01T08:00:00, 2020-01-01T08:00:36)",
214 ),
215 (
216 "DummyCamComp",
217 "424",
218 "20200101",
219 "d-r",
220 "fourtwentyfour",
221 "None",
222 "None",
223 "None",
224 "None",
225 "None",
226 "None",
227 "None",
228 "None",
229 "None",
230 ),
231 )
232 )
233 expected = AstropyTable(rows, names=self.expectedColumnNames)
234 self.assertAstropyTablesEqual(readTable(result.output), expected)
236 # verify getting records from the "foo" collection
237 result = self.runner.invoke(
238 butlerCli,
239 [
240 "query-dimension-records",
241 self.root,
242 "visit",
243 "--collections",
244 "foo",
245 "--datasets",
246 "test_metric_comp",
247 ],
248 )
249 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
250 rows = array(
251 (
252 (
253 "DummyCamComp",
254 "425",
255 "20200101",
256 "d-r",
257 "fourtwentyfive",
258 "None",
259 "None",
260 "None",
261 "None",
262 "None",
263 "None",
264 "None",
265 "None",
266 "None",
267 ),
268 )
269 )
270 expected = AstropyTable(rows, names=self.expectedColumnNames)
271 self.assertAstropyTablesEqual(readTable(result.output), expected)
273 def testSkymap(self):
274 butler = Butler.from_config(self.root, run="foo")
275 self.enterContext(butler)
276 # try replacing the testRepo's butler with the one with the "foo" run.
277 self.testRepo.butler = butler
279 skymapRecord = {"name": "example_skymap", "hash": (50).to_bytes(8, byteorder="little")}
280 self.testRepo.butler.registry.insertDimensionData("skymap", skymapRecord)
282 result = self.runner.invoke(butlerCli, ["query-dimension-records", self.root, "skymap"])
283 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
285 rows = array(("example_skymap", "0x3200000000000000", "None", "None", "None"))
286 expected = AstropyTable(rows, names=["name", "hash", "tract_max", "patch_nx_max", "patch_ny_max"])
287 self.assertAstropyTablesEqual(readTable(result.output), expected)
290if __name__ == "__main__":
291 unittest.main()