Coverage for tests/test_configurableField.py: 25%

85 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-06 09:49 +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 pickle 

29import tempfile 

30import unittest 

31 

32import lsst.pex.config as pexConf 

33 

34 

35class Config1(pexConf.Config): 

36 """First test config.""" 

37 

38 f = pexConf.Field("f", dtype=float, default=5, check=lambda x: x > 0) 

39 

40 

41class Target1: 

42 """First target class.""" 

43 

44 ConfigClass = Config1 

45 

46 def __init__(self, config): 

47 self.f = config.f 

48 

49 

50def Target2(config): 

51 """Second target class.""" 

52 return config.f 

53 

54 

55class Config2(pexConf.Config): 

56 """Second test config.""" 

57 

58 c1 = pexConf.ConfigurableField("c1", target=Target1) 

59 c2 = pexConf.ConfigurableField("c2", target=Target2, ConfigClass=Config1, default=Config1(f=3)) 

60 

61 

62class ConfigurableFieldTest(unittest.TestCase): 

63 """Test of ConfigurableField.""" 

64 

65 def testConstructor(self): 

66 try: 

67 

68 class BadTarget(pexConf.Config): 

69 d = pexConf.ConfigurableField("...", target=None) 

70 

71 except Exception: 

72 pass 

73 else: 

74 raise SyntaxError("Uncallable targets should not be allowed") 

75 

76 try: 

77 

78 class NoConfigClass(pexConf.Config): 

79 d = pexConf.ConfigurableField("...", target=Target2) 

80 

81 except Exception: 

82 pass 

83 else: 

84 raise SyntaxError("Missing ConfigClass should not be allowed") 

85 

86 try: 

87 

88 class BadConfigClass(pexConf.Config): 

89 d = pexConf.DictField("...", target=Target2, ConfigClass=Target2) 

90 

91 except Exception: 

92 pass 

93 else: 

94 raise SyntaxError("ConfigClass that are not subclasses of Config should not be allowed") 

95 

96 def testBasics(self): 

97 c = Config2() 

98 self.assertEqual(c.c1.f, 5) 

99 self.assertEqual(c.c2.f, 3) 

100 

101 self.assertEqual(type(c.c1.apply()), Target1) 

102 self.assertEqual(c.c1.apply().f, 5) 

103 self.assertEqual(c.c2.apply(), 3) 

104 

105 c.c2.retarget(Target1) 

106 self.assertEqual(c.c2.f, 3) 

107 self.assertEqual(type(c.c2.apply()), Target1) 

108 self.assertEqual(c.c2.apply().f, 3) 

109 

110 c.c1.f = 2 

111 self.assertEqual(c.c1.f, 2) 

112 self.assertRaises(pexConf.FieldValidationError, setattr, c.c1, "f", 0) 

113 

114 c.c1 = Config1(f=10) 

115 self.assertEqual(c.c1.f, 10) 

116 

117 c.c1 = Config1 

118 self.assertEqual(c.c1.f, 5) 

119 

120 f = Config2(**dict(c.items())) 

121 self.assertEqual(f.c1.f, c.c1.f) 

122 self.assertEqual(f.c1.target, c.c1.target) 

123 self.assertEqual(f.c2.target, c.c2.target) 

124 self.assertEqual(f.c2.f, c.c2.f) 

125 

126 c.c2.f = 1 

127 c.c1.f = 100 

128 f.update(**dict(c.items())) 

129 self.assertEqual(f.c1.f, c.c1.f) 

130 self.assertEqual(f.c1.target, c.c1.target) 

131 self.assertEqual(f.c2.target, c.c2.target) 

132 self.assertEqual(f.c2.f, c.c2.f) 

133 

134 def testValidate(self): 

135 c = Config2() 

136 self.assertRaises(pexConf.FieldValidationError, setattr, c.c1, "f", 0) 

137 

138 c.validate() 

139 

140 def testPersistence(self): 

141 c = Config2() 

142 c.c2.retarget(Target1) 

143 c.c2.f = 10 

144 

145 with tempfile.NamedTemporaryFile(suffix=".py", prefix="test-config-field-") as tmp: 

146 c.save(tmp.name) 

147 

148 r = Config2() 

149 r.load(tmp.name) 

150 

151 self.assertEqual(c.c2.f, r.c2.f) 

152 self.assertEqual(c.c2.target, r.c2.target) 

153 

154 def testNoPickle(self): 

155 """Test that pickle support is disabled for the proxy container.""" 

156 c = Config2() 

157 with self.assertRaises(pexConf.UnexpectedProxyUsageError): 

158 pickle.dumps(c.c2) 

159 

160 

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

162 unittest.main()