Coverage for tests/test_dictField.py: 17%

104 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-02-14 02:09 -0800

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 

34class Config1(pexConfig.Config): 

35 d1 = pexConfig.DictField("d1", keytype=str, itemtype=int, default={"hi": 4}, itemCheck=lambda x: x > 0) 35 ↛ exitline 35 didn't run the lambda on line 35

36 d2 = pexConfig.DictField("d2", keytype=str, itemtype=str, default=None) 

37 d3 = pexConfig.DictField("d3", keytype=float, itemtype=float, optional=True, itemCheck=lambda x: x > 0) 37 ↛ exitline 37 didn't run the lambda on line 37

38 d4 = pexConfig.DictField("d4", keytype=str, itemtype=None, default={}) 

39 

40 

41class DictFieldTest(unittest.TestCase): 

42 def testConstructor(self): 

43 try: 

44 

45 class BadKeytype(pexConfig.Config): 

46 d = pexConfig.DictField("...", keytype=list, itemtype=int) 

47 

48 except Exception: 

49 pass 

50 else: 

51 raise SyntaxError("Unsupported keyptype DictFields should not be allowed") 

52 

53 try: 

54 

55 class BadItemtype(pexConfig.Config): 

56 d = pexConfig.DictField("...", keytype=int, itemtype=dict) 

57 

58 except Exception: 

59 pass 

60 else: 

61 raise SyntaxError("Unsupported itemtype DictFields should not be allowed") 

62 

63 try: 

64 

65 class BadItemCheck(pexConfig.Config): 

66 d = pexConfig.DictField("...", keytype=int, itemtype=int, itemCheck=4) 

67 

68 except Exception: 

69 pass 

70 else: 

71 raise SyntaxError("Non-callable itemCheck DictFields should not be allowed") 

72 

73 try: 

74 

75 class BadDictCheck(pexConfig.Config): 

76 d = pexConfig.DictField("...", keytype=int, itemtype=int, dictCheck=4) 

77 

78 except Exception: 

79 pass 

80 else: 

81 raise SyntaxError("Non-callable dictCheck DictFields should not be allowed") 

82 

83 def testFieldTypeAnnotationRuntime(self): 

84 # test parsing type annotation for runtime keytype, itemtype 

85 testField = pexConfig.DictField[str, int](doc="") 

86 self.assertEqual(testField.keytype, str) 

87 self.assertEqual(testField.itemtype, int) 

88 

89 # verify that forward references work correctly 

90 testField = pexConfig.DictField["float", "int"](doc="") 

91 self.assertEqual(testField.keytype, float) 

92 self.assertEqual(testField.itemtype, int) 

93 

94 # verify that Field rejects single types 

95 with self.assertRaises(ValueError): 

96 pexConfig.DictField[int](doc="") # type: ignore 

97 

98 # verify that Field raises in conflict with keytype, itemtype 

99 with self.assertRaises(ValueError): 

100 pexConfig.DictField[str, int](doc="", keytype=int) 

101 

102 with self.assertRaises(ValueError): 

103 pexConfig.DictField[str, int](doc="", itemtype=str) 

104 

105 # verify that Field does not raise if dtype agrees 

106 testField = pexConfig.DictField[int, str](doc="", keytype=int, itemtype=str) 

107 self.assertEqual(testField.keytype, int) 

108 self.assertEqual(testField.itemtype, str) 

109 

110 def testAssignment(self): 

111 c = Config1() 

112 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "d1", {3: 3}) 

113 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "d1", {"a": 0}) 

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

115 c.d1 = None 

116 c.d1 = {"a": 1, "b": 2} 

117 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "d3", {"hi": True}) 

118 c.d3 = {4: 5} 

119 self.assertEqual(c.d3, {4.0: 5.0}) 

120 d = {"a": None, "b": 4, "c": "foo"} 

121 c.d4 = d 

122 self.assertEqual(c.d4, d) 

123 c.d4["a"] = 12 

124 c.d4["b"] = "three" 

125 c.d4["c"] = None 

126 self.assertEqual(c.d4["a"], 12) 

127 self.assertEqual(c.d4["b"], "three") 

128 self.assertIsNone(c.d4["c"]) 

129 self.assertRaises(pexConfig.FieldValidationError, setattr, c, "d4", {"hi": [1, 2, 3]}) 

130 

131 def testValidate(self): 

132 c = Config1() 

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

134 

135 c.d2 = {"a": "b"} 

136 c.validate() 

137 

138 def testInPlaceModification(self): 

139 c = Config1() 

140 self.assertRaises(pexConfig.FieldValidationError, c.d1.__setitem__, 2, 0) 

141 self.assertRaises(pexConfig.FieldValidationError, c.d1.__setitem__, "hi", 0) 

142 c.d1["hi"] = 10 

143 self.assertEqual(c.d1, {"hi": 10}) 

144 

145 c.d3 = {} 

146 c.d3[4] = 5 

147 self.assertEqual(c.d3, {4.0: 5.0}) 

148 

149 def testNoArbitraryAttributes(self): 

150 c = Config1() 

151 self.assertRaises(pexConfig.FieldValidationError, setattr, c.d1, "should", "fail") 

152 

153 def testEquality(self): 

154 """Test DictField.__eq__ 

155 

156 We create two dicts, with the keys explicitly added in a different 

157 order and test their equality. 

158 """ 

159 keys1 = ["A", "B", "C"] 

160 keys2 = ["X", "Y", "Z", "a", "b", "c", "d", "e"] 

161 

162 c1 = Config1() 

163 c1.d4 = {k: "" for k in keys1} 

164 for k in keys2: 

165 c1.d4[k] = "" 

166 

167 c2 = Config1() 

168 for k in keys2 + keys1: 

169 c2.d4[k] = "" 

170 

171 self.assertTrue(pexConfig.compareConfigs("test", c1, c2)) 

172 

173 def testNoPickle(self): 

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

175 c = Config1() 

176 with self.assertRaises(pexConfig.UnexpectedProxyUsageError): 

177 pickle.dumps(c.d4) 

178 

179 

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

181 unittest.main()