Coverage for tests/test_dictField.py: 21%
88 statements
« prev ^ index » next coverage.py v6.4.1, created at 2022-06-23 02:28 -0700
« prev ^ index » next coverage.py v6.4.1, created at 2022-06-23 02:28 -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 pickle
29import unittest
31import lsst.pex.config as pexConfig
34class Config1(pexConfig.Config):
35 d1 = pexConfig.DictField("d1", keytype=str, itemtype=int, default={"hi": 4}, itemCheck=lambda x: x > 0) 35 ↛ exitline 35 didn't run the lambda on line 35
36 d2 = pexConfig.DictField("d2", keytype=str, itemtype=str, default=None)
37 d3 = pexConfig.DictField("d3", keytype=float, itemtype=float, optional=True, itemCheck=lambda x: x > 0) 37 ↛ exitline 37 didn't run the lambda on line 37
38 d4 = pexConfig.DictField("d4", keytype=str, itemtype=None, default={})
41class DictFieldTest(unittest.TestCase):
42 def testConstructor(self):
43 try:
45 class BadKeytype(pexConfig.Config):
46 d = pexConfig.DictField("...", keytype=list, itemtype=int)
48 except Exception:
49 pass
50 else:
51 raise SyntaxError("Unsupported keyptype DictFields should not be allowed")
53 try:
55 class BadItemtype(pexConfig.Config):
56 d = pexConfig.DictField("...", keytype=int, itemtype=dict)
58 except Exception:
59 pass
60 else:
61 raise SyntaxError("Unsupported itemtype DictFields should not be allowed")
63 try:
65 class BadItemCheck(pexConfig.Config):
66 d = pexConfig.DictField("...", keytype=int, itemtype=int, itemCheck=4)
68 except Exception:
69 pass
70 else:
71 raise SyntaxError("Non-callable itemCheck DictFields should not be allowed")
73 try:
75 class BadDictCheck(pexConfig.Config):
76 d = pexConfig.DictField("...", keytype=int, itemtype=int, dictCheck=4)
78 except Exception:
79 pass
80 else:
81 raise SyntaxError("Non-callable dictCheck DictFields should not be allowed")
83 def testAssignment(self):
84 c = Config1()
85 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "d1", {3: 3})
86 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "d1", {"a": 0})
87 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "d1", [1.2, 3, 4])
88 c.d1 = None
89 c.d1 = {"a": 1, "b": 2}
90 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "d3", {"hi": True})
91 c.d3 = {4: 5}
92 self.assertEqual(c.d3, {4.0: 5.0})
93 d = {"a": None, "b": 4, "c": "foo"}
94 c.d4 = d
95 self.assertEqual(c.d4, d)
96 c.d4["a"] = 12
97 c.d4["b"] = "three"
98 c.d4["c"] = None
99 self.assertEqual(c.d4["a"], 12)
100 self.assertEqual(c.d4["b"], "three")
101 self.assertIsNone(c.d4["c"])
102 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "d4", {"hi": [1, 2, 3]})
104 def testValidate(self):
105 c = Config1()
106 self.assertRaises(pexConfig.FieldValidationError, Config1.validate, c)
108 c.d2 = {"a": "b"}
109 c.validate()
111 def testInPlaceModification(self):
112 c = Config1()
113 self.assertRaises(pexConfig.FieldValidationError, c.d1.__setitem__, 2, 0)
114 self.assertRaises(pexConfig.FieldValidationError, c.d1.__setitem__, "hi", 0)
115 c.d1["hi"] = 10
116 self.assertEqual(c.d1, {"hi": 10})
118 c.d3 = {}
119 c.d3[4] = 5
120 self.assertEqual(c.d3, {4.0: 5.0})
122 def testNoArbitraryAttributes(self):
123 c = Config1()
124 self.assertRaises(pexConfig.FieldValidationError, setattr, c.d1, "should", "fail")
126 def testEquality(self):
127 """Test DictField.__eq__
129 We create two dicts, with the keys explicitly added in a different
130 order and test their equality.
131 """
132 keys1 = ["A", "B", "C"]
133 keys2 = ["X", "Y", "Z", "a", "b", "c", "d", "e"]
135 c1 = Config1()
136 c1.d4 = {k: "" for k in keys1}
137 for k in keys2:
138 c1.d4[k] = ""
140 c2 = Config1()
141 for k in keys2 + keys1:
142 c2.d4[k] = ""
144 self.assertTrue(pexConfig.compareConfigs("test", c1, c2))
146 def testNoPickle(self):
147 """Test that pickle support is disabled for the proxy container."""
148 c = Config1()
149 with self.assertRaises(pexConfig.UnexpectedProxyUsageError):
150 pickle.dumps(c.d4)
153if __name__ == "__main__": 153 ↛ 154line 153 didn't jump to line 154, because the condition on line 153 was never true
154 unittest.main()