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