Coverage for tests/test_apdbSql.py: 70%

79 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-14 19:51 +0000

1# This file is part of dax_apdb. 

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 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 <http://www.gnu.org/licenses/>. 

21 

22"""Unit test for Apdb class. 

23""" 

24 

25import gc 

26import os 

27import shutil 

28import tempfile 

29import unittest 

30from typing import Any 

31 

32import lsst.utils.tests 

33from lsst.dax.apdb import ApdbConfig, ApdbSqlConfig, ApdbTables 

34from lsst.dax.apdb.tests import ApdbSchemaUpdateTest, ApdbTest 

35 

36try: 

37 import testing.postgresql 

38except ImportError: 

39 testing = None 

40 

41TEST_SCHEMA = os.path.join(os.path.abspath(os.path.dirname(__file__)), "config/schema.yaml") 

42 

43 

44class ApdbSQLiteTestCase(unittest.TestCase, ApdbTest): 

45 """A test case for ApdbSql class using SQLite backend.""" 

46 

47 fsrc_requires_id_list = True 

48 dia_object_index = "baseline" 

49 

50 def make_config(self, **kwargs: Any) -> ApdbConfig: 

51 """Make config class instance used in all tests.""" 

52 kw = { 

53 "db_url": "sqlite://", 

54 "schema_file": TEST_SCHEMA, 

55 "dia_object_index": self.dia_object_index, 

56 "use_insert_id": self.use_insert_id, 

57 } 

58 kw.update(kwargs) 

59 return ApdbSqlConfig(**kw) 

60 

61 def getDiaObjects_table(self) -> ApdbTables: 

62 """Return type of table returned from getDiaObjects method.""" 

63 return ApdbTables.DiaObject 

64 

65 

66class ApdbSQLiteTestCaseLastObject(ApdbSQLiteTestCase): 

67 """A test case for ApdbSql class using SQLite backend and DiaObjectLast 

68 table. 

69 """ 

70 

71 dia_object_index = "last_object_table" 

72 

73 def getDiaObjects_table(self) -> ApdbTables: 

74 """Return type of table returned from getDiaObjects method.""" 

75 return ApdbTables.DiaObjectLast 

76 

77 

78class ApdbSQLiteTestCasePixIdIovIndex(ApdbSQLiteTestCase): 

79 """A test case for ApdbSql class using SQLite backend with pix_id_iov 

80 indexing. 

81 """ 

82 

83 dia_object_index = "pix_id_iov" 

84 

85 

86class ApdbSQLiteTestCaseInsertIds(ApdbSQLiteTestCase): 

87 """A test case for ApdbSql class using SQLite backend with use_insert_id.""" 

88 

89 use_insert_id = True 

90 

91 

92@unittest.skipUnless(testing is not None, "testing.postgresql module not found") 

93class ApdbPostgresTestCase(unittest.TestCase, ApdbTest): 

94 """A test case for ApdbSql class using Postgres backend.""" 

95 

96 fsrc_requires_id_list = True 

97 dia_object_index = "last_object_table" 

98 postgresql: Any 

99 use_insert_id = True 

100 

101 @classmethod 

102 def setUpClass(cls) -> None: 

103 # Create the postgres test server. 

104 cls.postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True) 

105 super().setUpClass() 

106 

107 @classmethod 

108 def tearDownClass(cls) -> None: 

109 # Clean up any lingering SQLAlchemy engines/connections 

110 # so they're closed before we shut down the server. 

111 gc.collect() 

112 cls.postgresql.clear_cache() 

113 super().tearDownClass() 

114 

115 def setUp(self) -> None: 

116 self.server = self.postgresql() 

117 

118 def tearDown(self) -> None: 

119 self.server = self.postgresql() 

120 

121 def make_config(self, **kwargs: Any) -> ApdbConfig: 

122 """Make config class instance used in all tests.""" 

123 kw = { 

124 "db_url": self.server.url(), 

125 "schema_file": TEST_SCHEMA, 

126 "dia_object_index": self.dia_object_index, 

127 } 

128 kw.update(kwargs) 

129 return ApdbSqlConfig(**kw) 

130 

131 def getDiaObjects_table(self) -> ApdbTables: 

132 """Return type of table returned from getDiaObjects method.""" 

133 return ApdbTables.DiaObjectLast 

134 

135 

136@unittest.skipUnless(testing is not None, "testing.postgresql module not found") 

137class ApdbPostgresNamespaceTestCase(ApdbPostgresTestCase): 

138 """A test case for ApdbSql class using Postgres backend with schema name""" 

139 

140 # use mixed case to trigger quoting 

141 namespace = "ApdbSchema" 

142 

143 def make_config(self, **kwargs: Any) -> ApdbConfig: 

144 """Make config class instance used in all tests.""" 

145 return super().make_config(namespace=self.namespace, **kwargs) 

146 

147 

148class ApdbSchemaUpdateSQLiteTestCase(unittest.TestCase, ApdbSchemaUpdateTest): 

149 """A test case for schema updates using SQLite backend.""" 

150 

151 def setUp(self) -> None: 

152 self.tempdir = tempfile.mkdtemp() 

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

154 

155 def tearDown(self) -> None: 

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

157 

158 def make_config(self, **kwargs: Any) -> ApdbConfig: 

159 """Make config class instance used in all tests.""" 

160 kw = { 

161 "db_url": self.db_url, 

162 "schema_file": TEST_SCHEMA, 

163 } 

164 kw.update(kwargs) 

165 return ApdbSqlConfig(**kw) 

166 

167 

168class MyMemoryTestCase(lsst.utils.tests.MemoryTestCase): 

169 pass 

170 

171 

172def setup_module(module: Any) -> None: 

173 lsst.utils.tests.init() 

174 

175 

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

177 lsst.utils.tests.init() 

178 unittest.main()