Coverage for tests/test_apdbSql.py: 55%

78 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-12-14 03:45 -0800

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 unittest 

28from typing import Any, Dict 

29 

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

31from lsst.dax.apdb.tests import ApdbTest 

32import lsst.utils.tests 

33 

34try: 

35 import testing.postgresql 

36except ImportError: 

37 testing = None 

38 

39 

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

41 

42 

43class ApdbSqlTest(ApdbTest): 

44 """Base class for unit tests for SQL backends.""" 

45 

46 def n_columns(self, table: ApdbTables) -> int: 

47 """Return number of columns for a specified table.""" 

48 

49 # Some tables add pixelId column to standard schema 

50 if table is ApdbTables.DiaObject: 

51 return self.n_obj_columns + 1 

52 elif table is ApdbTables.DiaObjectLast: 

53 return self.n_obj_last_columns + 1 

54 elif table is ApdbTables.DiaSource: 

55 return self.n_src_columns + 1 

56 elif table is ApdbTables.DiaForcedSource: 

57 return self.n_fsrc_columns 

58 elif table is ApdbTables.SSObject: 

59 return self.n_ssobj_columns 

60 return -1 

61 

62 

63class ApdbSQLiteTestCase(unittest.TestCase, ApdbSqlTest): 

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

65 

66 fsrc_requires_id_list = True 

67 dia_object_index = "baseline" 

68 

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

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

71 kw = { 

72 "db_url": "sqlite://", 

73 "schema_file": TEST_SCHEMA, 

74 "dia_object_index": self.dia_object_index 

75 } 

76 kw.update(kwargs) 

77 return ApdbSqlConfig(**kw) 

78 

79 def getDiaObjects_table(self) -> ApdbTables: 

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

81 return ApdbTables.DiaObject 

82 

83 

84class ApdbSQLiteTestCaseLastObject(ApdbSQLiteTestCase): 

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

86 table. 

87 """ 

88 

89 dia_object_index = "last_object_table" 

90 

91 extra_object_columns: Dict[str, Any] = {"parallax": 0.} 

92 

93 def getDiaObjects_table(self) -> ApdbTables: 

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

95 return ApdbTables.DiaObjectLast 

96 

97 

98class ApdbSQLiteTestCasePixIdIovIndex(ApdbSQLiteTestCase): 

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

100 indexing. 

101 """ 

102 

103 dia_object_index = "pix_id_iov" 

104 

105 

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

107class ApdbPostgresTestCase(unittest.TestCase, ApdbSqlTest): 

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

109 

110 fsrc_requires_id_list = True 

111 dia_object_index = "last_object_table" 

112 postgresql: Any 

113 

114 @classmethod 

115 def setUpClass(cls) -> None: 

116 # Create the postgres test server. 

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

118 super().setUpClass() 

119 

120 @classmethod 

121 def tearDownClass(cls) -> None: 

122 # Clean up any lingering SQLAlchemy engines/connections 

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

124 gc.collect() 

125 cls.postgresql.clear_cache() 

126 super().tearDownClass() 

127 

128 def setUp(self) -> None: 

129 self.server = self.postgresql() 

130 

131 def tearDown(self) -> None: 

132 self.server = self.postgresql() 

133 

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

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

136 kw = { 

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

138 "schema_file": TEST_SCHEMA, 

139 "dia_object_index": self.dia_object_index 

140 } 

141 kw.update(kwargs) 

142 return ApdbSqlConfig(**kw) 

143 

144 def getDiaObjects_table(self) -> ApdbTables: 

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

146 return ApdbTables.DiaObjectLast 

147 

148 

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

150class ApdbPostgresNamespaceTestCase(ApdbPostgresTestCase): 

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

152 

153 # use mixed case to trigger quoting 

154 namespace = "ApdbSchema" 

155 

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

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

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

159 

160 

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

162 pass 

163 

164 

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

166 lsst.utils.tests.init() 

167 

168 

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

170 lsst.utils.tests.init() 

171 unittest.main()