Coverage for tests/test_configDictField.py: 26%
95 statements
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-15 02:35 -0700
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-15 02:35 -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 unittest
31import lsst.pex.config as pexConfig
34class Config1(pexConfig.Config):
35 f = pexConfig.Field("f", float, default=3.0)
37 def _collectImports(self):
38 # Exists to test that imports of dict values are collected
39 self._imports.add("builtins")
42class Config2(pexConfig.Config):
43 d1 = pexConfig.ConfigDictField("d1", keytype=str, itemtype=Config1, itemCheck=lambda x: x.f > 0) 43 ↛ exitline 43 didn't run the lambda on line 43
46class Config3(pexConfig.Config):
47 field1 = pexConfig.ConfigDictField(keytype=str, itemtype=pexConfig.Config, default={}, doc="doc")
50class ConfigDictFieldTest(unittest.TestCase):
51 def testConstructor(self):
52 try:
54 class BadKeytype(pexConfig.Config):
55 d = pexConfig.ConfigDictField("...", keytype=list, itemtype=Config1)
57 except Exception:
58 pass
59 else:
60 raise SyntaxError("Unsupported keytypes should not be allowed")
62 try:
64 class BadItemtype(pexConfig.Config):
65 d = pexConfig.ConfigDictField("...", keytype=int, itemtype=dict)
67 except Exception:
68 pass
69 else:
70 raise SyntaxError("Unsupported itemtypes should not be allowed")
72 try:
74 class BadItemCheck(pexConfig.Config):
75 d = pexConfig.ConfigDictField("...", keytype=str, itemtype=Config1, itemCheck=4)
77 except Exception:
78 pass
79 else:
80 raise SyntaxError("Non-callable itemCheck should not be allowed")
82 try:
84 class BadDictCheck(pexConfig.Config):
85 d = pexConfig.DictField("...", keytype=int, itemtype=Config1, dictCheck=4)
87 except Exception:
88 pass
89 else:
90 raise SyntaxError("Non-callable dictCheck should not be allowed")
92 def testAssignment(self):
93 c = Config2()
94 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "d1", {3: 3})
95 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "d1", {"a": 0})
96 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "d1", [1.2, 3, 4])
97 c.d1 = None
98 c.d1 = {"a": Config1, "b": Config1()}
100 def testValidate(self):
101 c = Config2()
102 self.assertRaises(pexConfig.FieldValidationError, Config2.validate, c)
104 c.d1 = {"a": Config1(f=0)}
105 self.assertRaises(pexConfig.FieldValidationError, Config2.validate, c)
107 c.d1["a"].f = 5
108 c.validate()
110 def testInPlaceModification(self):
111 c = Config2(d1={})
112 self.assertRaises(pexConfig.FieldValidationError, c.d1.__setitem__, 1, 0)
113 self.assertRaises(pexConfig.FieldValidationError, c.d1.__setitem__, "a", 0)
114 c.d1["a"] = Config1(f=4)
115 self.assertEqual(c.d1["a"].f, 4)
117 def testSave(self):
118 c = Config2(d1={"a": Config1(f=4)})
119 c.save("configDictTest.py")
121 # verify _collectImports is called on all the configDictValues
122 stringOutput = c.saveToString()
123 self.assertIn("import builtins", stringOutput)
125 rt = Config2()
126 rt.load("configDictTest.py")
128 os.remove("configDictTest.py")
129 self.assertEqual(rt.d1["a"].f, c.d1["a"].f)
131 c = Config2()
132 c.save("emptyConfigDictTest.py")
133 rt.load("emptyConfigDictTest.py")
134 os.remove("emptyConfigDictTest.py")
136 self.assertIsNone(rt.d1)
138 def testToDict(self):
139 c = Config2(d1={"a": Config1(f=4), "b": Config1})
140 dict_ = c.toDict()
141 self.assertEqual(dict_, {"d1": {"a": {"f": 4.0}, "b": {"f": 3.0}}})
143 def testFreeze(self):
144 c = Config2(d1={"a": Config1(f=4), "b": Config1})
145 c.freeze()
147 self.assertRaises(pexConfig.FieldValidationError, setattr, c.d1["a"], "f", 0)
149 def testNoArbitraryAttributes(self):
150 c = Config2(d1={})
151 self.assertRaises(pexConfig.FieldValidationError, setattr, c.d1, "should", "fail")
153 def testEquality(self):
154 """Test ConfigDictField.__eq__
156 We create two configs, with the keys explicitly added in a different
157 order and test their equality.
158 """
159 keys1 = ["A", "B", "C"]
160 keys2 = ["X", "Y", "Z", "a", "b", "c", "d", "e"]
162 c1 = Config3()
163 c1.field1 = {k: pexConfig.Config() for k in keys1}
164 for k in keys2:
165 c1.field1[k] = pexConfig.Config()
167 c2 = Config3()
168 for k in keys2 + keys1:
169 c2.field1[k] = pexConfig.Config()
171 self.assertTrue(pexConfig.compareConfigs("test", c1, c2))
174if __name__ == "__main__": 174 ↛ 175line 174 didn't jump to line 175, because the condition on line 174 was never true
175 unittest.main()