Coverage for tests/test_configChoiceField.py: 36%
86 statements
« prev ^ index » next coverage.py v6.4.4, created at 2022-08-30 02:27 -0700
« prev ^ index » next coverage.py v6.4.4, created at 2022-08-30 02:27 -0700
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 pickle
30import unittest
32import lsst.pex.config as pexConfig
35class Config1(pexConfig.Config):
36 f = pexConfig.Field(doc="Config1.f", dtype=int, default=4)
38 def validate(self):
39 pexConfig.Config.validate(self)
40 if self.f <= 0:
41 raise pexConfig.FieldValidationError(Config1.f, self, "f should be > 0")
44class Config2(pexConfig.Config):
45 f = pexConfig.Field(doc="Config2.f", dtype=float, default=0.5, check=lambda x: x > 0) 45 ↛ exitline 45 didn't run the lambda on line 45
48TYPEMAP = {"AAA": Config1, "BBB": Config2, "CCC": Config1}
51class Config3(pexConfig.Config):
52 a = pexConfig.ConfigChoiceField(
53 doc="single non-optional", typemap=TYPEMAP, default="AAA", multi=False, optional=False
54 )
55 b = pexConfig.ConfigChoiceField(
56 doc="single optional", typemap=TYPEMAP, default="AAA", multi=False, optional=True
57 )
58 c = pexConfig.ConfigChoiceField(
59 doc="multi non-optional", typemap=TYPEMAP, default=["AAA"], multi=True, optional=False
60 )
61 d = pexConfig.ConfigChoiceField(
62 doc="multi optional", typemap=TYPEMAP, default=["AAA"], multi=True, optional=True
63 )
66class ConfigChoiceFieldTest(unittest.TestCase):
67 def setUp(self):
68 self.config = Config3()
70 def tearDown(self):
71 del self.config
73 def testInit(self):
74 self.assertEqual(self.config.a.name, "AAA")
75 self.assertEqual(self.config.a.active.f, 4)
76 self.assertEqual(self.config.a["AAA"].f, 4)
77 self.assertEqual(self.config.a["BBB"].f, 0.5)
79 def testSave(self):
80 self.config.a["AAA"].f = 1
81 self.config.a["BBB"].f = 1.0
82 self.config.a = "BBB"
83 path = "choiceFieldTest.config"
84 self.config.save(path)
85 roundtrip = Config3()
86 roundtrip.load(path)
87 os.remove(path)
89 self.assertEqual(self.config.a.name, roundtrip.a.name)
90 self.assertEqual(self.config.a["AAA"].f, roundtrip.a["AAA"].f)
91 self.assertEqual(self.config.a["BBB"].f, roundtrip.a["BBB"].f)
93 def testValidate(self):
94 self.config.validate()
95 self.config.a = "AAA"
96 self.config.a["AAA"].f = 0
98 self.assertRaises(pexConfig.FieldValidationError, self.config.validate)
100 self.config.a = "BBB"
101 self.config.validate()
103 self.config.a = None
104 self.assertRaises(pexConfig.FieldValidationError, self.config.validate)
106 def testFreeze(self):
107 self.config.freeze()
108 self.assertRaises(pexConfig.FieldValidationError, setattr, self.config.a, "name", "AAA")
109 self.assertRaises(pexConfig.FieldValidationError, setattr, self.config.a["AAA"], "f", "1")
111 # Create a new unfrozen config
112 unfrozenConfig = Config3()
114 # Add a new entries to the typemap after the config is frozen and check
115 # that it is not in the frozen configs keys
116 TYPEMAP["DDD"] = Config1
117 self.assertNotIn("DDD", self.config.a.keys())
119 # Verify that the entry added to the typemap does show up in the
120 # unfrozen config
121 self.assertIn("DDD", unfrozenConfig.a.keys())
123 def testNoArbitraryAttributes(self):
124 self.assertRaises(pexConfig.FieldValidationError, setattr, self.config.a, "should", "fail")
126 def testSelectionSet(self):
127 # test in place modification
128 self.config.c.names.add("BBB")
129 self.assertEqual(set(self.config.c.names), set(["AAA", "BBB"]))
130 self.config.c.names.remove("AAA")
131 self.assertEqual(set(self.config.c.names), set(["BBB"]))
132 self.assertRaises(KeyError, self.config.c.names.remove, "AAA")
133 self.config.c.names.discard("AAA")
135 # test bad assignment
136 self.assertRaises(pexConfig.FieldValidationError, setattr, self.config.c, "names", "AAA")
137 self.config.c.names = ["AAA"]
139 def testNoneValue(self):
140 self.config.a = None
141 self.assertRaises(pexConfig.FieldValidationError, self.config.validate)
142 self.config.a = "AAA"
143 self.config.b = None
144 self.config.validate()
145 self.config.c = None
146 self.assertRaises(pexConfig.FieldValidationError, self.config.validate)
147 self.config.c = ["AAA"]
148 self.config.d = None
149 self.config.validate()
151 def testNoPickle(self):
152 """Test that pickle support is disabled for the proxy container."""
153 with self.assertRaises(pexConfig.UnexpectedProxyUsageError):
154 pickle.dumps(self.config.c)
155 with self.assertRaises(pexConfig.UnexpectedProxyUsageError):
156 pickle.dumps(self.config.c.names)
159if __name__ == "__main__": 159 ↛ 160line 159 didn't jump to line 160, because the condition on line 159 was never true
160 unittest.main()