Coverage for tests/test_listField.py : 20%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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 unittest
29import lsst.pex.config as pexConfig
32def isSorted(theList):
33 if len(theList) <= 1:
34 return True
36 p = theList[0]
37 for x in theList[1:]:
38 if x < p:
39 return False
40 p = x
41 return True
44def isPositive(x):
45 return x > 0
48class Config1(pexConfig.Config):
49 l1 = pexConfig.ListField("l1", int, minLength=2, maxLength=5, default=[1, 2, 3], itemCheck=isPositive)
50 l2 = pexConfig.ListField("l2", int, length=3, default=[1, 2, 3], listCheck=isSorted,
51 itemCheck=isPositive)
52 l3 = pexConfig.ListField("l3", int, length=3, default=None, optional=True, itemCheck=isPositive)
53 l4 = pexConfig.ListField("l4", int, length=3, default=None, itemCheck=isPositive)
56class Config2(pexConfig.Config):
57 lf = pexConfig.ListField("lf", float, default=[1, 2, 3])
58 ls = pexConfig.ListField("ls", str, default=["hi"])
61class ListFieldTest(unittest.TestCase):
62 def testConstructor(self):
63 try:
64 class BadDtype(pexConfig.Config):
65 ll = pexConfig.ListField("...", list)
66 except Exception:
67 pass
68 else:
69 raise SyntaxError("Unsupported dtype ListFields should not be allowed")
71 try:
72 class BadLengths(pexConfig.Config):
73 ll = pexConfig.ListField("...", int, minLength=4, maxLength=2)
74 except ValueError:
75 pass
76 else:
77 raise SyntaxError("minLnegth <= maxLength should not be allowed")
79 try:
80 class BadLength(pexConfig.Config):
81 ll = pexConfig.ListField("...", int, length=-1)
82 except Exception:
83 pass
84 else:
85 raise SyntaxError("negative length should not be allowed")
87 try:
88 class BadLength2(pexConfig.Config):
89 ll = pexConfig.ListField("...", int, maxLength=-1)
90 except Exception:
91 pass
92 else:
93 raise SyntaxError("negative max length should not be allowed")
95 def testAssignment(self):
96 c = Config1()
97 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l1", [1.2, 3, 4])
98 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l1", [-1, -2, -3])
99 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l1", [1, 2, 0])
100 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l1", [1, 2, None])
101 c.l1 = None
102 c.l1 = [1, 1]
103 c.l1 = [1, 1, 1]
104 c.l1 = [1, 1, 1, 1]
105 c.l1 = [1, 1, 1, 1, 1]
107 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l2", [1, 2, None])
108 c.l2 = None
109 c.l2 = [1, 2, 3]
111 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l3", [0, 3, 2])
112 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l3", [1, 2, None])
113 c.l3 = None
114 c.l3 = [1, 1, 1]
116 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l4", [0, 3, 2])
117 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l4", [1, 2, None])
118 c.l4 = None
119 c.l4 = [1, 1, 1]
121 def testValidate(self):
122 c = Config1()
123 self.assertRaises(pexConfig.FieldValidationError, Config1.validate, c)
125 c.l4 = [1, 2, 3]
126 c.validate()
128 def testInPlaceModification(self):
129 c = Config1()
130 self.assertRaises(pexConfig.FieldValidationError, c.l1.__setitem__, 2, 0)
131 c.l1[2] = 10
132 self.assertEqual(c.l1, [1, 2, 10])
133 self.assertEqual((1, 2, 10), c.l1)
135 c.l1.insert(2, 20)
136 self.assertEqual(c.l1, [1, 2, 20, 10])
137 c.l1.append(30)
138 self.assertEqual(c.l1, [1, 2, 20, 10, 30])
139 c.l1.extend([4, 5, 6])
140 self.assertEqual(c.l1, [1, 2, 20, 10, 30, 4, 5, 6])
142 def testCastAndTypes(self):
143 c = Config2()
144 self.assertEqual(c.lf, [1., 2., 3.])
146 c.lf[2] = 10
147 self.assertEqual(c.lf, [1., 2., 10.])
149 c.ls.append("foo")
150 self.assertEqual(c.ls, ["hi", "foo"])
152 def testNoArbitraryAttributes(self):
153 c = Config1()
154 self.assertRaises(pexConfig.FieldValidationError, setattr, c.l1, "should", "fail")
157if __name__ == "__main__": 157 ↛ 158line 157 didn't jump to line 158, because the condition on line 157 was never true
158 unittest.main()