Coverage for tests/test_listField.py: 22%

Shortcuts 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

98 statements  

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 unittest 

29 

30import lsst.pex.config as pexConfig 

31 

32 

33def isSorted(theList): 

34 if len(theList) <= 1: 

35 return True 

36 

37 p = theList[0] 

38 for x in theList[1:]: 

39 if x < p: 

40 return False 

41 p = x 

42 return True 

43 

44 

45def isPositive(x): 

46 return x > 0 

47 

48 

49class Config1(pexConfig.Config): 

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

51 l2 = pexConfig.ListField("l2", int, length=3, default=[1, 2, 3], listCheck=isSorted, 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) 

54 

55 

56class Config2(pexConfig.Config): 

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

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

59 

60 

61class ListFieldTest(unittest.TestCase): 

62 def testConstructor(self): 

63 try: 

64 

65 class BadDtype(pexConfig.Config): 

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

67 

68 except Exception: 

69 pass 

70 else: 

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

72 

73 try: 

74 

75 class BadLengths(pexConfig.Config): 

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

77 

78 except ValueError: 

79 pass 

80 else: 

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

82 

83 try: 

84 

85 class BadLength(pexConfig.Config): 

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

87 

88 except Exception: 

89 pass 

90 else: 

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

92 

93 try: 

94 

95 class BadLength2(pexConfig.Config): 

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

97 

98 except Exception: 

99 pass 

100 else: 

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

102 

103 def testAssignment(self): 

104 c = Config1() 

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

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

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

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

109 c.l1 = None 

110 c.l1 = [1, 1] 

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

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

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

114 

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

116 c.l2 = None 

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

118 

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

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

121 c.l3 = None 

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

123 

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

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

126 c.l4 = None 

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

128 

129 def testValidate(self): 

130 c = Config1() 

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

132 

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

134 c.validate() 

135 

136 def testInPlaceModification(self): 

137 c = Config1() 

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

139 c.l1[2] = 10 

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

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

142 

143 c.l1.insert(2, 20) 

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

145 c.l1.append(30) 

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

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

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

149 

150 def testCastAndTypes(self): 

151 c = Config2() 

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

153 

154 c.lf[2] = 10 

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

156 

157 c.ls.append("foo") 

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

159 

160 def testNoArbitraryAttributes(self): 

161 c = Config1() 

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

163 

164 

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

166 unittest.main()