Coverage for tests/test_listField.py: 20%
103 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-06 09:49 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-06 09:49 +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 pickle
29import unittest
31import lsst.pex.config as pexConfig
34def isSorted(theList):
35 """Determine if a list is sorted."""
36 if len(theList) <= 1:
37 return True
39 p = theList[0]
40 for x in theList[1:]:
41 if x < p:
42 return False
43 p = x
44 return True
47def isPositive(x):
48 """Determine if an integer is positive."""
49 return x > 0
52class Config1(pexConfig.Config):
53 """First test config."""
55 l1 = pexConfig.ListField("l1", int, minLength=2, maxLength=5, default=[1, 2, 3], itemCheck=isPositive)
56 l2 = pexConfig.ListField("l2", int, length=3, default=[1, 2, 3], listCheck=isSorted, itemCheck=isPositive)
57 l3 = pexConfig.ListField("l3", int, length=3, default=None, optional=True, itemCheck=isPositive)
58 l4 = pexConfig.ListField("l4", int, length=3, default=None, itemCheck=isPositive)
61class Config2(pexConfig.Config):
62 """Second test config."""
64 lf = pexConfig.ListField("lf", float, default=[1, 2, 3])
65 ls = pexConfig.ListField("ls", str, default=["hi"])
68class ListFieldTest(unittest.TestCase):
69 """Test ListField."""
71 def testConstructor(self):
72 try:
74 class BadDtype(pexConfig.Config):
75 ll = pexConfig.ListField("...", list)
77 except Exception:
78 pass
79 else:
80 raise SyntaxError("Unsupported dtype ListFields should not be allowed")
82 try:
84 class BadLengths(pexConfig.Config):
85 ll = pexConfig.ListField("...", int, minLength=4, maxLength=2)
87 except ValueError:
88 pass
89 else:
90 raise SyntaxError("minLnegth <= maxLength should not be allowed")
92 try:
94 class BadLength(pexConfig.Config):
95 ll = pexConfig.ListField("...", int, length=-1)
97 except Exception:
98 pass
99 else:
100 raise SyntaxError("negative length should not be allowed")
102 try:
104 class BadLength2(pexConfig.Config):
105 ll = pexConfig.ListField("...", int, maxLength=-1)
107 except Exception:
108 pass
109 else:
110 raise SyntaxError("negative max length should not be allowed")
112 def testAssignment(self):
113 c = Config1()
114 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l1", [1.2, 3, 4])
115 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l1", [-1, -2, -3])
116 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l1", [1, 2, 0])
117 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l1", [1, 2, None])
118 c.l1 = None
119 c.l1 = [1, 1]
120 c.l1 = [1, 1, 1]
121 c.l1 = [1, 1, 1, 1]
122 c.l1 = [1, 1, 1, 1, 1]
124 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l2", [1, 2, None])
125 c.l2 = None
126 c.l2 = [1, 2, 3]
128 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l3", [0, 3, 2])
129 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l3", [1, 2, None])
130 c.l3 = None
131 c.l3 = [1, 1, 1]
133 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l4", [0, 3, 2])
134 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l4", [1, 2, None])
135 c.l4 = None
136 c.l4 = [1, 1, 1]
138 def testValidate(self):
139 c = Config1()
140 self.assertRaises(pexConfig.FieldValidationError, Config1.validate, c)
142 c.l4 = [1, 2, 3]
143 c.validate()
145 def testInPlaceModification(self):
146 c = Config1()
147 self.assertRaises(pexConfig.FieldValidationError, c.l1.__setitem__, 2, 0)
148 c.l1[2] = 10
149 self.assertEqual(c.l1, [1, 2, 10])
150 self.assertEqual((1, 2, 10), c.l1)
152 c.l1.insert(2, 20)
153 self.assertEqual(c.l1, [1, 2, 20, 10])
154 c.l1.append(30)
155 self.assertEqual(c.l1, [1, 2, 20, 10, 30])
156 c.l1.extend([4, 5, 6])
157 self.assertEqual(c.l1, [1, 2, 20, 10, 30, 4, 5, 6])
159 def testCastAndTypes(self):
160 c = Config2()
161 self.assertEqual(c.lf, [1.0, 2.0, 3.0])
163 c.lf[2] = 10
164 self.assertEqual(c.lf, [1.0, 2.0, 10.0])
166 c.ls.append("foo")
167 self.assertEqual(c.ls, ["hi", "foo"])
169 def testNoArbitraryAttributes(self):
170 c = Config1()
171 self.assertRaises(pexConfig.FieldValidationError, setattr, c.l1, "should", "fail")
173 def testNoPickle(self):
174 """Test that pickle support is disabled for the proxy container."""
175 c = Config2()
176 with self.assertRaises(pexConfig.UnexpectedProxyUsageError):
177 pickle.dumps(c.ls)
180if __name__ == "__main__": 180 ↛ 181line 180 didn't jump to line 181, because the condition on line 180 was never true
181 unittest.main()