Coverage for tests/test_storageClass.py : 11%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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 os
23import pickle
24import unittest
26from lsst.daf.butler import StorageClass, StorageClassFactory, StorageClassConfig, CompositeAssembler
28"""Tests related to the StorageClass infrastructure.
29"""
31TESTDIR = os.path.abspath(os.path.dirname(__file__))
34class PythonType:
35 """A dummy class to test the registry of Python types."""
36 pass
39class StorageClassFactoryTestCase(unittest.TestCase):
40 """Tests of the storage class infrastructure.
41 """
43 def testCreation(self):
44 """Test that we can dynamically create storage class subclasses.
46 This is critical for testing the factory functions."""
47 className = "TestImage"
48 sc = StorageClass(className, pytype=dict)
49 self.assertIsInstance(sc, StorageClass)
50 self.assertEqual(sc.name, className)
51 self.assertEqual(str(sc), className)
52 self.assertFalse(sc.components)
53 self.assertTrue(sc.validateInstance({}))
54 self.assertFalse(sc.validateInstance(""))
56 r = repr(sc)
57 self.assertIn("StorageClass", r)
58 self.assertIn(className, r)
59 self.assertNotIn("parameters", r)
60 self.assertIn("pytype='dict'", r)
62 # Ensure we do not have an assembler
63 with self.assertRaises(TypeError):
64 sc.assembler()
66 # Allow no definition of python type
67 scn = StorageClass(className)
68 self.assertIs(scn.pytype, object)
70 # Include some components
71 scc = StorageClass(className, pytype=PythonType, components={"comp1": sc})
72 self.assertIn("comp1", scc.components)
73 r = repr(scc)
74 self.assertIn("comp1", r)
75 self.assertIn("lsst.daf.butler.core.assembler.CompositeAssembler", r)
77 # Ensure that we have an assembler
78 self.assertIsInstance(scc.assembler(), CompositeAssembler)
80 # Check we can create a storageClass using the name of an importable
81 # type.
82 sc2 = StorageClass("TestImage2",
83 "lsst.daf.butler.core.storageClass.StorageClassFactory")
84 self.assertIsInstance(sc2.pytype(), StorageClassFactory)
85 self.assertIn("butler.core", repr(sc2))
87 def testParameters(self):
88 """Test that we can set parameters and validate them"""
89 pt = ("a", "b")
90 ps = {"a", "b"}
91 pl = ["a", "b"]
92 for p in (pt, ps, pl):
93 sc1 = StorageClass("ParamClass", pytype=dict, parameters=p)
94 self.assertEqual(sc1.parameters, ps)
95 sc1.validateParameters(p)
97 sc1.validateParameters()
98 sc1.validateParameters({"a": None, "b": None})
99 sc1.validateParameters(["a", ])
100 with self.assertRaises(KeyError):
101 sc1.validateParameters({"a", "c"})
103 def testEquality(self):
104 """Test that StorageClass equality works"""
105 className = "TestImage"
106 sc1 = StorageClass(className, pytype=dict)
107 sc2 = StorageClass(className, pytype=dict)
108 self.assertEqual(sc1, sc2)
109 sc3 = StorageClass(className + "2", pytype=str)
110 self.assertNotEqual(sc1, sc3)
112 # Same StorageClass name but different python type
113 sc4 = StorageClass(className, pytype=str)
114 self.assertNotEqual(sc1, sc4)
116 # Parameters
117 scp = StorageClass("Params", pytype=PythonType, parameters=["a", "b", "c"])
118 scp1 = StorageClass("Params", pytype=PythonType, parameters=["a", "b", "c"])
119 scp2 = StorageClass("Params", pytype=PythonType, parameters=["a", "b", "d", "e"])
120 self.assertEqual(scp, scp1)
121 self.assertNotEqual(scp, scp2)
123 # Now with components
124 sc5 = StorageClass("Composite", pytype=PythonType,
125 components={"comp1": sc1, "comp2": sc3})
126 sc6 = StorageClass("Composite", pytype=PythonType,
127 components={"comp1": sc1, "comp2": sc3})
128 self.assertEqual(sc5, sc6)
129 self.assertNotEqual(sc5, sc3)
130 sc7 = StorageClass("Composite", pytype=PythonType,
131 components={"comp1": sc4, "comp2": sc3})
132 self.assertNotEqual(sc5, sc7)
133 sc8 = StorageClass("Composite", pytype=PythonType,
134 components={"comp2": sc3})
135 self.assertNotEqual(sc5, sc8)
136 sc9 = StorageClass("Composite", pytype=PythonType,
137 components={"comp2": sc3}, assembler="lsst.daf.butler.Butler")
138 self.assertNotEqual(sc5, sc9)
140 def testRegistry(self):
141 """Check that storage classes can be created on the fly and stored
142 in a registry."""
143 className = "TestImage"
144 factory = StorageClassFactory()
145 newclass = StorageClass(className, pytype=PythonType)
146 factory.registerStorageClass(newclass)
147 sc = factory.getStorageClass(className)
148 self.assertIsInstance(sc, StorageClass)
149 self.assertEqual(sc.name, className)
150 self.assertFalse(sc.components)
151 self.assertEqual(sc.pytype, PythonType)
152 self.assertIn(sc, factory)
153 newclass2 = StorageClass("Temporary2", pytype=str)
154 self.assertNotIn(newclass2, factory)
155 factory.registerStorageClass(newclass2)
156 self.assertIn(newclass2, factory)
157 self.assertIn("Temporary2", factory)
158 self.assertNotIn("Temporary3", factory)
159 self.assertNotIn({}, factory)
161 # Make sure we can't register a storage class with the same name
162 # but different values
163 newclass3 = StorageClass("Temporary2", pytype=dict)
164 with self.assertRaises(ValueError):
165 factory.registerStorageClass(newclass3)
167 factory._unregisterStorageClass(newclass3.name)
168 self.assertNotIn(newclass3, factory)
169 self.assertNotIn(newclass3.name, factory)
170 factory.registerStorageClass(newclass3)
171 self.assertIn(newclass3, factory)
172 self.assertIn(newclass3.name, factory)
174 # Check you can silently insert something that is already there
175 factory.registerStorageClass(newclass3)
177 def testFactoryConfig(self):
178 factory = StorageClassFactory()
179 factory.addFromConfig(StorageClassConfig())
180 image = factory.getStorageClass("Image")
181 imageF = factory.getStorageClass("ImageF")
182 self.assertIsInstance(imageF, type(image))
183 self.assertNotEqual(imageF, image)
185 # Check component inheritance
186 exposure = factory.getStorageClass("Exposure")
187 exposureF = factory.getStorageClass("ExposureF")
188 self.assertIsInstance(exposureF, type(exposure))
189 self.assertIsInstance(exposure.components["image"], type(image))
190 self.assertNotIsInstance(exposure.components["image"], type(imageF))
191 self.assertIsInstance(exposureF.components["image"], type(image))
192 self.assertIsInstance(exposureF.components["image"], type(imageF))
193 self.assertIn("wcs", exposure.components)
194 self.assertIn("wcs", exposureF.components)
196 # Check parameters
197 factory.addFromConfig(os.path.join(TESTDIR, "config", "basic", "storageClasses.yaml"))
198 thing1 = factory.getStorageClass("ThingOne")
199 thing2 = factory.getStorageClass("ThingTwo")
200 self.assertIsInstance(thing2, type(thing1))
201 param1 = thing1.parameters
202 param2 = thing2.parameters
203 self.assertIn("param3", thing2.parameters)
204 self.assertNotIn("param3", thing1.parameters)
205 param2.remove("param3")
206 self.assertEqual(param1, param2)
208 # Check that we can't have a new StorageClass that does not
209 # inherit from StorageClass
210 with self.assertRaises(ValueError):
211 factory.makeNewStorageClass("ClassName", baseClass=StorageClassFactory)
213 sc = factory.makeNewStorageClass("ClassName")
214 self.assertIsInstance(sc(), StorageClass)
216 def testPickle(self):
217 """Test that we can pickle storageclasses.
218 """
219 className = "TestImage"
220 sc = StorageClass(className, pytype=dict)
221 self.assertIsInstance(sc, StorageClass)
222 self.assertEqual(sc.name, className)
223 self.assertFalse(sc.components)
224 sc2 = pickle.loads(pickle.dumps(sc))
225 self.assertEqual(sc2, sc)
228if __name__ == "__main__": 228 ↛ 229line 228 didn't jump to line 229, because the condition on line 228 was never true
229 unittest.main()