Coverage for tests/test_configApdbLoader.py: 20%

140 statements  

« prev     ^ index     » next       coverage.py v6.4.1, created at 2022-07-03 01:31 -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 testNoConfig(self): 

54 result = self.task.run(None) 

55 self.assertIsNone(result.apdb) 

56 

57 def testEmptyConfig(self): 

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

59 self.assertIsNone(result.apdb) 

60 

61 def testSelfConfig(self): 

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

63 self.assertIsInstance(result.apdb, Apdb) 

64 

65 def testConfigChoiceFieldUnSelected(self): 

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

67 

68 class TestConfig(Config): 

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

70 

71 config = TestConfig() 

72 config.field = "foo" 

73 result = self.task.run(config) 

74 self.assertIsNone(result.apdb) 

75 

76 def testConfigChoiceFieldSelected(self): 

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

78 # not very useful for ApdbConfig and subclasses. 

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

80 

81 class TestConfig(Config): 

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

83 

84 config = TestConfig() 

85 config.field = "bar" 

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

87 result = self.task.run(config) 

88 self.assertIsInstance(result.apdb, Apdb) 

89 

90 def testConfigChoiceFieldMulti(self): 

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

92 # not very useful for ApdbConfig and subclasses. 

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

94 

95 class TestConfig(Config): 

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

97 

98 config = TestConfig() 

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

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

101 result = self.task.run(config) 

102 self.assertIsInstance(result.apdb, Apdb) 

103 

104 def testRegistryFieldUnSelected(self): 

105 registry = self._dummyRegistry() 

106 

107 class TestConfig(Config): 

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

109 

110 config = TestConfig() 

111 config.field = "foo" 

112 result = self.task.run(config) 

113 self.assertIsNone(result.apdb) 

114 

115 def testRegistryFieldSelected(self): 

116 registry = self._dummyRegistry() 

117 

118 class TestConfig(Config): 

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

120 

121 config = TestConfig() 

122 config.field = "bar" 

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

124 result = self.task.run(config) 

125 self.assertIsInstance(result.apdb, Apdb) 

126 

127 def testRegistryFieldMulti(self): 

128 registry = self._dummyRegistry() 

129 

130 class TestConfig(Config): 

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

132 

133 config = TestConfig() 

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

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

136 result = self.task.run(config) 

137 self.assertIsInstance(result.apdb, Apdb) 

138 

139 def testConfigField(self): 

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

141 # very useful for ApdbConfig and subclasses. 

142 class TestConfig(Config): 

143 field = ConfigField(dtype=ApdbSqlConfig, 

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

145 

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

147 self.assertIsInstance(result.apdb, Apdb) 

148 

149 def testConfigurableField(self): 

150 class TestConfig(Config): 

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

152 

153 config = TestConfig() 

154 config.field = self._dummyApdbConfig() 

155 self.assertIsInstance(config.field, ConfigurableInstance) 

156 result = self.task.run(config) 

157 self.assertIsInstance(result.apdb, Apdb) 

158 

159 def testConfigurableFieldRetarget(self): 

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

161 class TestConfig(Config): 

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

163 

164 config = TestConfig() 

165 config.field.retarget(ApdbSql) 

166 config.field = self._dummyApdbConfig() 

167 self.assertIsInstance(config.field, ConfigurableInstance) 

168 result = self.task.run(config) 

169 self.assertIsInstance(result.apdb, Apdb) 

170 

171 def testConfigDictFieldUnSelected(self): 

172 class TestConfig(Config): 

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

174 

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

176 self.assertIsNone(result.apdb) 

177 

178 def testConfigDictFieldSelected(self): 

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

180 # not very useful for ApdbConfig and subclasses. 

181 class TestConfig(Config): 

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

183 doc="") 

184 

185 config = TestConfig() 

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

187 result = self.task.run(config) 

188 self.assertIsInstance(result.apdb, Apdb) 

189 

190 def testSiblingConfigs(self): 

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

192 # very useful for ApdbConfig and subclasses. 

193 class TestConfig(Config): 

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

195 field2 = ConfigField(dtype=ApdbSqlConfig, 

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

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

198 

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

200 self.assertIsInstance(result.apdb, Apdb) 

201 

202 def testNestedConfigs(self): 

203 class InnerConfig(Config): 

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

205 

206 class TestConfig(Config): 

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

208 

209 config = TestConfig() 

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

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

212 result = self.task.run(config) 

213 self.assertIsInstance(result.apdb, Apdb) 

214 

215 

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

217 pass 

218 

219 

220def setup_module(module): 

221 lsst.utils.tests.init() 

222 

223 

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

225 lsst.utils.tests.init() 

226 unittest.main()