Coverage for tests/test_configApdbLoader.py: 20%

137 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-09-21 01:43 -0700

1# This file is part of verify. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://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 program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21 

22import unittest 

23 

24import lsst.utils.tests 

25from lsst.pex.config import Config, Field, ConfigField, ConfigChoiceField, \ 

26 RegistryField, Registry, ConfigurableField, ConfigurableInstance, \ 

27 ConfigDictField 

28from lsst.dax.apdb import Apdb, ApdbConfig, ApdbSql, ApdbSqlConfig 

29 

30from lsst.verify.tasks import ConfigApdbLoader 

31 

32 

33class ConfigApdbLoaderTestSuite(lsst.utils.tests.TestCase): 

34 

35 @staticmethod 

36 def _dummyRegistry(): 

37 class DummyConfigurable: 

38 ConfigClass = Config 

39 registry = Registry() 

40 registry.register("foo", DummyConfigurable) 

41 registry.register("bar", ApdbSql, ConfigClass=ApdbSqlConfig) 

42 return registry 

43 

44 @staticmethod 

45 def _dummyApdbConfig(): 

46 config = ApdbSqlConfig() 

47 config.db_url = "sqlite://" # in-memory DB 

48 return config 

49 

50 def setUp(self): 

51 self.task = ConfigApdbLoader() 

52 

53 def testEmptyConfig(self): 

54 result = self.task.run(Config()) 

55 self.assertIsNone(result.apdb) 

56 

57 def testSelfConfig(self): 

58 result = self.task.run(self._dummyApdbConfig()) 

59 self.assertIsInstance(result.apdb, Apdb) 

60 

61 def testConfigChoiceFieldUnSelected(self): 

62 typemap = {"foo": Config, "bar": ApdbConfig} 

63 

64 class TestConfig(Config): 

65 field = ConfigChoiceField(typemap=typemap, doc="") 

66 

67 config = TestConfig() 

68 config.field = "foo" 

69 result = self.task.run(config) 

70 self.assertIsNone(result.apdb) 

71 

72 def testConfigChoiceFieldSelected(self): 

73 # Note: ConfigChoiceField does not support polymorphic types and it is 

74 # not very useful for ApdbConfig and subclasses. 

75 typemap = {"foo": Config, "bar": ApdbSqlConfig} 

76 

77 class TestConfig(Config): 

78 field = ConfigChoiceField(typemap=typemap, doc="") 

79 

80 config = TestConfig() 

81 config.field = "bar" 

82 config.field["bar"] = self._dummyApdbConfig() 

83 result = self.task.run(config) 

84 self.assertIsInstance(result.apdb, Apdb) 

85 

86 def testConfigChoiceFieldMulti(self): 

87 # Note: ConfigChoiceField does not support polymorphic types and it is 

88 # not very useful for ApdbConfig and subclasses. 

89 typemap = {"foo": Config, "bar": ApdbSqlConfig} 

90 

91 class TestConfig(Config): 

92 field = ConfigChoiceField(typemap=typemap, doc="", multi=True) 

93 

94 config = TestConfig() 

95 config.field = {"bar", "foo"} 

96 config.field["bar"] = self._dummyApdbConfig() 

97 result = self.task.run(config) 

98 self.assertIsInstance(result.apdb, Apdb) 

99 

100 def testRegistryFieldUnSelected(self): 

101 registry = self._dummyRegistry() 

102 

103 class TestConfig(Config): 

104 field = RegistryField(registry=registry, doc="") 

105 

106 config = TestConfig() 

107 config.field = "foo" 

108 result = self.task.run(config) 

109 self.assertIsNone(result.apdb) 

110 

111 def testRegistryFieldSelected(self): 

112 registry = self._dummyRegistry() 

113 

114 class TestConfig(Config): 

115 field = RegistryField(registry=registry, doc="") 

116 

117 config = TestConfig() 

118 config.field = "bar" 

119 config.field["bar"] = self._dummyApdbConfig() 

120 result = self.task.run(config) 

121 self.assertIsInstance(result.apdb, Apdb) 

