Coverage for tests/test_cliCmdQueryDataIds.py: 26%
76 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-28 10:10 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-28 10:10 +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 os
26import unittest
28from astropy.table import Table as AstropyTable
29from lsst.daf.butler import Butler, DatasetType, script
30from lsst.daf.butler.tests.utils import ButlerTestHelper, makeTestTempDir, removeTestTempDir
31from lsst.daf.butler.transfers import YamlRepoImportBackend
32from numpy import array
34TESTDIR = os.path.abspath(os.path.dirname(__file__))
37class QueryDataIdsTest(unittest.TestCase, ButlerTestHelper):
38 """Test the query-data-ids command-line."""
40 mockFuncName = "lsst.daf.butler.cli.cmd.commands.script.queryDataIds"
42 @staticmethod
43 def _queryDataIds(repo, dimensions=(), collections=(), datasets=None, where=""):
44 """Call script.queryDataIds, allowing for default values."""
45 return script.queryDataIds(
46 repo=repo,
47 dimensions=dimensions,
48 collections=collections,
49 datasets=datasets,
50 where=where,
51 order_by=None,
52 limit=0,
53 offset=0,
54 )
56 def setUp(self):
57 self.root = makeTestTempDir(TESTDIR)
58 self.repo = Butler.makeRepo(self.root)
60 def tearDown(self):
61 removeTestTempDir(self.root)
63 def loadData(self, *filenames: str) -> Butler:
64 """Load registry test data from ``TESTDIR/data/registry/<filename>``,
65 which should be a YAML import/export file.
66 """
67 butler = Butler(self.repo, writeable=True)
68 for filename in filenames:
69 with open(os.path.join(TESTDIR, "data", "registry", filename)) as stream:
70 # Go behind the back of the import code a bit to deal with
71 # the fact that this is just registry content with no actual
72 # files for the datastore.
73 backend = YamlRepoImportBackend(stream, butler.registry)
74 backend.register()
75 backend.load(datastore=None)
76 return butler
78 def testDimensions(self):
79 """Test getting a dimension."""
80 self.loadData("base.yaml")
81 res, msg = self._queryDataIds(self.root, dimensions=("detector",))
82 expected = AstropyTable(
83 array((("Cam1", 1), ("Cam1", 2), ("Cam1", 3), ("Cam1", 4))), names=("instrument", "detector")
84 )
85 self.assertFalse(msg)
86 self.assertAstropyTablesEqual(res, expected)
88 def testNoDimensions(self):
89 """Test asking for no dimensions."""
90 res, msg = self._queryDataIds(self.root)
91 self.assertIsNone(res, msg)
92 self.assertEqual(
93 msg, "Result has one logical row but no columns because no dimensions were requested."
94 )
96 def testNoResultsEasy(self):
97 """Test getting no results in a way that's detectable without having
98 to execute the full query.
99 """
100 self.loadData("base.yaml", "spatial.yaml")
101 res, msg = self._queryDataIds(
102 self.root,
103 dimensions=("visit", "tract"),
104 where="instrument='Cam1' AND skymap='SkyMap1' AND visit=1 AND tract=1",
105 )
106 self.assertIsNone(res, msg)
107 self.assertIn("yields no results when applied to", msg)
109 def testNoResultsHard(self):
110 """Test getting no results in a way that can't be detected unless we
111 run the whole query.
112 """
113 self.loadData("base.yaml", "spatial.yaml")
114 res, msg = self._queryDataIds(
115 self.root,
116 dimensions=("visit", "tract"),
117 where="instrument='Cam1' AND skymap='SkyMap1' AND visit=1 AND tract=0 AND patch=5",
118 )
119 self.assertIsNone(res, msg)
120 self.assertIn("Post-query region filtering removed all rows", msg)
122 def testWhere(self):
123 """Test with a WHERE constraint."""
124 self.loadData("base.yaml")
125 res, msg = self._queryDataIds(
126 self.root, dimensions=("detector",), where="instrument='Cam1' AND detector=2"
127 )
128 expected = AstropyTable(
129 array((("Cam1", 2),)),
130 names=(
131 "instrument",
132 "detector",
133 ),
134 )
135 self.assertAstropyTablesEqual(res, expected)
136 self.assertIsNone(msg)
138 def testDatasetsAndCollections(self):
139 """Test constraining via datasets and collections."""
140 butler = self.loadData("base.yaml", "datasets-uuid.yaml")
141 # See that the data IDs returned are constrained by that collection's
142 # contents.
143 res, msg = self._queryDataIds(
144 repo=self.root, dimensions=("detector",), collections=("imported_g",), datasets="bias"
145 )
146 expected = AstropyTable(
147 array((("Cam1", 1), ("Cam1", 2), ("Cam1", 3))),
148 names=(
149 "instrument",
150 "detector",
151 ),
152 )
153 self.assertAstropyTablesEqual(res, expected)
154 self.assertIsNone(msg)
156 # Check that the dimensions are inferred when not provided.
157 with self.assertLogs("lsst.daf.butler.script.queryDataIds", "INFO") as cm:
158 res, msg = self._queryDataIds(repo=self.root, collections=("imported_g",), datasets="bias")
159 self.assertIn("Determined dimensions", "\n".join(cm.output))
160 self.assertAstropyTablesEqual(res, expected)
161 self.assertIsNone(msg)
163 # Check that we get a reason if no dimensions can be inferred.
164 new_dataset_type = DatasetType(
165 "test_metric_dimensionless",
166 (),
167 "StructuredDataDict",
168 universe=butler.dimensions,
169 )
170 butler.registry.registerDatasetType(new_dataset_type)
171 res, msg = self._queryDataIds(repo=self.root, collections=("imported_g",), datasets=...)
172 self.assertIsNone(res)
173 self.assertIn("No dimensions in common", msg)
175 # Check that we get a reason returned if no dataset type is found.
176 with self.assertWarns(FutureWarning):
177 res, msg = self._queryDataIds(
178 repo=self.root, dimensions=("detector",), collections=("imported_g",), datasets="raw"
179 )
180 self.assertIsNone(res)
181 self.assertEqual(msg, "Dataset type raw is not registered.")
183 # Check that we get a reason returned if no dataset is found in
184 # collection.
185 res, msg = self._queryDataIds(
186 repo=self.root,
187 dimensions=("detector",),
188 collections=("imported_g",),
189 datasets="test_metric_dimensionless",
190 )
191 self.assertIsNone(res)
192 self.assertIn("No datasets of type test_metric_dimensionless", msg)
195if __name__ == "__main__":
196 unittest.main()