Coverage for tests/test_storageClass.py: 13%
180 statements
« prev ^ index » next coverage.py v6.4.1, created at 2022-06-09 02:25 -0700
« prev ^ index » next coverage.py v6.4.1, created at 2022-06-09 02:25 -0700
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/>.
22import logging
23import os
24import pickle
25import unittest
27from lsst.daf.butler import StorageClass, StorageClassConfig, StorageClassDelegate, StorageClassFactory
28from lsst.daf.butler.tests import MetricsExample
29from lsst.utils.introspection import get_full_type_name
31"""Tests related to the StorageClass infrastructure.
32"""
34TESTDIR = os.path.abspath(os.path.dirname(__file__))
37class PythonType:
38 """A dummy class to test the registry of Python types."""
40 pass
43class StorageClassFactoryTestCase(unittest.TestCase):
44 """Tests of the storage class infrastructure."""
46 def testCreation(self):
47 """Test that we can dynamically create storage class subclasses.
49 This is critical for testing the factory functions."""
50 className = "TestImage"
51 sc = StorageClass(className, pytype=dict)
52 self.assertIsInstance(sc, StorageClass)
53 self.assertEqual(sc.name, className)
54 self.assertEqual(str(sc), className)
55 self.assertFalse(sc.components)
56 self.assertTrue(sc.validateInstance({}))
57 self.assertFalse(sc.validateInstance(""))
59 r = repr(sc)
60 self.assertIn("StorageClass", r)
61 self.assertIn(className, r)
62 self.assertNotIn("parameters", r)
63 self.assertIn("pytype='dict'", r)
65 # Ensure we do not have a delegate
66 with self.assertRaises(TypeError):
67 sc.delegate()
69 # Allow no definition of python type
70 scn = StorageClass(className)
71 self.assertIs(scn.pytype, object)
73 # Include some components
74 scc = StorageClass(className, pytype=PythonType, components={"comp1": sc, "comp2": sc})
75 self.assertIn("comp1", scc.components)
76 r = repr(scc)
77 self.assertIn("comp1", r)
78 self.assertIn("lsst.daf.butler.core.storageClassDelegate.StorageClassDelegate", r)
80 # Ensure that we have a delegate
81 self.assertIsInstance(scc.delegate(), StorageClassDelegate)
83 # Check we can create a storageClass using the name of an importable
84 # type.
85 sc2 = StorageClass("TestImage2", "lsst.daf.butler.core.storageClass.StorageClassFactory")
86 self.assertIsInstance(sc2.pytype(), StorageClassFactory)
87 self.assertIn("butler.core", repr(sc2))
89 def testParameters(self):
90 """Test that we can set parameters and validate them"""
91 pt = ("a", "b")
92 ps = {"a", "b"}
93 pl = ["a", "b"]
94 for p in (pt, ps, pl):
95 sc1 = StorageClass("ParamClass", pytype=dict, parameters=p)
96 self.assertEqual(sc1.parameters, ps)
97 sc1.validateParameters(p)
99 sc1.validateParameters()
100 sc1.validateParameters({"a": None, "b": None})
101 sc1.validateParameters(
102 [
103 "a",
104 ]
105 )
106 with self.assertRaises(KeyError):
107 sc1.validateParameters({"a", "c"})
109 def testEquality(self):
110 """Test that StorageClass equality works"""
111 className = "TestImage"
112 sc1 = StorageClass(className, pytype=dict)
113 sc2 = StorageClass(className, pytype=dict)
114 self.assertEqual(sc1, sc2)
115 sc3 = StorageClass(className + "2", pytype=str)
116 self.assertNotEqual(sc1, sc3)
118 # Same StorageClass name but different python type
119 sc4 = StorageClass(className, pytype=str)
120 self.assertNotEqual(sc1, sc4)
122 # Parameters
123 scp = StorageClass("Params", pytype=PythonType, parameters=["a", "b", "c"])
124 scp1 = StorageClass("Params", pytype=PythonType, parameters=["a", "b", "c"])
125 scp2 = StorageClass("Params", pytype=PythonType, parameters=["a", "b", "d", "e"])
126 self.assertEqual(scp, scp1)
127 self.assertNotEqual(scp, scp2)
129 # Now with components
130 sc5 = StorageClass("Composite", pytype=PythonType, components={"comp1": sc1, "comp2": sc3})
131 sc6 = StorageClass("Composite", pytype=PythonType, components={"comp1": sc1, "comp2": sc3})
132 self.assertEqual(sc5, sc6)
133 self.assertNotEqual(sc5, sc3)
134 sc7 = StorageClass("Composite", pytype=PythonType, components={"comp1": sc4, "comp2": sc3})
135 self.assertNotEqual(sc5, sc7)
136 sc8 = StorageClass("Composite", pytype=PythonType, components={"comp2": sc3, "comp3": sc3})
137 self.assertNotEqual(sc5, sc8)
138 sc9 = StorageClass(
139 "Composite",
140 pytype=PythonType,
141 components={"comp1": sc1, "comp2": sc3},
142 delegate="lsst.daf.butler.Butler",
143 )
144 self.assertNotEqual(sc5, sc9)
146 def testRegistry(self):
147 """Check that storage classes can be created on the fly and stored
148 in a registry."""
149 className = "TestImage"
150 factory = StorageClassFactory()
151 newclass = StorageClass(className, pytype=PythonType)
152 factory.registerStorageClass(newclass)
153 sc = factory.getStorageClass(className)
154 self.assertIsInstance(sc, StorageClass)
155 self.assertEqual(sc.name, className)
156 self.assertFalse(sc.components)
157 self.assertEqual(sc.pytype, PythonType)
158 self.assertIn(sc, factory)
159 newclass2 = StorageClass("Temporary2", pytype=str)
160 self.assertNotIn(newclass2, factory)
161 factory.registerStorageClass(newclass2)
162 self.assertIn(newclass2, factory)
163 self.assertIn("Temporary2", factory)
164 self.assertNotIn("Temporary3", factory)
165 self.assertNotIn({}, factory)
167 # Make sure we can't register a storage class with the same name
168 # but different values
169 newclass3 = StorageClass("Temporary2", pytype=dict)
170 with self.assertRaises(ValueError):
171 factory.registerStorageClass(newclass3)
173 factory._unregisterStorageClass(newclass3.name)
174 self.assertNotIn(newclass3, factory)
175 self.assertNotIn(newclass3.name, factory)
176 factory.registerStorageClass(newclass3)
177 self.assertIn(newclass3, factory)
178 self.assertIn(newclass3.name, factory)
180 # Check you can silently insert something that is already there
181 factory.registerStorageClass(newclass3)
183 def testFactoryConfig(self):
184 factory = StorageClassFactory()
185 factory.addFromConfig(StorageClassConfig())
186 image = factory.getStorageClass("Image")
187 imageF = factory.getStorageClass("ImageF")
188 self.assertIsInstance(imageF, type(image))
189 self.assertNotEqual(imageF, image)
191 # Check component inheritance
192 exposure = factory.getStorageClass("Exposure")
193 exposureF = factory.getStorageClass("ExposureF")
194 self.assertIsInstance(exposureF, type(exposure))
195 self.assertIsInstance(exposure.components["image"], type(image))
196 self.assertNotIsInstance(exposure.components["image"], type(imageF))
197 self.assertIsInstance(exposureF.components["image"], type(image))
198 self.assertIsInstance(exposureF.components["image"], type(imageF))
199 self.assertIn("wcs", exposure.components)
200 self.assertIn("wcs", exposureF.components)
202 # Check parameters
203 factory.addFromConfig(os.path.join(TESTDIR, "config", "basic", "storageClasses.yaml"))
204 thing1 = factory.getStorageClass("ThingOne")
205 thing2 = factory.getStorageClass("ThingTwo")
206 self.assertIsInstance(thing2, type(thing1))
207 param1 = thing1.parameters
208 param2 = thing2.parameters
209 self.assertIn("param3", thing2.parameters)
210 self.assertNotIn("param3", thing1.parameters)
211 param2.remove("param3")
212 self.assertEqual(param1, param2)
214 # Check that we can't have a new StorageClass that does not
215 # inherit from StorageClass
216 with self.assertRaises(ValueError):
217 factory.makeNewStorageClass("ClassName", baseClass=StorageClassFactory)
219 sc = factory.makeNewStorageClass("ClassName")
220 self.assertIsInstance(sc(), StorageClass)
222 def testPickle(self):
223 """Test that we can pickle storageclasses."""
224 className = "TestImage"
225 sc = StorageClass(className, pytype=dict)
226 self.assertIsInstance(sc, StorageClass)
227 self.assertEqual(sc.name, className)
228 self.assertFalse(sc.components)
229 sc2 = pickle.loads(pickle.dumps(sc))
230 self.assertEqual(sc2, sc)
232 @classmethod
233 def _convert_type(cls, data):
234 # Test helper function. Fail if the list is empty.
235 if not len(data):
236 raise RuntimeError("Deliberate failure.")
237 return {"key": data}
239 def testConverters(self):
240 """Test conversion maps."""
242 className = "TestConverters"
243 converters = {
244 "lsst.daf.butler.tests.MetricsExample": "lsst.daf.butler.tests.MetricsExample.exportAsDict",
245 # Add some entries that will fail to import.
246 "lsst.daf.butler.bad.type": "lsst.daf.butler.tests.MetricsExampleModel.from_metrics",
247 "lsst.daf.butler.tests.MetricsExampleModel": "lsst.daf.butler.bad.function",
248 "lsst.daf.butler.Butler": "lsst.daf.butler.core.location.__all__",
249 "list": get_full_type_name(self._convert_type),
250 }
251 sc = StorageClass(className, pytype=dict, converters=converters)
252 self.assertEqual(len(sc.converters), 5) # Pre-filtering
253 sc2 = StorageClass("Test2", pytype=set)
254 sc3 = StorageClass("Test3", pytype="lsst.daf.butler.tests.MetricsExample")
256 self.assertIn("lsst.daf.butler.tests.MetricsExample", repr(sc))
257 # Initially the converter list is not filtered.
258 self.assertIn("lsst.daf.butler.bad.type", repr(sc))
259 self.assertNotIn("converters", repr(sc2))
261 self.assertTrue(sc.can_convert(sc))
262 self.assertFalse(sc.can_convert(sc2))
263 self.assertTrue(sc.can_convert(sc3))
265 # After we've processed the converters the bad ones will no longer
266 # be reported.
267 self.assertNotIn("lsst.daf.butler.bad.type", repr(sc))
268 self.assertEqual(len(sc.converters), 2)
270 self.assertIsNone(sc.coerce_type(None))
272 converted = sc.coerce_type([1, 2, 3])
273 self.assertEqual(converted, {"key": [1, 2, 3]})
275 # Convert Metrics using a named method converter.
276 metric = MetricsExample(summary={"a": 1}, data=[1, 2], output={"c": "e"})
277 converted = sc.coerce_type(metric)
278 self.assertEqual(converted["data"], [1, 2], converted)
280 # Check that python types matching is allowed.
281 sc4 = StorageClass("Test2", pytype=set)
282 self.assertTrue(sc2.can_convert(sc4))
283 converted = sc2.coerce_type({1, 2})
284 self.assertEqual(converted, {1, 2})
286 # Try to coerce a type that is not supported.
287 with self.assertRaises(TypeError):
288 sc.coerce_type(set([1, 2, 3]))
290 # Coerce something that will fail to convert.
291 with self.assertLogs(level=logging.ERROR) as cm:
292 with self.assertRaises(RuntimeError):
293 sc.coerce_type([])
294 self.assertIn("failed to convert type list", cm.output[0])
297if __name__ == "__main__": 297 ↛ 298line 297 didn't jump to line 298, because the condition on line 297 was never true
298 unittest.main()