Coverage for tests/test_listField.py: 20%
103 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-03-28 09:11 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-03-28 09:11 +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 if len(theList) <= 1:
36 return True
38 p = theList[0]
39 for x in theList[1:]:
40 if x < p:
41 return False
42 p = x
43 return True
46def isPositive(x):
47 return x > 0
50class Config1(pexConfig.Config):
51 l1 = pexConfig.ListField("l1", int, minLength=2, maxLength=5, default=[1, 2, 3], itemCheck=isPositive)
52 l2 = pexConfig.ListField("l2", int, length=3, default=[1, 2, 3], listCheck=isSorted, itemCheck=isPositive)
53 l3 = pexConfig.ListField("l3", int, length=3, default=None, optional=True, itemCheck=isPositive)
54 l4 = pexConfig.ListField("l4", int, length=3, default=None, itemCheck=isPositive)
57class Config2(pexConfig.Config):
58 lf = pexConfig.ListField("lf", float, default=[1, 2, 3])
59 ls = pexConfig.ListField("ls", str, default=["hi"])
62class ListFieldTest(unittest.TestCase):
63 def testConstructor(self):
64 try:
66 class BadDtype(pexConfig.Config):
67 ll = pexConfig.ListField("...", list)
69 except Exception:
70 pass
71 else:
72 raise SyntaxError("Unsupported dtype ListFields should not be allowed")
74 try:
76 class BadLengths(pexConfig.Config):
77 ll = pexConfig.ListField("...", int, minLength=4, maxLength=2)
79 except ValueError:
80 pass
81 else:
82 raise SyntaxError("minLnegth <= maxLength should not be allowed")
84 try:
86 class BadLength(pexConfig.Config):
87 ll = pexConfig.ListField("...", int, length=-1)
89 except Exception:
90 pass
91 else:
92 raise SyntaxError("negative length should not be allowed")
94 try:
96 class BadLength2(pexConfig.Config):
97 ll = pexConfig.ListField("...", int, maxLength=-1)
99 except Exception:
100 pass
101 else:
102 raise SyntaxError("negative max length should not be allowed")
104 def testAssignment(self):
105 c = Config1()
106 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l1", [1.2, 3, 4])
107 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l1", [-1, -2, -3])
108 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l1", [1, 2, 0])
109 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l1", [1, 2, None])
110 c.l1 = None
111 c.l1 = [1, 1]
112 c.l1 = [1, 1, 1]
113 c.l1 = [1, 1, 1, 1]
114 c.l1 = [1, 1, 1, 1, 1]
116 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l2", [1, 2, None])
117 c.l2 = None
118 c.l2 = [1, 2, 3]
120 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l3", [0, 3, 2])
121 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l3", [1, 2, None])
122 c.l3 = None
123 c.l3 = [1, 1, 1]
125 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l4", [0, 3, 2])
126 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l4", [1, 2, None])
127 c.l4 = None
128 c.l4 = [1, 1, 1]
130 def testValidate(self):
131 c = Config1()
132 self.assertRaises(pexConfig.FieldValidationError, Config1.validate, c)
134 c.l4 = [1, 2, 3]
135 c.validate()
137 def testInPlaceModification(self):
138 c = Config1()
139 self.assertRaises(pexConfig.FieldValidationError, c.l1.__setitem__, 2, 0)
140 c.l1[2] = 10
141 self.assertEqual(c.l1, [1, 2, 10])
142 self.assertEqual((1, 2, 10), c.l1)
144 c.l1.insert(2, 20)
145 self.assertEqual(c.l1, [1, 2, 20, 10])
146 c.l1.append(30)
147 self.assertEqual(c.l1, [1, 2, 20, 10, 30])
148 c.l1.extend([4, 5, 6])
149 self.assertEqual(c.l1, [1, 2, 20, 10, 30, 4, 5, 6])
151 def testCastAndTypes(self):
152 c = Config2()
153 self.assertEqual(c.lf, [1.0, 2.0, 3.0])
155 c.lf[2] = 10
156 self.assertEqual(c.lf, [1.0, 2.0, 10.0])
158 c.ls.append("foo")
159 self.assertEqual(c.ls, ["hi", "foo"])
161 def testNoArbitraryAttributes(self):
162 c = Config1()
163 self.assertRaises(pexConfig.FieldValidationError, setattr, c.l1, "should", "fail")
165 def testNoPickle(self):
166 """Test that pickle support is disabled for the proxy container."""
167 c = Config2()
168 with self.assertRaises(pexConfig.UnexpectedProxyUsageError):
169 pickle.dumps(c.ls)
172if __name__ == "__main__": 172 ↛ 173line 172 didn't jump to line 173, because the condition on line 172 was never true
173 unittest.main()