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 

30import lsst.pex.config as pexConfig 

31 

32 

33class Config1(pexConfig.Config): 

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

35 

36 

37class Config2(pexConfig.Config): 

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

39 

40 

41class Config3(pexConfig.Config): 

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

43 

44 

45class ConfigDictFieldTest(unittest.TestCase): 

46 def testConstructor(self): 

47 try: 

48 class BadKeytype(pexConfig.Config): 

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

50 except Exception: 

51 pass 

52 else: 

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

54 

55 try: 

56 class BadItemtype(pexConfig.Config): 

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

58 except Exception: 

59 pass 

60 else: 

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

62 

63 try: 

64 class BadItemCheck(pexConfig.Config): 

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

66 except Exception: 

67 pass 

68 else: 

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

70 

71 try: 

72 class BadDictCheck(pexConfig.Config): 

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

74 except Exception: 

75 pass 

76 else: 

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

78 

79 def testAssignment(self): 

80 c = Config2() 

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

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

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

84 c.d1 = None 

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

86 

87 def testValidate(self): 

88 c = Config2() 

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

90 

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

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

93 

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

95 c.validate() 

96 

97 def testInPlaceModification(self): 

98 c = Config2(d1={}) 

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

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

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

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

103 

104 def testSave(self): 

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

106 c.save("configDictTest.py") 

107 

108 rt = Config2() 

109 rt.load("configDictTest.py") 

110 

111 os.remove("configDictTest.py") 

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

113 

114 c = Config2() 

115 c.save("emptyConfigDictTest.py") 

116 rt.load("emptyConfigDictTest.py") 

117 os.remove("emptyConfigDictTest.py") 

118 

119 self.assertIsNone(rt.d1) 

120 

121 def testToDict(self): 

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

123 dict_ = c.toDict() 

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

125 

126 def testFreeze(self): 

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

128 c.freeze() 

129 

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

131 

132 def testNoArbitraryAttributes(self): 

133 c = Config2(d1={}) 

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

135 

136 def testEquality(self): 

137 """Test ConfigDictField.__eq__ 

138 

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

140 order and test their equality. 

141 """ 

142 keys1 = ['A', 'B', 'C'] 

143 keys2 = ['X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e'] 

144 

145 c1 = Config3() 

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

147 for k in keys2: 

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

149 

150 c2 = Config3() 

151 for k in keys2 + keys1: 

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

153 

154 self.assertTrue(pexConfig.compareConfigs('test', c1, c2)) 

155 

156 

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

158 unittest.main()