Coverage for tests/test_configChoiceField.py: 31%

80 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 os 

29import unittest 

30import lsst.pex.config as pexConfig 

31 

32 

33class Config1(pexConfig.Config): 

34 f = pexConfig.Field(doc="Config1.f", dtype=int, default=4) 

35 

36 def validate(self): 

37 pexConfig.Config.validate(self) 

38 if self.f <= 0: 

39 raise pexConfig.FieldValidationError(Config1.f, self, "f should be > 0") 

40 

41 

42class Config2(pexConfig.Config): 

43 f = pexConfig.Field(doc="Config2.f", dtype=float, default=0.5, check=lambda x: x > 0) 43 ↛ exitline 43 didn't run the lambda on line 43

44 

45 

46TYPEMAP = {"AAA": Config1, "BBB": Config2, "CCC": Config1} 

47 

48 

49class Config3(pexConfig.Config): 

50 a = pexConfig.ConfigChoiceField(doc="single non-optional", 

51 typemap=TYPEMAP, 

52 default="AAA", multi=False, optional=False) 

53 b = pexConfig.ConfigChoiceField(doc="single optional", 

54 typemap=TYPEMAP, 

55 default="AAA", multi=False, optional=True) 

56 c = pexConfig.ConfigChoiceField(doc="multi non-optional", 

57 typemap=TYPEMAP, 

58 default=["AAA"], multi=True, optional=False) 

59 d = pexConfig.ConfigChoiceField(doc="multi optional", 

60 typemap=TYPEMAP, 

61 default=["AAA"], multi=True, optional=True) 

62 

63 

64class ConfigChoiceFieldTest(unittest.TestCase): 

65 def setUp(self): 

66 self.config = Config3() 

67 

68 def tearDown(self): 

69 del self.config 

70 

71 def testInit(self): 

72 self.assertEqual(self.config.a.name, "AAA") 

73 self.assertEqual(self.config.a.active.f, 4) 

74 self.assertEqual(self.config.a["AAA"].f, 4) 

75 self.assertEqual(self.config.a["BBB"].f, 0.5) 

76 

77 def testSave(self): 

78 self.config.a["AAA"].f = 1 

79 self.config.a["BBB"].f = 1.0 

80 self.config.a = "BBB" 

81 path = "choiceFieldTest.config" 

82 self.config.save(path) 

83 roundtrip = Config3() 

84 roundtrip.load(path) 

85 os.remove(path) 

86 

87 self.assertEqual(self.config.a.name, roundtrip.a.name) 

88 self.assertEqual(self.config.a["AAA"].f, roundtrip.a["AAA"].f) 

89 self.assertEqual(self.config.a["BBB"].f, roundtrip.a["BBB"].f) 

90 

91 def testValidate(self): 

92 self.config.validate() 

93 self.config.a = "AAA" 

94 self.config.a["AAA"].f = 0 

95 

96 self.assertRaises(pexConfig.FieldValidationError, self.config.validate) 

97 

98 self.config.a = "BBB" 

99 self.config.validate() 

100 

101 self.config.a = None 

102 self.assertRaises(pexConfig.FieldValidationError, self.config.validate) 

103 

104 def testFreeze(self): 

105 self.config.freeze() 

106 self.assertRaises(pexConfig.FieldValidationError, setattr, self.config.a, "name", "AAA") 

107 self.assertRaises(pexConfig.FieldValidationError, setattr, self.config.a["AAA"], "f", "1") 

108 

109 # Create a new unfrozen config 

110 unfrozenConfig = Config3() 

111 

112 # Add a new entries to the typemap after the config is frozen and check 

113 # that it is not in the frozen configs keys 

114 TYPEMAP["DDD"] = Config1 

115 self.assertNotIn("DDD", self.config.a.keys()) 

116 

117 # Verify that the entry added to the typemap does show up in the 

118 # unfrozen config 

119 self.assertIn("DDD", unfrozenConfig.a.keys()) 

120 

121 def testNoArbitraryAttributes(self): 

122 self.assertRaises(pexConfig.FieldValidationError, setattr, self.config.a, "should", "fail") 

123 

124 def testSelectionSet(self): 

125 # test in place modification 

126 self.config.c.names.add("BBB") 

127 self.assertEqual(set(self.config.c.names), set(["AAA", "BBB"])) 

128 self.config.c.names.remove("AAA") 

129 self.assertEqual(set(self.config.c.names), set(["BBB"])) 

130 self.assertRaises(KeyError, self.config.c.names.remove, "AAA") 

131 self.config.c.names.discard("AAA") 

132 

133 # test bad assignment 

134 self.assertRaises(pexConfig.FieldValidationError, 

135 setattr, self.config.c, "names", "AAA") 

136 self.config.c.names = ["AAA"] 

137 

138 def testNoneValue(self): 

139 self.config.a = None 

140 self.assertRaises(pexConfig.FieldValidationError, self.config.validate) 

141 self.config.a = "AAA" 

142 self.config.b = None 

143 self.config.validate() 

144 self.config.c = None 

145 self.assertRaises(pexConfig.FieldValidationError, self.config.validate) 

146 self.config.c = ["AAA"] 

147 self.config.d = None 

148 self.config.validate() 

149 

150 

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

152 unittest.main()