Coverage for tests/test_configApdbLoader.py: 24%
137 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-03-19 02:12 -0700
« prev ^ index » next coverage.py v7.4.4, created at 2024-03-19 02:12 -0700
1# This file is part of verify.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://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 <https://www.gnu.org/licenses/>.
22import unittest
24import lsst.utils.tests
25from lsst.pex.config import Config, Field, ConfigField, ConfigChoiceField, \
26 RegistryField, Registry, ConfigurableField, ConfigurableInstance, \
27 ConfigDictField
28from lsst.dax.apdb import Apdb, ApdbConfig, ApdbSql, ApdbSqlConfig
30from lsst.verify.tasks import ConfigApdbLoader
33class ConfigApdbLoaderTestSuite(lsst.utils.tests.TestCase):
35 @staticmethod
36 def _dummyRegistry():
37 class DummyConfigurable:
38 ConfigClass = Config
39 registry = Registry()
40 registry.register("foo", DummyConfigurable)
41 registry.register("bar", ApdbSql, ConfigClass=ApdbSqlConfig)
42 return registry
44 @staticmethod
45 def _dummyApdbConfig():
46 config = ApdbSqlConfig()
47 config.db_url = "sqlite://" # in-memory DB
48 return config
50 def setUp(self):
51 self.task = ConfigApdbLoader()
53 def testEmptyConfig(self):
54 result = self.task.run(Config())
55 self.assertIsNone(result.apdb)
57 def testSelfConfig(self):
58 result = self.task.run(self._dummyApdbConfig())
59 self.assertIsInstance(result.apdb, Apdb)
61 def testConfigChoiceFieldUnSelected(self):
62 typemap = {"foo": Config, "bar": ApdbConfig}
64 class TestConfig(Config):
65 field = ConfigChoiceField(typemap=typemap, doc="test")
67 config = TestConfig()
68 config.field = "foo"
69 result = self.task.run(config)
70 self.assertIsNone(result.apdb)
72 def testConfigChoiceFieldSelected(self):
73 # Note: ConfigChoiceField does not support polymorphic types and it is
74 # not very useful for ApdbConfig and subclasses.
75 typemap = {"foo": Config, "bar": ApdbSqlConfig}
77 class TestConfig(Config):
78 field = ConfigChoiceField(typemap=typemap, doc="test")
80 config = TestConfig()
81 config.field = "bar"
82 config.field["bar"] = self._dummyApdbConfig()
83 result = self.task.run(config)
84 self.assertIsInstance(result.apdb, Apdb)
86 def testConfigChoiceFieldMulti(self):
87 # Note: ConfigChoiceField does not support polymorphic types and it is
88 # not very useful for ApdbConfig and subclasses.
89 typemap = {"foo": Config, "bar": ApdbSqlConfig}
91 class TestConfig(Config):
92 field = ConfigChoiceField(typemap=typemap, doc="test", multi=True)
94 config = TestConfig()
95 config.field = {"bar", "foo"}
96 config.field["bar"] = self._dummyApdbConfig()
97 result = self.task.run(config)
98 self.assertIsInstance(result.apdb, Apdb)
100 def testRegistryFieldUnSelected(self):
101 registry = self._dummyRegistry()
103 class TestConfig(Config):
104 field = RegistryField(registry=registry, doc="test")
106 config = TestConfig()
107 config.field = "foo"
108 result = self.task.run(config)
109 self.assertIsNone(result.apdb)
111 def testRegistryFieldSelected(self):
112 registry = self._dummyRegistry()
114 class TestConfig(Config):
115 field = RegistryField(registry=registry, doc="test")
117 config = TestConfig()
118 config.field = "bar"
119 config.field["bar"] = self._dummyApdbConfig()
120 result = self.task.run(config)
121 self.assertIsInstance(result.apdb, Apdb)
123 def testRegistryFieldMulti(self):
124 registry = self._dummyRegistry()
126 class TestConfig(Config):
127 field = RegistryField(registry=registry, doc="test", multi=True)
129 config = TestConfig()
130 config.field = {"bar", "foo"}
131 config.field["bar"] = self._dummyApdbConfig()
132 result = self.task.run(config)
133 self.assertIsInstance(result.apdb, Apdb)
135 def testConfigField(self):
136 # Note: ConfigField does not support polymorphic types and it is not
137 # very useful for ApdbConfig and subclasses.
138 class TestConfig(Config):
139 field = ConfigField(dtype=ApdbSqlConfig,
140 default=self._dummyApdbConfig(), doc="test")
142 result = self.task.run(TestConfig())
143 self.assertIsInstance(result.apdb, Apdb)
145 def testConfigurableField(self):
146 class TestConfig(Config):
147 field = ConfigurableField(target=ApdbSql, doc="test")
149 config = TestConfig()
150 config.field = self._dummyApdbConfig()
151 self.assertIsInstance(config.field, ConfigurableInstance)
152 result = self.task.run(config)
153 self.assertIsInstance(result.apdb, Apdb)
155 def testConfigurableFieldRetarget(self):
156 # Initally set to abstract target, has to be re-targeted before use.
157 class TestConfig(Config):
158 field = ConfigurableField(target=Apdb, doc="test")
160 config = TestConfig()
161 config.field.retarget(ApdbSql)
162 config.field = self._dummyApdbConfig()
163 self.assertIsInstance(config.field, ConfigurableInstance)
164 result = self.task.run(config)
165 self.assertIsInstance(result.apdb, Apdb)
167 def testConfigDictFieldUnSelected(self):
168 class TestConfig(Config):
169 field = ConfigDictField(keytype=int, itemtype=ApdbConfig, doc="test")
171 result = self.task.run(TestConfig())
172 self.assertIsNone(result.apdb)
174 def testConfigDictFieldSelected(self):
175 # Note: ConfigDictField does not support polymorphic types and it is
176 # not very useful for ApdbConfig and subclasses.
177 class TestConfig(Config):
178 field = ConfigDictField(keytype=int, itemtype=ApdbSqlConfig,
179 doc="test")
181 config = TestConfig()
182 config.field = {42: self._dummyApdbConfig()}
183 result = self.task.run(config)
184 self.assertIsInstance(result.apdb, Apdb)
186 def testSiblingConfigs(self):
187 # Note: ConfigField does not support polymorphic types and it is not
188 # very useful for ApdbConfig and subclasses.
189 class TestConfig(Config):
190 field1 = Field(dtype=int, doc="test")
191 field2 = ConfigField(dtype=ApdbSqlConfig,
192 default=self._dummyApdbConfig(), doc="test")
193 field3 = Field(dtype=str, doc="test")
195 result = self.task.run(TestConfig())
196 self.assertIsInstance(result.apdb, Apdb)
198 def testNestedConfigs(self):
199 class InnerConfig(Config):
200 field = ConfigurableField(target=ApdbSql, doc="test")
202 class TestConfig(Config):
203 field = ConfigField(dtype=InnerConfig, doc="test")
205 config = TestConfig()
206 config.field.field = self._dummyApdbConfig()
207 self.assertIsInstance(config.field.field, ConfigurableInstance)
208 result = self.task.run(config)
209 self.assertIsInstance(result.apdb, Apdb)
212class MemoryTester(lsst.utils.tests.MemoryTestCase):
213 pass
216def setup_module(module):
217 lsst.utils.tests.init()
220if __name__ == "__main__": 220 ↛ 221line 220 didn't jump to line 221, because the condition on line 220 was never true
221 lsst.utils.tests.init()
222 unittest.main()