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

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 if len(theList) <= 1: 

36 return True 

37 

38 p = theList[0] 

39 for x in theList[1:]: 

40 if x < p: 

41 return False 

42 p = x 

43 return True 

44 

45 

46def isPositive(x): 

47 return x > 0 

48 

49 

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) 

55 

56 

57class Config2(pexConfig.Config): 

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

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

60 

61 

62class ListFieldTest(unittest.TestCase): 

63 def testConstructor(self): 

64 try: 

65 

66 class BadDtype(pexConfig.Config): 

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

68 

69 except Exception: 

70 pass 

71 else: 

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

73 

74 try: 

75 

76 class BadLengths(pexConfig.Config): 

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

78 

79 except ValueError: 

80 pass 

81 else: 

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

83 

84 try: 

85 

86 class BadLength(pexConfig.Config): 

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

88 

89 except Exception: 

90 pass 

91 else: 

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

93 

94 try: 

95 

96 class BadLength2(pexConfig.Config): 

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

98 

99 except Exception: 

100 pass 

101 else: 

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

103 

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] 

115 

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

117 c.l2 = None 

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

119 

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] 

124 

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] 

129 

130 def testValidate(self): 

131 c = Config1() 

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

133 

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

135 c.validate() 

136 

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) 

143 

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]) 

150 

151 def testCastAndTypes(self): 

152 c = Config2() 

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

154 

155 c.lf[2] = 10 

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

157 

158 c.ls.append("foo") 

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

160 

161 def testNoArbitraryAttributes(self): 

162 c = Config1() 

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

164 

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) 

170 

171 

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

173 unittest.main()