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