Coverage for tests/test_cliCmdQueryDimensionRecords.py: 39%
61 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-12 10:56 -0700
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-12 10:56 -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/>.
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):
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 f"timespan{timespan_columns}",
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 "None .. None",
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 .. None",
121 ),
122 )
123 )
124 expected = AstropyTable(rows, names=self.expectedColumnNames)
125 self.assertAstropyTablesEqual(readTable(result.output), expected)
127 def testWhere(self):
128 result = self.runner.invoke(
129 butlerCli,
130 [
131 "query-dimension-records",
132 self.root,
133 "visit",
134 "--where",
135 "instrument='DummyCamComp' AND visit.name='fourtwentythree'",
136 ],
137 )
138 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
139 rows = array(
140 (
141 (
142 "DummyCamComp",
143 "423",
144 "d-r",
145 "fourtwentythree",
146 "None",
147 "None",
148 "None",
149 "None",
150 "None",
151 "None",
152 "None",
153 "None",
154 "None",
155 "None .. None",
156 ),
157 )
158 )
159 expected = AstropyTable(rows, names=self.expectedColumnNames)
160 self.assertAstropyTablesEqual(readTable(result.output), expected)
162 def testCollection(self):
163 butler = Butler(self.root, run="foo")
165 # try replacing the testRepo's butler with the one with the "foo" run.
166 self.testRepo.butler = butler
168 self.testRepo.butler.registry.insertDimensionData(
169 "visit",
170 {
171 "instrument": "DummyCamComp",
172 "id": 425,
173 "name": "fourtwentyfive",
174 "physical_filter": "d-r",
175 },
176 )
177 self.testRepo.addDataset(dataId={"instrument": "DummyCamComp", "visit": 425}, run="foo")
179 # verify getting records from the "ingest/run" collection
180 result = self.runner.invoke(
181 butlerCli,
182 [
183 "query-dimension-records",
184 self.root,
185 "visit",
186 "--collections",
187 "ingest/run",
188 "--datasets",
189 "test_metric_comp",
190 ],
191 )
192 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
193 rows = array(
194 (
195 (
196 "DummyCamComp",
197 "423",
198 "d-r",
199 "fourtwentythree",
200 "None",
201 "None",
202 "None",
203 "None",
204 "None",
205 "None",
206 "None",
207 "None",
208 "None",
209 "None .. None",
210 ),
211 (
212 "DummyCamComp",
213 "424",
214 "d-r",
215 "fourtwentyfour",
216 "None",
217 "None",
218 "None",
219 "None",
220 "None",
221 "None",
222 "None",
223 "None",
224 "None",
225 "None .. None",
226 ),
227 )
228 )
229 expected = AstropyTable(rows, names=self.expectedColumnNames)
230 self.assertAstropyTablesEqual(readTable(result.output), expected)
232 # verify getting records from the "foo" collection
233 result = self.runner.invoke(
234 butlerCli,
235 [
236 "query-dimension-records",
237 self.root,
238 "visit",
239 "--collections",
240 "foo",
241 "--datasets",
242 "test_metric_comp",
243 ],
244 )
245 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
246 rows = array(
247 (
248 (
249 "DummyCamComp",
250 "425",
251 "d-r",
252 "fourtwentyfive",
253 "None",
254 "None",
255 "None",
256 "None",
257 "None",
258 "None",
259 "None",
260 "None",
261 "None",
262 "None .. None",
263 ),
264 )
265 )
266 expected = AstropyTable(rows, names=self.expectedColumnNames)
267 self.assertAstropyTablesEqual(readTable(result.output), expected)
269 def testSkymap(self):
270 butler = Butler(self.root, run="foo")
271 # try replacing the testRepo's butler with the one with the "foo" run.
272 self.testRepo.butler = butler
274 skymapRecord = {"name": "example_skymap", "hash": (50).to_bytes(8, byteorder="little")}
275 self.testRepo.butler.registry.insertDimensionData("skymap", skymapRecord)
277 result = self.runner.invoke(butlerCli, ["query-dimension-records", self.root, "skymap"])
278 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
280 rows = array(("example_skymap", "0x3200000000000000", "None", "None", "None"))
281 expected = AstropyTable(rows, names=["name", "hash", "tract_max", "patch_nx_max", "patch_ny_max"])
282 self.assertAstropyTablesEqual(readTable(result.output), expected)
285if __name__ == "__main__":
286 unittest.main()