Coverage for tests/test_registry.py: 14%
81 statements
« prev ^ index » next coverage.py v6.4.4, created at 2022-09-07 09:30 +0000
« prev ^ index » next coverage.py v6.4.4, created at 2022-09-07 09:30 +0000
1# This file is part of pex_config.
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 software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
28import unittest
30import lsst.pex.config as pexConfig
33class ConfigTest(unittest.TestCase):
34 def setUp(self):
35 """Note: the classes are defined here in order to test the register
36 decorator.
37 """
39 class ParentConfig(pexConfig.Config):
40 pass
42 self.registry = pexConfig.makeRegistry(doc="unit test configs", configBaseType=ParentConfig)
44 class FooConfig1(ParentConfig):
45 pass
47 self.fooConfig1Class = FooConfig1
49 class FooConfig2(ParentConfig):
50 pass
52 self.fooConfig2Class = FooConfig2
54 class Config1(pexConfig.Config):
55 pass
57 self.config1Class = Config1
59 class Config2(pexConfig.Config):
60 pass
62 self.config2Class = Config2
64 @pexConfig.registerConfigurable("foo1", self.registry)
65 class FooAlg1:
66 ConfigClass = FooConfig1
68 def __init__(self, config):
69 self.config = config
71 def foo(self):
72 pass
74 self.fooAlg1Class = FooAlg1
76 class FooAlg2:
77 ConfigClass = FooConfig2
79 def __init__(self, config):
80 self.config = config
82 def foo(self):
83 pass
85 self.registry.register("foo2", FooAlg2, FooConfig2)
86 self.fooAlg2Class = FooAlg2
88 # override Foo2 with FooConfig1
89 self.registry.register("foo21", FooAlg2, FooConfig1)
91 def tearDown(self):
92 del self.registry
93 del self.fooConfig1Class
94 del self.fooConfig2Class
95 del self.fooAlg1Class
96 del self.fooAlg2Class
98 def testBasics(self):
99 self.assertEqual(self.registry["foo1"], self.fooAlg1Class)
100 self.assertEqual(self.registry["foo2"].ConfigClass, self.fooConfig2Class)
101 self.assertEqual(self.registry["foo21"].ConfigClass, self.fooConfig1Class)
103 self.assertEqual(set(self.registry.keys()), set(("foo1", "foo2", "foo21")))
105 def testWrapper(self):
106 wrapper21 = self.registry["foo21"]
107 foo21 = wrapper21(wrapper21.ConfigClass())
108 self.assertIsInstance(foo21, self.fooAlg2Class)
110 def testReplace(self):
111 """Test replacement in registry (should always fail)"""
112 self.assertRaises(Exception, self.registry.register, "foo1", self.fooAlg2Class)
113 self.assertEqual(self.registry["foo1"], self.fooAlg1Class)
115 def testNesting(self):
116 """Make sure nesting a config with a RegistryField doesn't deep-copy
117 the registry."""
119 class MidConfig(pexConfig.Config):
120 field = self.registry.makeField("docs for registry field")
122 class TopConfig(pexConfig.Config):
123 middle = pexConfig.ConfigField(dtype=MidConfig, doc="docs for middle")
125 self.assertIs(MidConfig.field.registry, self.registry)
126 middle = MidConfig()
127 top = TopConfig()
128 self.assertIs(middle.field.registry, self.registry)
129 self.assertIs(top.middle.field.registry, self.registry)
131 def testRegistryField(self):
132 class C1(pexConfig.Config):
133 r = self.registry.makeField("registry field")
135 for t in C1.r.typemap:
136 self.assertEqual(C1.r.typemap[t], self.registry[t].ConfigClass)
138 c = C1()
139 c.r = "foo2"
140 c.r.apply()
142 def testExceptions(self):
143 class C1(pexConfig.Config):
144 r = self.registry.makeField("registry field", multi=True, default=[])
146 c = C1()
148 def fail(name): # lambda doesn't like |=
149 c.r.names |= [name]
151 self.assertRaises(pexConfig.FieldValidationError, fail, "bar")
154if __name__ == "__main__": 154 ↛ 155line 154 didn't jump to line 155, because the condition on line 154 was never true
155 unittest.main()