Coverage for tests / test_configDictField.py: 21%
121 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-28 08:38 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-28 08:38 +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 os
29import tempfile
30import unittest
32import lsst.pex.config as pexConfig
35class Config1(pexConfig.Config):
36 """First test config."""
38 f = pexConfig.Field("f", float, default=3.0)
40 def _collectImports(self):
41 # Exists to test that imports of dict values are collected
42 self._imports.add("builtins")
45class Config2(pexConfig.Config):
46 """Second test config."""
48 d1 = pexConfig.ConfigDictField("d1", keytype=str, itemtype=Config1, itemCheck=lambda x: x.f > 0)
51class Config3(pexConfig.Config):
52 """Third test config."""
54 field1 = pexConfig.ConfigDictField(keytype=str, itemtype=pexConfig.Config, default={}, doc="doc")
57class Config4(pexConfig.Config):
58 """Fourth test config."""
60 field1 = pexConfig.ConfigDictField(
61 keytype=str, itemtype=pexConfig.Config, default={}, doc="doc", keyCheck=lambda k: k.islower()
62 )
65class ConfigDictFieldTest(unittest.TestCase):
66 """Test of ConfigDictField."""
68 def testConstructor(self):
69 try:
71 class BadKeytype(pexConfig.Config):
72 d = pexConfig.ConfigDictField("...", keytype=list, itemtype=Config1)
74 except Exception:
75 pass
76 else:
77 raise SyntaxError("Unsupported keytypes should not be allowed")
79 try:
81 class BadItemtype(pexConfig.Config):
82 d = pexConfig.ConfigDictField("...", keytype=int, itemtype=dict)
84 except Exception:
85 pass
86 else:
87 raise SyntaxError("Unsupported itemtypes should not be allowed")
89 try:
91 class BadKeyCheck(pexConfig.Config):
92 d = pexConfig.ConfigDictField("...", keytype=str, itemtype=Config1, keyCheck=4)
94 except Exception:
95 pass
96 else:
97 raise SyntaxError("Non-callable keyCheck should not be allowed")
99 try:
101 class BadItemCheck(pexConfig.Config):
102 d = pexConfig.ConfigDictField("...", keytype=str, itemtype=Config1, itemCheck=4)
104 except Exception:
105 pass
106 else:
107 raise SyntaxError("Non-callable itemCheck should not be allowed")
109 try:
111 class BadDictCheck(pexConfig.Config):
112 d = pexConfig.DictField("...", keytype=int, itemtype=Config1, dictCheck=4)
114 except Exception:
115 pass
116 else:
117 raise SyntaxError("Non-callable dictCheck should not be allowed")
119 def testAssignment(self):
120 c = Config2()
121 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "d1", {3: 3})
122 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "d1", {"a": 0})
123 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "d1", [1.2, 3, 4])
124 c.d1 = None
125 c.d1 = {"a": Config1, "b": Config1()}
127 def testValidate(self):
128 c = Config2()
129 self.assertRaises(pexConfig.FieldValidationError, Config2.validate, c)
131 c.d1 = {"a": Config1(f=0)}
132 self.assertRaises(pexConfig.FieldValidationError, Config2.validate, c)
134 c.d1["a"].f = 5
135 c.validate()
137 def testKeyCheckValidation(self):
138 c = Config4()
139 c.field1["lower"] = pexConfig.Config()
140 with self.assertRaises(pexConfig.FieldValidationError, msg="Key check should fail"):
141 c.field1["UPPER"] = pexConfig.Config()
142 # No need for c.validate() here, as the exception for key check is
143 # raised by the assignment.
145 def testInPlaceModification(self):
146 c = Config2(d1={})
147 self.assertRaises(pexConfig.FieldValidationError, c.d1.__setitem__, 1, 0)
148 self.assertRaises(pexConfig.FieldValidationError, c.d1.__setitem__, "a", 0)
149 c.d1["a"] = Config1(f=4)
150 self.assertEqual(c.d1["a"].f, 4)
152 def testSave(self):
153 c = Config2(d1={"a": Config1(f=4)})
155 # verify _collectImports is called on all the configDictValues
156 stringOutput = c.saveToString()
157 self.assertIn("import builtins", stringOutput)
159 with tempfile.TemporaryDirectory(prefix="config-dictfield-", ignore_cleanup_errors=True) as tmpdir:
160 path = os.path.join(tmpdir, "configDictTest.py")
161 c.save(path)
163 rt = Config2()
164 rt.load(path)
166 self.assertEqual(rt.d1["a"].f, c.d1["a"].f)
168 c = Config2()
169 path = os.path.join(tmpdir, "emptyConfigDictTest.py")
170 c.save(path)
171 rt.load(path)
173 self.assertIsNone(rt.d1)
175 def testToDict(self):
176 c = Config2(d1={"a": Config1(f=4), "b": Config1})
177 dict_ = c.toDict()
178 self.assertEqual(dict_, {"d1": {"a": {"f": 4.0}, "b": {"f": 3.0}}})
180 def testFreeze(self):
181 c = Config2(d1={"a": Config1(f=4), "b": Config1})
182 c.freeze()
184 self.assertRaises(pexConfig.FieldValidationError, setattr, c.d1["a"], "f", 0)
186 def testNoArbitraryAttributes(self):
187 c = Config2(d1={})
188 self.assertRaises(pexConfig.FieldValidationError, setattr, c.d1, "should", "fail")
190 def testEquality(self):
191 """Test ConfigDictField.__eq__.
193 We create two configs, with the keys explicitly added in a different
194 order and test their equality.
195 """
196 keys1 = ["A", "B", "C"]
197 keys2 = ["X", "Y", "Z", "a", "b", "c", "d", "e"]
199 c1 = Config3()
200 c1.field1 = {k: pexConfig.Config() for k in keys1}
201 for k in keys2:
202 c1.field1[k] = pexConfig.Config()
204 c2 = Config3()
205 for k in keys2 + keys1:
206 c2.field1[k] = pexConfig.Config()
208 self.assertTrue(pexConfig.compareConfigs("test", c1, c2))
210 def test_copy(self):
211 """Test that the copy method works on ConfigDictField instances."""
212 original = Config2()
213 original.d1 = {"a": Config1, "b": Config1(f=4.0)}
214 original.freeze()
215 copy1 = original.copy()
216 self.assertEqual(copy1.d1["a"].f, 3.0)
217 self.assertEqual(copy1.d1["b"].f, 4.0)
218 copy1.d1["a"].f = 6.0
219 self.assertEqual(copy1.d1["a"].f, 6.0)
220 self.assertEqual(copy1.d1["b"].f, 4.0)
221 self.assertEqual(original.d1["a"].f, 3.0)
224if __name__ == "__main__": 224 ↛ 225line 224 didn't jump to line 225 because the condition on line 224 was never true
225 unittest.main()