Coverage for tests/test_dictField.py: 19%

83 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-12-01 19:59 +0000

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 

29import lsst.pex.config as pexConfig 

30 

31 

32class Config1(pexConfig.Config): 

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

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

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

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

37 

38 

39class DictFieldTest(unittest.TestCase): 

40 def testConstructor(self): 

41 try: 

42 class BadKeytype(pexConfig.Config): 

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

44 except Exception: 

45 pass 

46 else: 

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

48 

49 try: 

50 class BadItemtype(pexConfig.Config): 

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

52 except Exception: 

53 pass 

54 else: 

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

56 

57 try: 

58 class BadItemCheck(pexConfig.Config): 

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

60 except Exception: 

61 pass 

62 else: 

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

64 

65 try: 

66 class BadDictCheck(pexConfig.Config): 

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

68 except Exception: 

69 pass 

70 else: 

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

72 

73 def testAssignment(self): 

74 c = Config1() 

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

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

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

78 c.d1 = None 

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

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

81 c.d3 = {4: 5} 

82 self.assertEqual(c.d3, {4.: 5.}) 

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

84 c.d4 = d 

85 self.assertEqual(c.d4, d) 

86 c.d4["a"] = 12 

87 c.d4[u"b"] = "three" 

88 c.d4["c"] = None 

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

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

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

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

93 

94 def testValidate(self): 

95 c = Config1() 

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

97 

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

99 c.validate() 

100 

101 def testInPlaceModification(self): 

102 c = Config1() 

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

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

105 c.d1["hi"] = 10 

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

107 

108 c.d3 = {} 

109 c.d3[4] = 5 

110 self.assertEqual(c.d3, {4.: 5.}) 

111 

112 def testNoArbitraryAttributes(self): 

113 c = Config1() 

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

115 

116 def testEquality(self): 

117 """Test DictField.__eq__ 

118 

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

120 order and test their equality. 

121 """ 

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

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

124 

125 c1 = Config1() 

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

127 for k in keys2: 

128 c1.d4[k] = "" 

129 

130 c2 = Config1() 

131 for k in keys2 + keys1: 

132 c2.d4[k] = "" 

133 

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

135 

136 

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

138 unittest.main()