Coverage for tests/test_cliCmdQueryDimensionRecords.py: 46%
54 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-12-01 19:55 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2022-12-01 19:55 +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 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 astropy
26from astropy.table import Table as AstropyTable
27from astropy.utils.introspection import minversion
28from numpy import array
29import os
30import unittest
32from lsst.daf.butler import StorageClassFactory
33from lsst.daf.butler import Butler
34from lsst.daf.butler.cli.butler import cli as butlerCli
35from lsst.daf.butler.cli.utils import clickResultMsg, LogCliRunner
36from lsst.daf.butler.tests.utils import (
37 ButlerTestHelper,
38 makeTestTempDir,
39 MetricTestRepo,
40 readTable,
41 removeTestTempDir,
42)
45TESTDIR = os.path.abspath(os.path.dirname(__file__))
47# Astropy changed the handling of numpy columns in v5.1 so anything
48# greater than 5.0 (two digit version) does not need the annotated column.
49timespan_columns = "" if minversion(astropy, "5.1") else " [2]"
52class QueryDimensionRecordsTest(unittest.TestCase, ButlerTestHelper):
54 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.queryDimensionRecords"
56 configFile = os.path.join(TESTDIR, "config/basic/butler.yaml")
57 storageClassFactory = StorageClassFactory()
59 expectedColumnNames = ("instrument", "id", "physical_filter", "visit_system", "name", "day_obs",
60 "exposure_time", "target_name", "observation_reason", "science_program",
61 "zenith_angle", "region", f"timespan{timespan_columns}")
63 def setUp(self):
64 self.root = makeTestTempDir(TESTDIR)
65 self.testRepo = MetricTestRepo(self.root,
66 configFile=os.path.join(TESTDIR, "config/basic/butler.yaml"))
67 self.runner = LogCliRunner()
69 def tearDown(self):
70 removeTestTempDir(self.root)
72 def testBasic(self):
73 result = self.runner.invoke(butlerCli, ["query-dimension-records",
74 self.root,
75 "visit"])
76 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
77 rows = array((
78 ("DummyCamComp", "423", "d-r", "1", "fourtwentythree", "None", "None", "None", "None", "None",
79 "None", "None", "None .. None"),
80 ("DummyCamComp", "424", "d-r", "1", "fourtwentyfour", "None", "None", "None", "None", "None",
81 "None", "None", "None .. None")
82 ))
83 expected = AstropyTable(rows, names=self.expectedColumnNames)
84 self.assertAstropyTablesEqual(readTable(result.output), expected)
86 def testWhere(self):
87 result = self.runner.invoke(butlerCli, ["query-dimension-records",
88 self.root,
89 "visit",
90 "--where",
91 "instrument='DummyCamComp' AND visit.name='fourtwentythree'"])
92 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
93 rows = array((
94 ("DummyCamComp", "423", "d-r", "1", "fourtwentythree", "None", "None", "None", "None", "None",
95 "None", "None", "None .. None"),
96 ))
97 expected = AstropyTable(rows, names=self.expectedColumnNames)
98 self.assertAstropyTablesEqual(readTable(result.output), expected)
100 def testCollection(self):
102 butler = Butler(self.root, run="foo")
104 # try replacing the testRepo's butler with the one with the "foo" run.
105 self.testRepo.butler = butler
107 self.testRepo.butler.registry.insertDimensionData("visit", {"instrument": "DummyCamComp", "id": 425,
108 "name": "fourtwentyfive",
109 "physical_filter": "d-r",
110 "visit_system": 1})
111 self.testRepo.addDataset(dataId={"instrument": "DummyCamComp", "visit": 425},
112 run="foo")
114 # verify getting records from the "ingest/run" collection
115 result = self.runner.invoke(butlerCli, ["query-dimension-records",
116 self.root,
117 "visit",
118 "--collections", "ingest/run",
119 "--datasets", "test_metric_comp"
120 ])
121 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
122 rows = array((
123 ("DummyCamComp", "423", "d-r", "1", "fourtwentythree", "None", "None", "None", "None", "None",
124 "None", "None", "None .. None"),
125 ("DummyCamComp", "424", "d-r", "1", "fourtwentyfour", "None", "None", "None", "None", "None",
126 "None", "None", "None .. None")
127 ))
128 expected = AstropyTable(rows, names=self.expectedColumnNames)
129 self.assertAstropyTablesEqual(readTable(result.output), expected)
131 # verify getting records from the "foo" collection
132 result = self.runner.invoke(butlerCli, ["query-dimension-records",
133 self.root,
134 "visit",
135 "--collections", "foo",
136 "--datasets", "test_metric_comp"
137 ])
138 self.assertEqual(result.exit_code, 0, clickResultMsg(result))
139 rows = array((
140 ("DummyCamComp", "425", "d-r", "1", "fourtwentyfive", "None", "None", "None", "None", "None",
141 "None", "None", "None .. None"),
142 ))
143 expected = AstropyTable(rows, names=self.expectedColumnNames)
144 self.assertAstropyTablesEqual(readTable(result.output), expected)
147if __name__ == "__main__": 147 ↛ 148line 147 didn't jump to line 148, because the condition on line 147 was never true
148 unittest.main()