Coverage for tests/test_listField.py: 20%

103 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-03-01 12:22 +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/>. 

27 

28import pickle 

29import unittest 

30 

31import lsst.pex.config as pexConfig 

32 

33 

34def isSorted(theList): 

35 """Determine if a list is sorted. 

36 

37 Parameters 

38 ---------- 

39 theList : `list` 

40 The list to check. 

41 """ 

42 if len(theList) <= 1: 

43 return True 

44 

45 p = theList[0] 

46 for x in theList[1:]: 

47 if x < p: 

48 return False 

49 p = x 

50 return True 

51 

52 

53def isPositive(x): 

54 """Determine if an integer is positive. 

55 

56 Parameters 

57 ---------- 

58 x : `int` 

59 The integer to check. 

60 """ 

61 return x > 0 

62 

63 

64class Config1(pexConfig.Config): 

65 """First test config.""" 

66 

67 l1 = pexConfig.ListField("l1", int, minLength=2, maxLength=5, default=[1, 2, 3], itemCheck=isPositive) 

68 l2 = pexConfig.ListField("l2", int, length=3, default=[1, 2, 3], listCheck=isSorted, itemCheck=isPositive) 

69 l3 = pexConfig.ListField("l3", int, length=3, default=None, optional=True, itemCheck=isPositive) 

70 l4 = pexConfig.ListField("l4", int, length=3, default=None, itemCheck=isPositive) 

71 

72 

73class Config2(pexConfig.Config): 

74 """Second test config.""" 

75 

76 lf = pexConfig.ListField("lf", float, default=[1, 2, 3]) 

77 ls = pexConfig.ListField("ls", str, default=["hi"]) 

78 

79 

80class ListFieldTest(unittest.TestCase): 

81 """Test ListField.""" 

82 

83 def testConstructor(self): 

84 try: 

85 

86 class BadDtype(pexConfig.Config): 

87 ll = pexConfig.ListField("...", list) 

88 

89 except Exception: 

90 pass 

91 else: 

92 raise SyntaxError("Unsupported dtype ListFields should not be allowed") 

93 

94 try: 

95 

96 class BadLengths(pexConfig.Config): 

97 ll = pexConfig.ListField("...", int, minLength=4, maxLength=2) 

98 

99 except ValueError: 

100 pass 

101 else: 

102 raise SyntaxError("minLnegth <= maxLength should not be allowed") 

103 

104 try: 

105 

106 class BadLength(pexConfig.Config): 

107 ll = pexConfig.ListField("...", int, length=-1) 

108 

109 except Exception: 

110 pass 

111 else: 

112 raise SyntaxError("negative length should not be allowed") 

113 

114 try: 

115 

116 class BadLength2(pexConfig.Config): 

117 ll = pexConfig.ListField("...", int, maxLength=-1) 

118 

119 except Exception: 

120 pass 

121 else: 

122 raise SyntaxError("negative max length should not be allowed") 

123 

124 def testAssignment(self): 

125 c = Config1() 

126 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l1", [1.2, 3, 4]) 

127 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l1", [-1, -2, -3]) 

128 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l1", [1, 2, 0]) 

129 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l1", [1, 2, None]) 

130 c.l1 = None 

131 c.l1 = [1, 1] 

132 c.l1 = [1, 1, 1] 

133 c.l1 = [1, 1, 1, 1] 

134 c.l1 = [1, 1, 1, 1, 1] 

135 

136 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l2", [1, 2, None]) 

137 c.l2 = None 

138 c.l2 = [1, 2, 3] 

139 

140 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l3", [0, 3, 2]) 

141 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l3", [1, 2, None]) 

142 c.l3 = None 

143 c.l3 = [1, 1, 1] 

144 

145 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l4", [0, 3, 2]) 

146 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "l4", [1, 2, None]) 

147 c.l4 = None 

148 c.l4 = [1, 1, 1] 

149 

150 def testValidate(self): 

151 c = Config1() 

152 self.assertRaises(pexConfig.FieldValidationError, Config1.validate, c) 

153 

154 c.l4 = [1, 2, 3] 

155 c.validate() 

156 

157 def testInPlaceModification(self): 

158 c = Config1() 

159 self.assertRaises(pexConfig.FieldValidationError, c.l1.__setitem__, 2, 0) 

160 c.l1[2] = 10 

161 self.assertEqual(c.l1, [1, 2, 10]) 

162 self.assertEqual((1, 2, 10), c.l1) 

163 

164 c.l1.insert(2, 20) 

165 self.assertEqual(c.l1, [1, 2, 20, 10]) 

166 c.l1.append(30) 

167 self.assertEqual(c.l1, [1, 2, 20, 10, 30]) 

168 c.l1.extend([4, 5, 6]) 

169 self.assertEqual(c.l1, [1, 2, 20, 10, 30, 4, 5, 6]) 

170 

171 def testCastAndTypes(self): 

172 c = Config2() 

173 self.assertEqual(c.lf, [1.0, 2.0, 3.0]) 

174 

175 c.lf[2] = 10 

176 self.assertEqual(c.lf, [1.0, 2.0, 10.0]) 

177 

178 c.ls.append("foo") 

179 self.assertEqual(c.ls, ["hi", "foo"]) 

180 

181 def testNoArbitraryAttributes(self): 

182 c = Config1() 

183 self.assertRaises(pexConfig.FieldValidationError, setattr, c.l1, "should", "fail") 

184 

185 def testNoPickle(self): 

186 """Test that pickle support is disabled for the proxy container.""" 

187 c = Config2() 

188 with self.assertRaises(pexConfig.UnexpectedProxyUsageError): 

189 pickle.dumps(c.ls) 

190 

191 

192if __name__ == "__main__": 192 ↛ 193line 192 didn't jump to line 193, because the condition on line 192 was never true

193 unittest.main()