Coverage for tests/test_apdbSql.py: 69%

83 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-03-16 02:07 -0700

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(ApdbTest, unittest.TestCase): 

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

46 

47 fsrc_requires_id_list = True 

48 dia_object_index = "baseline" 

49 allow_visit_query = False 

50 

51 def setUp(self) -> None: 

52 self.tempdir = tempfile.mkdtemp() 

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

54 

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

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

57 kw = { 

58 "db_url": self.db_url, 

59 "schema_file": TEST_SCHEMA, 

60 "dia_object_index": self.dia_object_index, 

61 "use_insert_id": self.use_insert_id, 

62 } 

63 kw.update(kwargs) 

64 return ApdbSqlConfig(**kw) 

65 

66 def getDiaObjects_table(self) -> ApdbTables: 

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

68 return ApdbTables.DiaObject 

69 

70 

71class ApdbSQLiteTestCaseLastObject(ApdbSQLiteTestCase): 

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

73 table. 

74 """ 

75 

76 dia_object_index = "last_object_table" 

77 

78 def getDiaObjects_table(self) -> ApdbTables: 

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

80 return ApdbTables.DiaObjectLast 

81 

82 

83class ApdbSQLiteTestCasePixIdIovIndex(ApdbSQLiteTestCase): 

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

85 indexing. 

86 """ 

87 

88 dia_object_index = "pix_id_iov" 

89 

90 

91class ApdbSQLiteTestCaseInsertIds(ApdbSQLiteTestCase): 

92 """Test case for ApdbSql class using SQLite backend with use_insert_id.""" 

93 

94 use_insert_id = True 

95 

96 

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

98class ApdbPostgresTestCase(ApdbTest, unittest.TestCase): 

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

100 

101 fsrc_requires_id_list = True 

102 dia_object_index = "last_object_table" 

103 postgresql: Any 

104 use_insert_id = True 

105 allow_visit_query = False 

106 

107 @classmethod 

108 def setUpClass(cls) -> None: 

109 # Create the postgres test server. 

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

111 super().setUpClass() 

112 

113 @classmethod 

114 def tearDownClass(cls) -> None: 

115 # Clean up any lingering SQLAlchemy engines/connections 

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

117 gc.collect() 

118 cls.postgresql.clear_cache() 

119 super().tearDownClass() 

120 

121 def setUp(self) -> None: 

122 self.server = self.postgresql() 

123 

124 def tearDown(self) -> None: 

125 self.server = self.postgresql() 

126 

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

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

129 kw = { 

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

131 "schema_file": TEST_SCHEMA, 

132 "dia_object_index": self.dia_object_index, 

133 "use_insert_id": self.use_insert_id, 

134 } 

135 kw.update(kwargs) 

136 return ApdbSqlConfig(**kw) 

137 

138 def getDiaObjects_table(self) -> ApdbTables: 

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

140 return ApdbTables.DiaObjectLast 

141 

142 

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

144class ApdbPostgresNamespaceTestCase(ApdbPostgresTestCase): 

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

146 

147 # use mixed case to trigger quoting 

148 namespace = "ApdbSchema" 

149 

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

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

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

153 

154 

155class ApdbSchemaUpdateSQLiteTestCase(ApdbSchemaUpdateTest, unittest.TestCase): 

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

157 

158 def setUp(self) -> None: 

159 self.tempdir = tempfile.mkdtemp() 

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

161 

162 def tearDown(self) -> None: 

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

164 

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

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

167 kw = { 

168 "db_url": self.db_url, 

169 "schema_file": TEST_SCHEMA, 

170 } 

171 kw.update(kwargs) 

172 return ApdbSqlConfig(**kw) 

173 

174 

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

176 """Run file leak tests.""" 

177 

178 

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

180 """Configure pytest.""" 

181 lsst.utils.tests.init() 

182 

183 

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

185 lsst.utils.tests.init() 

186 unittest.main()