Coverage for tests/test_configDictField.py: 22%

95 statements  

« prev     ^ index     » next       coverage.py v7.2.5, created at 2023-05-11 02:59 -0700

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 os 

29import unittest 

30 

31import lsst.pex.config as pexConfig 

32 

33 

34class Config1(pexConfig.Config): 

35 f = pexConfig.Field("f", float, default=3.0) 

36 

37 def _collectImports(self): 

38 # Exists to test that imports of dict values are collected 

39 self._imports.add("builtins") 

40 

41 

42class Config2(pexConfig.Config): 

43 d1 = pexConfig.ConfigDictField("d1", keytype=str, itemtype=Config1, itemCheck=lambda x: x.f > 0) 43 ↛ exitline 43 didn't run the lambda on line 43

44 

45 

46class Config3(pexConfig.Config): 

47 field1 = pexConfig.ConfigDictField(keytype=str, itemtype=pexConfig.Config, default={}, doc="doc") 

48 

49 

50class ConfigDictFieldTest(unittest.TestCase): 

51 def testConstructor(self): 

52 try: 

53 

54 class BadKeytype(pexConfig.Config): 

55 d = pexConfig.ConfigDictField("...", keytype=list, itemtype=Config1) 

56 

57 except Exception: 

58 pass 

59 else: 

60 raise SyntaxError("Unsupported keytypes should not be allowed") 

61 

62 try: 

63 

64 class BadItemtype(pexConfig.Config): 

65 d = pexConfig.ConfigDictField("...", keytype=int, itemtype=dict) 

66 

67 except Exception: 

68 pass 

69 else: 

70 raise SyntaxError("Unsupported itemtypes should not be allowed") 

71 

72 try: 

73 

74 class BadItemCheck(pexConfig.Config): 

75 d = pexConfig.ConfigDictField("...", keytype=str, itemtype=Config1, itemCheck=4) 

76 

77 except Exception: 

78 pass 

79 else: 

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

81 

82 try: 

83 

84 class BadDictCheck(pexConfig.Config): 

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

86 

87 except Exception: 

88 pass 

89 else: 

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

91 

92 def testAssignment(self): 

93 c = Config2() 

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

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

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

97 c.d1 = None 

98 c.d1 = {"a": Config1, "b": Config1()} 

99 

100 def testValidate(self): 

101 c = Config2() 

102 self.assertRaises(pexConfig.FieldValidationError, Config2.validate, c) 

103 

104 c.d1 = {"a": Config1(f=0)} 

105 self.assertRaises(pexConfig.FieldValidationError, Config2.validate, c) 

106 

107 c.d1["a"].f = 5 

108 c.validate() 

109 

110 def testInPlaceModification(self): 

111 c = Config2(d1={}) 

112 self.assertRaises(pexConfig.FieldValidationError, c.d1.__setitem__, 1, 0) 

113 self.assertRaises(pexConfig.FieldValidationError, c.d1.__setitem__, "a", 0) 

114 c.d1["a"] = Config1(f=4) 

115 self.assertEqual(c.d1["a"].f, 4) 

116 

117 def testSave(self): 

118 c = Config2(d1={"a": Config1(f=4)}) 

119 c.save("configDictTest.py") 

120 

121 # verify _collectImports is called on all the configDictValues 

122 stringOutput = c.saveToString() 

123 self.assertIn("import builtins", stringOutput) 

124 

125 rt = Config2() 

126 rt.load("configDictTest.py") 

127 

128 os.remove("configDictTest.py") 

129 self.assertEqual(rt.d1["a"].f, c.d1["a"].f) 

130 

131 c = Config2() 

132 c.save("emptyConfigDictTest.py") 

133 rt.load("emptyConfigDictTest.py") 

134 os.remove("emptyConfigDictTest.py") 

135 

136 self.assertIsNone(rt.d1) 

137 

138 def testToDict(self): 

139 c = Config2(d1={"a": Config1(f=4), "b": Config1}) 

140 dict_ = c.toDict() 

141 self.assertEqual(dict_, {"d1": {"a": {"f": 4.0}, "b": {"f": 3.0}}}) 

142 

143 def testFreeze(self): 

144 c = Config2(d1={"a": Config1(f=4), "b": Config1}) 

145 c.freeze() 

146 

147 self.assertRaises(pexConfig.FieldValidationError, setattr, c.d1["a"], "f", 0) 

148 

149 def testNoArbitraryAttributes(self): 

150 c = Config2(d1={}) 

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

152 

153 def testEquality(self): 

154 """Test ConfigDictField.__eq__ 

155 

156 We create two configs, 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 = Config3() 

163 c1.field1 = {k: pexConfig.Config() for k in keys1} 

164 for k in keys2: 

165 c1.field1[k] = pexConfig.Config() 

166 

167 c2 = Config3() 

168 for k in keys2 + keys1: 

169 c2.field1[k] = pexConfig.Config() 

170 

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

172 

173 

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

175 unittest.main()