Coverage for tests/test_registry.py: 15%
Shortcuts 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
Shortcuts 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 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
29import lsst.pex.config as pexConfig
32class ConfigTest(unittest.TestCase):
33 def setUp(self):
34 """Note: the classes are defined here in order to test the register
35 decorator.
36 """
37 class ParentConfig(pexConfig.Config):
38 pass
40 self.registry = pexConfig.makeRegistry(doc="unit test configs", configBaseType=ParentConfig)
42 class FooConfig1(ParentConfig):
43 pass
44 self.fooConfig1Class = FooConfig1
46 class FooConfig2(ParentConfig):
47 pass
48 self.fooConfig2Class = FooConfig2
50 class Config1(pexConfig.Config):
51 pass
52 self.config1Class = Config1
54 class Config2(pexConfig.Config):
55 pass
56 self.config2Class = Config2
58 @pexConfig.registerConfigurable("foo1", self.registry)
59 class FooAlg1:
60 ConfigClass = FooConfig1
62 def __init__(self, config):
63 self.config = config
65 def foo(self):
66 pass
67 self.fooAlg1Class = FooAlg1
69 class FooAlg2:
70 ConfigClass = FooConfig2
72 def __init__(self, config):
73 self.config = config
75 def foo(self):
76 pass
77 self.registry.register("foo2", FooAlg2, FooConfig2)
78 self.fooAlg2Class = FooAlg2
80 # override Foo2 with FooConfig1
81 self.registry.register("foo21", FooAlg2, FooConfig1)
83 def tearDown(self):
84 del self.registry
85 del self.fooConfig1Class
86 del self.fooConfig2Class
87 del self.fooAlg1Class
88 del self.fooAlg2Class
90 def testBasics(self):
91 self.assertEqual(self.registry["foo1"], self.fooAlg1Class)
92 self.assertEqual(self.registry["foo2"].ConfigClass, self.fooConfig2Class)
93 self.assertEqual(self.registry["foo21"].ConfigClass, self.fooConfig1Class)
95 self.assertEqual(set(self.registry.keys()), set(("foo1", "foo2", "foo21")))
97 def testWrapper(self):
98 wrapper21 = self.registry["foo21"]
99 foo21 = wrapper21(wrapper21.ConfigClass())
100 self.assertIsInstance(foo21, self.fooAlg2Class)
102 def testReplace(self):
103 """Test replacement in registry (should always fail)
104 """
105 self.assertRaises(Exception, self.registry.register, "foo1", self.fooAlg2Class)
106 self.assertEqual(self.registry["foo1"], self.fooAlg1Class)
108 def testNesting(self):
109 """Make sure nesting a config with a RegistryField doesn't deep-copy
110 the registry."""
111 class MidConfig(pexConfig.Config):
112 field = self.registry.makeField("docs for registry field")
114 class TopConfig(pexConfig.Config):
115 middle = pexConfig.ConfigField(dtype=MidConfig, doc="docs for middle")
116 self.assertIs(MidConfig.field.registry, self.registry)
117 middle = MidConfig()
118 top = TopConfig()
119 self.assertIs(middle.field.registry, self.registry)
120 self.assertIs(top.middle.field.registry, self.registry)
122 def testRegistryField(self):
123 class C1(pexConfig.Config):
124 r = self.registry.makeField("registry field")
126 for t in C1.r.typemap:
127 self.assertEqual(C1.r.typemap[t], self.registry[t].ConfigClass)
129 c = C1()
130 c.r = "foo2"
131 c.r.apply()
133 def testExceptions(self):
134 class C1(pexConfig.Config):
135 r = self.registry.makeField("registry field", multi=True, default=[])
136 c = C1()
138 def fail(name): # lambda doesn't like |=
139 c.r.names |= [name]
140 self.assertRaises(pexConfig.FieldValidationError, fail, "bar")
143if __name__ == "__main__": 143 ↛ 144line 143 didn't jump to line 144, because the condition on line 143 was never true
144 unittest.main()