Coverage for tests/test_cliCmdQueryDimensionRecords.py: 44%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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
28from astropy.table import Table as AstropyTable
29from lsst.daf.butler import Butler, StorageClassFactory
30from lsst.daf.butler.cli.butler import cli as butlerCli
31from lsst.daf.butler.cli.utils import LogCliRunner, clickResultMsg
32from lsst.daf.butler.tests.utils import (
33 ButlerTestHelper,
34 MetricTestRepo,
35 makeTestTempDir,
36 readTable,
37 removeTestTempDir,
38)
39from numpy import array
41TESTDIR = os.path.abspath(os.path.dirname(__file__))
44class QueryDimensionRecordsTest(unittest.TestCase, ButlerTestHelper):
46 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.queryDimensionRecords"
48 configFile = os.path.join(TESTDIR, "config/basic/butler.yaml")
49 storageClassFactory = StorageClassFactory()
51 expectedColumnNames = (
52 "instrument",
53 "id",
54 "physical_filter",
55 "visit_system",
56 "name",
57 "day_obs",
58 "exposure_time",
59 "target_name",
60 "observation_reason",
61 "science_program",
62 "zenith_angle",
63 "region",
64 "timespan [2]",
65 )
67 def setUp(self):
68 self.root = makeTestTempDir(TESTDIR)
69 self.testRepo = MetricTestRepo(
70 self.root, configFile=os.path.join(TESTDIR, "config/basic/butler.yaml")
71 )
72 self.runner = LogCliRunner()
74 def tearDown(self):
75 removeTestTempDir(self.root)
77 def testBasic(self):
78 result = self.runner.invoke(butlerCli, ["query-dimension-records", self.root, "visit"])
79 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
80 rows = array(
81 (
82 (
83 "DummyCamComp",
84 "423",
85 "d-r",
86 "1",
87 "fourtwentythree",
88 "None",
89 "None",
90 "None",
91 "None",
92 "None",
93 "None",
94 "None",
95 "None .. None",
96 ),
97 (
98 "DummyCamComp",
99 "424",
100 "d-r",
101 "1",
102 "fourtwentyfour",
103 "None",
104 "None",
105 "None",
106 "None",
107 "None",
108 "None",
109 "None",
110 "None .. None",
111 ),
112 )
113 )
114 expected = AstropyTable(rows, names=self.expectedColumnNames)
115 self.assertAstropyTablesEqual(readTable(result.output), expected)
117 def testWhere(self):
118 result = self.runner.invoke(
119 butlerCli,
120 [
121 "query-dimension-records",
122 self.root,
123 "visit",
124 "--where",
125 "instrument='DummyCamComp' AND visit.name='fourtwentythree'",
126 ],
127 )
128 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
129 rows = array(
130 (
131 (
132 "DummyCamComp",
133 "423",
134 "d-r",
135 "1",
136 "fourtwentythree",
137 "None",
138 "None",
139 "None",
140 "None",
141 "None",
142 "None",
143 "None",
144 "None .. None",
145 ),
146 )
147 )
148 expected = AstropyTable(rows, names=self.expectedColumnNames)
149 self.assertAstropyTablesEqual(readTable(result.output), expected)
151 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 "visit_system": 1,
166 },
167 )
168 self.testRepo.addDataset(dataId={"instrument": "DummyCamComp", "visit": 425}, run="foo")
170 # verify getting records from the "ingest/run" collection
171 result = self.runner.invoke(
172 butlerCli,
173 [
174 "query-dimension-records",
175 self.root,
176 "visit",
177 "--collections",
178 "ingest/run",
179 "--datasets",
180 "test_metric_comp",
181 ],
182 )
183 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
184 rows = array(
185 (
186 (
187 "DummyCamComp",
188 "423",
189 "d-r",
190 "1",
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 "1",
206 "fourtwentyfour",
207 "None",
208 "None",
209 "None",
210 "None",
211 "None",
212 "None",
213 "None",
214 "None .. None",
215 ),
216 )
217 )
218 expected = AstropyTable(rows, names=self.expectedColumnNames)
219 self.assertAstropyTablesEqual(readTable(result.output), expected)
221 # verify getting records from the "foo" collection
222 result = self.runner.invoke(
223 butlerCli,
224 [
225 "query-dimension-records",
226 self.root,
227 "visit",
228 "--collections",
229 "foo",
230 "--datasets",
231 "test_metric_comp",
232 ],
233 )
234 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
235 rows = array(
236 (
237 (
238 "DummyCamComp",
239 "425",
240 "d-r",
241 "1",
242 "fourtwentyfive",
243 "None",
244 "None",
245 "None",
246 "None",
247 "None",
248 "None",
249 "None",
250 "None .. None",
251 ),
252 )
253 )
254 expected = AstropyTable(rows, names=self.expectedColumnNames)
255 self.assertAstropyTablesEqual(readTable(result.output), expected)
258if __name__ == "__main__": 258 ↛ 259line 258 didn't jump to line 259, because the condition on line 258 was never true
259 unittest.main()