Coverage for tests/test_apdbSql.py: 55%
78 statements
« prev ^ index » next coverage.py v7.2.1, created at 2023-03-13 03:06 +0000
« prev ^ index » next coverage.py v7.2.1, created at 2023-03-13 03:06 +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/>.
22"""Unit test for Apdb class.
23"""
25import gc
26import os
27import unittest
28from typing import Any, Dict
30from lsst.dax.apdb import ApdbConfig, ApdbSqlConfig, ApdbTables
31from lsst.dax.apdb.tests import ApdbTest
32import lsst.utils.tests
34try:
35 import testing.postgresql
36except ImportError:
37 testing = None
40TEST_SCHEMA = os.path.join(os.path.abspath(os.path.dirname(__file__)), "config/schema.yaml")
43class ApdbSqlTest(ApdbTest):
44 """Base class for unit tests for SQL backends."""
46 def n_columns(self, table: ApdbTables) -> int:
47 """Return number of columns for a specified table."""
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
63class ApdbSQLiteTestCase(unittest.TestCase, ApdbSqlTest):
64 """A test case for ApdbSql class using SQLite backend."""
66 fsrc_requires_id_list = True
67 dia_object_index = "baseline"
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)
79 def getDiaObjects_table(self) -> ApdbTables:
80 """Return type of table returned from getDiaObjects method."""
81 return ApdbTables.DiaObject
84class ApdbSQLiteTestCaseLastObject(ApdbSQLiteTestCase):
85 """A test case for ApdbSql class using SQLite backend and DiaObjectLast
86 table.
87 """
89 dia_object_index = "last_object_table"
91 extra_object_columns: Dict[str, Any] = {"parallax": 0.}
93 def getDiaObjects_table(self) -> ApdbTables:
94 """Return type of table returned from getDiaObjects method."""
95 return ApdbTables.DiaObjectLast
98class ApdbSQLiteTestCasePixIdIovIndex(ApdbSQLiteTestCase):
99 """A test case for ApdbSql class using SQLite backend with pix_id_iov
100 indexing.
101 """
103 dia_object_index = "pix_id_iov"
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."""
110 fsrc_requires_id_list = True
111 dia_object_index = "last_object_table"
112 postgresql: Any
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()
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()
128 def setUp(self) -> None:
129 self.server = self.postgresql()
131 def tearDown(self) -> None:
132 self.server = self.postgresql()
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)
144 def getDiaObjects_table(self) -> ApdbTables:
145 """Return type of table returned from getDiaObjects method."""
146 return ApdbTables.DiaObjectLast
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"""
153 # use mixed case to trigger quoting
154 namespace = "ApdbSchema"
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)
161class MyMemoryTestCase(lsst.utils.tests.MemoryTestCase):
162 pass
165def setup_module(module: Any) -> None:
166 lsst.utils.tests.init()
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()