Coverage for tests/test_testRepo.py: 21%
117 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-01-10 02:33 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2023-01-10 02:33 -0800
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 `lsst.daf.butler.tests.testRepo`, a module for creating
23test repositories or butlers.
24"""
26import os
27import shutil
28import unittest
30import lsst.daf.butler
31from lsst.daf.butler.tests import (
32 MetricsExample,
33 addDataIdValue,
34 addDatasetType,
35 expandUniqueId,
36 makeTestCollection,
37 makeTestRepo,
38 registerMetricsExample,
39)
40from lsst.daf.butler.tests.utils import makeTestTempDir, removeTestTempDir, safeTestTempDir
42TESTDIR = os.path.abspath(os.path.dirname(__file__))
45class ButlerTestRepoTestCase(unittest.TestCase):
46 """Simpler test than below without setUpClass getting in the way."""
48 def setUp(self):
49 self.root = makeTestTempDir(TESTDIR)
51 def tearDown(self):
52 removeTestTempDir(self.root)
54 def testMakeTestRepo(self):
56 dataIds = {
57 "instrument": ["DummyCam"],
58 "physical_filter": ["d-r"],
59 "exposure": [42, 43, 44],
60 "visit": [42, 43, 44],
61 }
63 butler = makeTestRepo(self.root, dataIds)
65 records = list(butler.registry.queryDimensionRecords("visit"))
66 self.assertEqual(len(records), 3)
69class ButlerUtilsTestSuite(unittest.TestCase):
70 @classmethod
71 def setUpClass(cls):
72 # Repository should be re-created for each test case, but
73 # this has a prohibitive run-time cost at present
74 cls.root = makeTestTempDir(TESTDIR)
76 cls.creatorButler = makeTestRepo(cls.root)
77 addDataIdValue(cls.creatorButler, "instrument", "notACam")
78 addDataIdValue(cls.creatorButler, "instrument", "dummyCam")
79 addDataIdValue(cls.creatorButler, "physical_filter", "k2020", band="k", instrument="notACam")
80 addDataIdValue(cls.creatorButler, "physical_filter", "l2019", instrument="dummyCam")
81 addDataIdValue(cls.creatorButler, "visit", 101, instrument="notACam", physical_filter="k2020")
82 addDataIdValue(cls.creatorButler, "visit", 102, instrument="notACam", physical_filter="k2020")
83 addDataIdValue(cls.creatorButler, "detector", 5)
84 # Leave skymap/patch/tract undefined so that tests can assume
85 # they're missing.
87 registerMetricsExample(cls.creatorButler)
88 addDatasetType(cls.creatorButler, "DataType1", {"instrument"}, "StructuredDataNoComponents")
89 addDatasetType(cls.creatorButler, "DataType2", {"instrument", "visit"}, "StructuredData")
91 @classmethod
92 def tearDownClass(cls):
93 # TODO: use addClassCleanup rather than tearDownClass in Python 3.8
94 # to keep the addition and removal together and make it more robust
95 removeTestTempDir(cls.root)
97 def setUp(self):
98 # TestCase.id() is unique for each test method
99 self.butler = makeTestCollection(self.creatorButler, uniqueId=self.id())
101 def testButlerValid(self):
102 self.butler.validateConfiguration()
104 def testButlerKwargs(self):
105 # outfile has the most obvious effects of any Butler.makeRepo keyword
106 with safeTestTempDir(TESTDIR) as temp:
107 path = os.path.join(temp, "oddConfig.json")
108 makeTestRepo(temp, {}, outfile=path)
109 self.assertTrue(os.path.isfile(path))
111 def _checkButlerDimension(self, dimensions, query, expected):
112 result = list(self.butler.registry.queryDataIds(dimensions, where=query, check=False))
113 self.assertEqual(len(result), 1)
114 self.assertIn(result[0].byName(), expected)
116 def testButlerDimensions(self):
117 self._checkButlerDimension(
118 {"instrument"}, "instrument='notACam'", [{"instrument": "notACam"}, {"instrument": "dummyCam"}]
119 )
120 self._checkButlerDimension(
121 {"visit", "instrument"},
122 "visit=101",
123 [{"instrument": "notACam", "visit": 101}, {"instrument": "dummyCam", "visit": 101}],
124 )
125 self._checkButlerDimension(
126 {"visit", "instrument"},
127 "visit=102",
128 [{"instrument": "notACam", "visit": 102}, {"instrument": "dummyCam", "visit": 102}],
129 )
130 self._checkButlerDimension(
131 {"detector", "instrument"},
132 "detector=5",
133 [{"instrument": "notACam", "detector": 5}, {"instrument": "dummyCam", "detector": 5}],
134 )
136 def testAddDataIdValue(self):
137 addDataIdValue(self.butler, "visit", 1, instrument="notACam", physical_filter="k2020")
138 self._checkButlerDimension(
139 {"visit", "instrument"}, "visit=1", [{"instrument": "notACam", "visit": 1}]
140 )
141 addDataIdValue(self.butler, "visit", 2, instrument="dummyCam", physical_filter="l2019")
142 self._checkButlerDimension(
143 {"visit", "instrument"}, "visit=2", [{"instrument": "dummyCam", "visit": 2}]
144 )
146 with self.assertRaises(ValueError):
147 addDataIdValue(self.butler, "NotADimension", 42)
148 with self.assertRaises(ValueError):
149 addDataIdValue(self.butler, "detector", "nonNumeric")
150 with self.assertRaises(ValueError):
151 addDataIdValue(self.butler, "detector", 101, nonsenseField="string")
153 # Keywords imply different instruments
154 with self.assertRaises(RuntimeError):
155 addDataIdValue(self.butler, "exposure", 101, instrument="dummyCam", physical_filter="k2020")
157 # No skymap defined
158 with self.assertRaises(RuntimeError):
159 addDataIdValue(self.butler, "tract", 42)
160 # Didn't create skymap "map" first.
161 with self.assertRaises(RuntimeError):
162 addDataIdValue(self.butler, "tract", 43, skymap="map")
164 def testAddDatasetType(self):
165 # 1 for StructuredDataNoComponents, 1 for StructuredData (components
166 # not included).
167 self.assertEqual(len(list(self.butler.registry.queryDatasetTypes(components=False))), 2)
169 # Testing the DatasetType objects is not practical, because all tests
170 # need a DimensionUniverse. So just check that we have the dataset
171 # types we expect.
172 self.butler.registry.getDatasetType("DataType1")
173 self.butler.registry.getDatasetType("DataType2")
175 with self.assertRaises(ValueError):
176 addDatasetType(self.butler, "DataType3", {"4thDimension"}, "NumpyArray")
177 with self.assertRaises(ValueError):
178 addDatasetType(self.butler, "DataType3", {"instrument"}, "UnstorableType")
180 def testRegisterMetricsExample(self):
181 id1 = {"instrument": "notACam"}
182 id2 = expandUniqueId(self.butler, {"visit": 101})
183 data = MetricsExample(summary={"answer": 42, "question": "unknown"})
185 self.butler.put(data, "DataType1", id1)
186 self.assertEqual(self.butler.get("DataType1", id1), data)
188 self.butler.put(data, "DataType2", id2)
189 self.assertEqual(self.butler.get("DataType2", id2), data)
190 self.assertEqual(self.butler.get("DataType2.summary", id2), data.summary)
192 def testRegisterMetricsExampleChained(self):
193 """Regression test for registerMetricsExample having no effect
194 on ChainedDatastore.
195 """
196 temp = makeTestTempDir(TESTDIR)
197 try:
198 config = lsst.daf.butler.Config()
199 config["datastore", "cls"] = "lsst.daf.butler.datastores.chainedDatastore.ChainedDatastore"
200 config["datastore", "datastores"] = [
201 {
202 "cls": "lsst.daf.butler.datastores.fileDatastore.FileDatastore",
203 }
204 ]
206 repo = lsst.daf.butler.Butler.makeRepo(temp, config=config)
207 butler = lsst.daf.butler.Butler(repo, run="chainedExample")
208 registerMetricsExample(butler)
209 addDatasetType(butler, "DummyType", {}, "StructuredDataNoComponents")
211 data = MetricsExample(summary={})
212 # Should not raise
213 butler.put(data, "DummyType")
214 finally:
215 shutil.rmtree(temp, ignore_errors=True)
217 def testUniqueButler(self):
218 dataId = {"instrument": "notACam"}
219 self.butler.put(MetricsExample({"answer": 42, "question": "unknown"}), "DataType1", dataId)
220 self.assertTrue(self.butler.datasetExists("DataType1", dataId))
222 newButler = makeTestCollection(self.creatorButler)
223 with self.assertRaises(LookupError):
224 newButler.datasetExists("DataType1", dataId)
226 def testExpandUniqueId(self):
227 self.assertEqual(
228 dict(expandUniqueId(self.butler, {"instrument": "notACam"})), {"instrument": "notACam"}
229 )
230 self.assertIn(
231 dict(expandUniqueId(self.butler, {"visit": 101})),
232 [{"instrument": "notACam", "visit": 101}, {"instrument": "dummyCam", "visit": 101}],
233 )
234 self.assertIn(
235 dict(expandUniqueId(self.butler, {"detector": 5})),
236 [{"instrument": "notACam", "detector": 5}, {"instrument": "dummyCam", "detector": 5}],
237 )
238 self.assertIn(
239 dict(expandUniqueId(self.butler, {"physical_filter": "k2020"})),
240 [
241 {"instrument": "notACam", "physical_filter": "k2020"},
242 {"instrument": "notACam", "physical_filter": "k2020"},
243 ],
244 )
245 with self.assertRaises(ValueError):
246 expandUniqueId(self.butler, {"tract": 42})
249if __name__ == "__main__": 249 ↛ 250line 249 didn't jump to line 250, because the condition on line 249 was never true
250 unittest.main()