Coverage for tests/test_configDictField.py: 26%

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

91 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 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 

38class Config2(pexConfig.Config): 

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

40 

41 

42class Config3(pexConfig.Config): 

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

44 

45 

46class ConfigDictFieldTest(unittest.TestCase): 

47 def testConstructor(self): 

48 try: 

49 

50 class BadKeytype(pexConfig.Config): 

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

52 

53 except Exception: 

54 pass 

55 else: 

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

57 

58 try: 

59 

60 class BadItemtype(pexConfig.Config): 

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

62 

63 except Exception: 

64 pass 

65 else: 

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

67 

68 try: 

69 

70 class BadItemCheck(pexConfig.Config): 

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

72 

73 except Exception: 

74 pass 

75 else: 

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

77 

78 try: 

79 

80 class BadDictCheck(pexConfig.Config): 

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

82 

83 except Exception: 

84 pass 

85 else: 

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

87 

88 def testAssignment(self): 

89 c = Config2() 

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

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

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

93 c.d1 = None 

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

95 

96 def testValidate(self): 

97 c = Config2() 

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

99 

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

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

102 

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

104 c.validate() 

105 

106 def testInPlaceModification(self): 

107 c = Config2(d1={}) 

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

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

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

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

112 

113 def testSave(self): 

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

115 c.save("configDictTest.py") 

116 

117 rt = Config2() 

118 rt.load("configDictTest.py") 

119 

120 os.remove("configDictTest.py") 

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

122 

123 c = Config2() 

124 c.save("emptyConfigDictTest.py") 

125 rt.load("emptyConfigDictTest.py") 

126 os.remove("emptyConfigDictTest.py") 

127 

128 self.assertIsNone(rt.d1) 

129 

130 def testToDict(self): 

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

132 dict_ = c.toDict() 

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

134 

135 def testFreeze(self): 

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

137 c.freeze() 

138 

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

140 

141 def testNoArbitraryAttributes(self): 

142 c = Config2(d1={}) 

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

144 

145 def testEquality(self): 

146 """Test ConfigDictField.__eq__ 

147 

148 We create two configs, with the keys explicitly added in a different 

149 order and test their equality. 

150 """ 

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

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

153 

154 c1 = Config3() 

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

156 for k in keys2: 

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

158 

159 c2 = Config3() 

160 for k in keys2 + keys1: 

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

162 

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

164 

165 

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

167 unittest.main()