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