122 

123 def testRegistryFieldMulti(self): 

124 registry = self._dummyRegistry() 

125 

126 class TestConfig(Config): 

127 field = RegistryField(registry=registry, doc="", multi=True) 

128 

129 config = TestConfig() 

130 config.field = {"bar", "foo"} 

131 config.field["bar"] = self._dummyApdbConfig() 

132 result = self.task.run(config) 

133 self.assertIsInstance(result.apdb, Apdb) 

134 

135 def testConfigField(self): 

136 # Note: ConfigField does not support polymorphic types and it is not 

137 # very useful for ApdbConfig and subclasses. 

138 class TestConfig(Config): 

139 field = ConfigField(dtype=ApdbSqlConfig, 

140 default=self._dummyApdbConfig(), doc="") 

141 

142 result = self.task.run(TestConfig()) 

143 self.assertIsInstance(result.apdb, Apdb) 

144 

145 def testConfigurableField(self): 

146 class TestConfig(Config): 

147 field = ConfigurableField(target=ApdbSql, doc="") 

148 

149 config = TestConfig() 

150 config.field = self._dummyApdbConfig() 

151 self.assertIsInstance(config.field, ConfigurableInstance) 

152 result = self.task.run(config) 

153 self.assertIsInstance(result.apdb, Apdb) 

154 

155 def testConfigurableFieldRetarget(self): 

156 # Initally set to abstract target, has to be re-targeted before use. 

157 class TestConfig(Config): 

158 field = ConfigurableField(target=Apdb, doc="") 

159 

160 config = TestConfig() 

161 config.field.retarget(ApdbSql) 

162 config.field = self._dummyApdbConfig() 

163 self.assertIsInstance(config.field, ConfigurableInstance) 

164 result = self.task.run(config) 

165 self.assertIsInstance(result.apdb, Apdb) 

166 

167 def testConfigDictFieldUnSelected(self): 

168 class TestConfig(Config): 

169 field = ConfigDictField(keytype=int, itemtype=ApdbConfig, doc="") 

170 

171 result = self.task.run(TestConfig()) 

172 self.assertIsNone(result.apdb) 

173 

174 def testConfigDictFieldSelected(self): 

175 # Note: ConfigDictField does not support polymorphic types and it is 

176 # not very useful for ApdbConfig and subclasses. 

177 class TestConfig(Config): 

178 field = ConfigDictField(keytype=int, itemtype=ApdbSqlConfig, 

179 doc="") 

180 

181 config = TestConfig() 

182 config.field = {42: self._dummyApdbConfig()} 

183 result = self.task.run(config) 

184 self.assertIsInstance(result.apdb, Apdb) 

185 

186 def testSiblingConfigs(self): 

187 # Note: ConfigField does not support polymorphic types and it is not 

188 # very useful for ApdbConfig and subclasses. 

189 class TestConfig(Config): 

190 field1 = Field(dtype=int, doc="") 

191 field2 = ConfigField(dtype=ApdbSqlConfig, 

192 default=self._dummyApdbConfig(), doc="") 

193 field3 = Field(dtype=str, doc="") 

194 

195 result = self.task.run(TestConfig()) 

196 self.assertIsInstance(result.apdb, Apdb) 

197 

198 def testNestedConfigs(self): 

199 class InnerConfig(Config): 

200 field = ConfigurableField(target=ApdbSql, doc="") 

201 

202 class TestConfig(Config): 

203 field = ConfigField(dtype=InnerConfig, doc="") 

204 

205 config = TestConfig() 

206 config.field.field = self._dummyApdbConfig() 

207 self.assertIsInstance(config.field.field, ConfigurableInstance) 

208 result = self.task.run(config) 

209 self.assertIsInstance(result.apdb, Apdb) 

210 

211 

212class MemoryTester(lsst.utils.tests.MemoryTestCase): 

213 pass 

214 

215 

216def setup_module(module): 

217 lsst.utils.tests.init() 

218 

219 

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

221 lsst.utils.tests.init() 

222 unittest.main()