Coverage for tests/test_dictField.py: 21%

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

83 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 

33class Config1(pexConfig.Config): 

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

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

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

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

38 

39 

40class DictFieldTest(unittest.TestCase): 

41 def testConstructor(self): 

42 try: 

43 

44 class BadKeytype(pexConfig.Config): 

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

46 

47 except Exception: 

48 pass 

49 else: 

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

51 

52 try: 

53 

54 class BadItemtype(pexConfig.Config): 

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

56 

57 except Exception: 

58 pass 

59 else: 

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

61 

62 try: 

63 

64 class BadItemCheck(pexConfig.Config): 

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

66 

67 except Exception: 

68 pass 

69 else: 

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

71 

72 try: 

73 

74 class BadDictCheck(pexConfig.Config): 

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

76 

77 except Exception: 

78 pass 

79 else: 

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

81 

82 def testAssignment(self): 

83 c = Config1() 

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

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

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

87 c.d1 = None 

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

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

90 c.d3 = {4: 5} 

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

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

93 c.d4 = d 

94 self.assertEqual(c.d4, d) 

95 c.d4["a"] = 12 

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

97 c.d4["c"] = None 

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

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

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

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

102 

103 def testValidate(self): 

104 c = Config1() 

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

106 

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

108 c.validate() 

109 

110 def testInPlaceModification(self): 

111 c = Config1() 

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

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

114 c.d1["hi"] = 10 

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

116 

117 c.d3 = {} 

118 c.d3[4] = 5 

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

120 

121 def testNoArbitraryAttributes(self): 

122 c = Config1() 

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

124 

125 def testEquality(self): 

126 """Test DictField.__eq__ 

127 

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

129 order and test their equality. 

130 """ 

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

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

133 

134 c1 = Config1() 

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

136 for k in keys2: 

137 c1.d4[k] = "" 

138 

139 c2 = Config1() 

140 for k in keys2 + keys1: 

141 c2.d4[k] = "" 

142 

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

144 

145 

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

147 unittest.main()