Coverage for tests/test_configApdbLoader.py: 24%

141 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-05-04 09:40 +0000

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 shutil 

23import tempfile 

24import unittest 

25 

26import lsst.utils.tests 

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

28 RegistryField, Registry, ConfigurableField, ConfigurableInstance, \ 

29 ConfigDictField 

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

31 

32from lsst.verify.tasks import ConfigApdbLoader 

33 

34 

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

36 

37 @staticmethod 

38 def _dummyRegistry(): 

39 class DummyConfigurable: 

40 ConfigClass = Config 

41 registry = Registry() 

42 registry.register("foo", DummyConfigurable) 

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

44 return registry 

45 

46 def _dummyApdbConfig(self): 

47 return ApdbSql.init_database(db_url=self.db_url) 

48 

49 def setUp(self): 

50 self.tempdir = tempfile.mkdtemp() 

51 self.db_url = f"sqlite:///{self.tempdir}/apdb.sqlite3" 

52 with self.assertWarns(FutureWarning): 

53 self.task = ConfigApdbLoader() 

54 

55 def tearDown(self): 

56 shutil.rmtree(self.tempdir, ignore_errors=True) 

57 

58 def testEmptyConfig(self): 

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

60 self.assertIsNone(result.apdb) 

61 

62 def testSelfConfig(self): 

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

64 self.assertIsInstance(result.apdb, Apdb) 

65 

66 def testConfigChoiceFieldUnSelected(self): 

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

68 

69 class TestConfig(Config): 

70 field = ConfigChoiceField(typemap=typemap, doc="test") 

71 

72 config = TestConfig() 

73 config.field = "foo" 

74 result = self.task.run(config) 

75 self.assertIsNone(result.apdb) 

76 

77 def testConfigChoiceFieldSelected(self): 

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

79 # not very useful for ApdbConfig and subclasses. 

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

81 

82 class TestConfig(Config): 

83 field = ConfigChoiceField(typemap=typemap, doc="test") 

84 

85 config = TestConfig() 

86 config.field = "bar" 

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

88 result = self.task.run(config) 

89 self.assertIsInstance(result.apdb, Apdb) 

90 

91 def testConfigChoiceFieldMulti(self): 

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

93 # not very useful for ApdbConfig and subclasses. 

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

95 

96 class TestConfig(Config): 

97 field = ConfigChoiceField(typemap=typemap, doc="test", multi=True) 

98 

99 config = TestConfig() 

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

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

102 result = self.task.run(config) 

103 self.assertIsInstance(result.apdb, Apdb) 

104 

105 def testRegistryFieldUnSelected(self): 

106 registry = self._dummyRegistry() 

107 

108 class TestConfig(Config): 

109 field = RegistryField(registry=registry, doc="test") 

110 

111 config = TestConfig() 

112 config.field = "foo" 

113 result = self.task.run(config) 

114 self.assertIsNone(result.apdb) 

115 

116 def testRegistryFieldSelected(self): 

117 registry = self._dummyRegistry() 

118 

119 class TestConfig(Config): 

120 field = RegistryField(registry=registry, doc="test") 

121 

122 config = TestConfig() 

123 config.field = "bar" 

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

125 result = self.task.run(config) 

126 self.assertIsInstance(result.apdb, Apdb) 

127 

128 def testRegistryFieldMulti(self): 

129 registry = self._dummyRegistry() 

130 

131 class TestConfig(Config): 

132 field = RegistryField(registry=registry, doc="test", multi=True) 

133 

134 config = TestConfig() 

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

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

137 result = self.task.run(config) 

138 self.assertIsInstance(result.apdb, Apdb) 

139 

140 def testConfigField(self): 

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

142 # very useful for ApdbConfig and subclasses. 

143 class TestConfig(Config): 

144 field = ConfigField(dtype=ApdbSqlConfig, 

145 default=self._dummyApdbConfig(), doc="test") 

146 

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

148 self.assertIsInstance(result.apdb, Apdb) 

149 

150 def testConfigurableField(self): 

151 class TestConfig(Config): 

152 field = ConfigurableField(target=ApdbSql, doc="test") 

153 

154 config = TestConfig() 

155 config.field = self._dummyApdbConfig() 

156 self.assertIsInstance(config.field, ConfigurableInstance) 

157 result = self.task.run(config) 

158 self.assertIsInstance(result.apdb, Apdb) 

159 

160 def testConfigurableFieldRetarget(self): 

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

162 class TestConfig(Config): 

163 field = ConfigurableField(target=Apdb, doc="test") 

164 

165 config = TestConfig() 

166 config.field.retarget(ApdbSql) 

167 config.field = self._dummyApdbConfig() 

168 self.assertIsInstance(config.field, ConfigurableInstance) 

169 result = self.task.run(config) 

170 self.assertIsInstance(result.apdb, Apdb) 

171 

172 def testConfigDictFieldUnSelected(self): 

173 class TestConfig(Config): 

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

175 

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

177 self.assertIsNone(result.apdb) 

178 

179 def testConfigDictFieldSelected(self): 

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

181 # not very useful for ApdbConfig and subclasses. 

182 class TestConfig(Config): 

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

184 doc="test") 

185 

186 config = TestConfig() 

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

188 result = self.task.run(config) 

189 self.assertIsInstance(result.apdb, Apdb) 

190 

191 def testSiblingConfigs(self): 

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

193 # very useful for ApdbConfig and subclasses. 

194 class TestConfig(Config): 

195 field1 = Field(dtype=int, doc="test") 

196 field2 = ConfigField(dtype=ApdbSqlConfig, 

197 default=self._dummyApdbConfig(), doc="test") 

198 field3 = Field(dtype=str, doc="test") 

199 

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

201 self.assertIsInstance(result.apdb, Apdb) 

202 

203 def testNestedConfigs(self): 

204 class InnerConfig(Config): 

205 field = ConfigurableField(target=ApdbSql, doc="test") 

206 

207 class TestConfig(Config): 

208 field = ConfigField(dtype=InnerConfig, doc="test") 

209 

210 config = TestConfig() 

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

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

213 result = self.task.run(config) 

214 self.assertIsInstance(result.apdb, Apdb) 

215 

216 

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

218 pass 

219 

220 

221def setup_module(module): 

222 lsst.utils.tests.init() 

223 

224 

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

226 lsst.utils.tests.init() 

227 unittest.main()