Coverage for tests/test_configChoiceField.py: 37%
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 os
29import unittest
31import lsst.pex.config as pexConfig
34class Config1(pexConfig.Config):
35 f = pexConfig.Field(doc="Config1.f", dtype=int, default=4)
37 def validate(self):
38 pexConfig.Config.validate(self)
39 if self.f <= 0:
40 raise pexConfig.FieldValidationError(Config1.f, self, "f should be > 0")
43class Config2(pexConfig.Config):
44 f = pexConfig.Field(doc="Config2.f", dtype=float, default=0.5, check=lambda x: x > 0) 44 ↛ exitline 44 didn't run the lambda on line 44
47TYPEMAP = {"AAA": Config1, "BBB": Config2, "CCC": Config1}
50class Config3(pexConfig.Config):
51 a = pexConfig.ConfigChoiceField(
52 doc="single non-optional", typemap=TYPEMAP, default="AAA", multi=False, optional=False
53 )
54 b = pexConfig.ConfigChoiceField(
55 doc="single optional", typemap=TYPEMAP, default="AAA", multi=False, optional=True
56 )
57 c = pexConfig.ConfigChoiceField(
58 doc="multi non-optional", typemap=TYPEMAP, default=["AAA"], multi=True, optional=False
59 )
60 d = pexConfig.ConfigChoiceField(
61 doc="multi optional", typemap=TYPEMAP, default=["AAA"], multi=True, optional=True
62 )
65class ConfigChoiceFieldTest(unittest.TestCase):
66 def setUp(self):
67 self.config = Config3()
69 def tearDown(self):
70 del self.config
72 def testInit(self):
73 self.assertEqual(self.config.a.name, "AAA")
74 self.assertEqual(self.config.a.active.f, 4)
75 self.assertEqual(self.config.a["AAA"].f, 4)
76 self.assertEqual(self.config.a["BBB"].f, 0.5)
78 def testSave(self):
79 self.config.a["AAA"].f = 1
80 self.config.a["BBB"].f = 1.0
81 self.config.a = "BBB"
82 path = "choiceFieldTest.config"
83 self.config.save(path)
84 roundtrip = Config3()
85 roundtrip.load(path)
86 os.remove(path)
88 self.assertEqual(self.config.a.name, roundtrip.a.name)
89 self.assertEqual(self.config.a["AAA"].f, roundtrip.a["AAA"].f)
90 self.assertEqual(self.config.a["BBB"].f, roundtrip.a["BBB"].f)
92 def testValidate(self):
93 self.config.validate()
94 self.config.a = "AAA"
95 self.config.a["AAA"].f = 0
97 self.assertRaises(pexConfig.FieldValidationError, self.config.validate)
99 self.config.a = "BBB"
100 self.config.validate()
102 self.config.a = None
103 self.assertRaises(pexConfig.FieldValidationError, self.config.validate)
105 def testFreeze(self):
106 self.config.freeze()
107 self.assertRaises(pexConfig.FieldValidationError, setattr, self.config.a, "name", "AAA")
108 self.assertRaises(pexConfig.FieldValidationError, setattr, self.config.a["AAA"], "f", "1")
110 # Create a new unfrozen config
111 unfrozenConfig = Config3()
113 # Add a new entries to the typemap after the config is frozen and check
114 # that it is not in the frozen configs keys
115 TYPEMAP["DDD"] = Config1
116 self.assertNotIn("DDD", self.config.a.keys())
118 # Verify that the entry added to the typemap does show up in the
119 # unfrozen config
120 self.assertIn("DDD", unfrozenConfig.a.keys())
122 def testNoArbitraryAttributes(self):
123 self.assertRaises(pexConfig.FieldValidationError, setattr, self.config.a, "should", "fail")
125 def testSelectionSet(self):
126 # test in place modification
127 self.config.c.names.add("BBB")
128 self.assertEqual(set(self.config.c.names), set(["AAA", "BBB"]))
129 self.config.c.names.remove("AAA")
130 self.assertEqual(set(self.config.c.names), set(["BBB"]))
131 self.assertRaises(KeyError, self.config.c.names.remove, "AAA")
132 self.config.c.names.discard("AAA")
134 # test bad assignment
135 self.assertRaises(pexConfig.FieldValidationError, setattr, self.config.c, "names", "AAA")
136 self.config.c.names = ["AAA"]
138 def testNoneValue(self):
139 self.config.a = None
140 self.assertRaises(pexConfig.FieldValidationError, self.config.validate)
141 self.config.a = "AAA"
142 self.config.b = None
143 self.config.validate()
144 self.config.c = None
145 self.assertRaises(pexConfig.FieldValidationError, self.config.validate)
146 self.config.c = ["AAA"]
147 self.config.d = None
148 self.config.validate()
151if __name__ == "__main__": 151 ↛ 152line 151 didn't jump to line 152, because the condition on line 151 was never true
152 unittest.main()