Coverage for tests/test_apdbSql.py: 55%

75 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-10-12 02:38 -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 unittest 

28from typing import Any 

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 

61 

62class ApdbSQLiteTestCase(unittest.TestCase, ApdbSqlTest): 

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

64 

65 fsrc_requires_id_list = True 

66 dia_object_index = "baseline" 

67 

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

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

70 kw = { 

71 "db_url": "sqlite://", 

72 "schema_file": TEST_SCHEMA, 

73 "dia_object_index": self.dia_object_index 

74 } 

75 kw.update(kwargs) 

76 return ApdbSqlConfig(**kw) 

77 

78 def getDiaObjects_table(self) -> ApdbTables: 

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

80 return ApdbTables.DiaObject 

81 

82 

83class ApdbSQLiteTestCaseLastObject(ApdbSQLiteTestCase): 

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

85 table. 

86 """ 

87 

88 dia_object_index = "last_object_table" 

89 

90 def getDiaObjects_table(self) -> ApdbTables: 

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

92 return ApdbTables.DiaObjectLast 

93 

94 

95class ApdbSQLiteTestCasePixIdIovIndex(ApdbSQLiteTestCase): 

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

97 indexing. 

98 """ 

99 

100 dia_object_index = "pix_id_iov" 

101 

102 

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

104class ApdbPostgresTestCase(unittest.TestCase, ApdbSqlTest): 

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

106 

107 fsrc_requires_id_list = True 

108 dia_object_index = "last_object_table" 

109 

110 @classmethod 

111 def setUpClass(cls): 

112 # Create the postgres test server. 

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

114 super().setUpClass() 

115 

116 @classmethod 

117 def tearDownClass(cls): 

118 # Clean up any lingering SQLAlchemy engines/connections 

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

120 gc.collect() 

121 cls.postgresql.clear_cache() 

122 super().tearDownClass() 

123 

124 def setUp(self): 

125 self.server = self.postgresql() 

126 

127 def tearDown(self): 

128 self.server = self.postgresql() 

129 

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

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

132 kw = { 

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

134 "schema_file": TEST_SCHEMA, 

135 "dia_object_index": self.dia_object_index 

136 } 

137 kw.update(kwargs) 

138 return ApdbSqlConfig(**kw) 

139 

140 def getDiaObjects_table(self) -> ApdbTables: 

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

142 return ApdbTables.DiaObjectLast 

143 

144 

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

146class ApdbPostgresNamespaceTestCase(ApdbPostgresTestCase): 

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

148 

149 # use mixed case to trigger quoting 

150 namespace = "ApdbSchema" 

151 

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

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

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

155 

156 

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

158 pass 

159 

160 

161def setup_module(module): 

162 lsst.utils.tests.init() 

163 

164 

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

166 lsst.utils.tests.init() 

167 unittest.main()