Coverage for tests/test_apdbSql.py: 69%
78 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-10-26 10:23 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-10-26 10:23 +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 shutil
28import tempfile
29import unittest
30from typing import Any
32import lsst.utils.tests
33from lsst.dax.apdb import ApdbConfig, ApdbSqlConfig, ApdbTables
34from lsst.dax.apdb.tests import ApdbSchemaUpdateTest, ApdbTest
36try:
37 import testing.postgresql
38except ImportError:
39 testing = None
41TEST_SCHEMA = os.path.join(os.path.abspath(os.path.dirname(__file__)), "config/schema.yaml")
44class ApdbSQLiteTestCase(unittest.TestCase, ApdbTest):
45 """A test case for ApdbSql class using SQLite backend."""
47 fsrc_requires_id_list = True
48 dia_object_index = "baseline"
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)
61 def getDiaObjects_table(self) -> ApdbTables:
62 """Return type of table returned from getDiaObjects method."""
63 return ApdbTables.DiaObject
66class ApdbSQLiteTestCaseLastObject(ApdbSQLiteTestCase):
67 """A test case for ApdbSql class using SQLite backend and DiaObjectLast
68 table.
69 """
71 dia_object_index = "last_object_table"
73 def getDiaObjects_table(self) -> ApdbTables:
74 """Return type of table returned from getDiaObjects method."""
75 return ApdbTables.DiaObjectLast
78class ApdbSQLiteTestCasePixIdIovIndex(ApdbSQLiteTestCase):
79 """A test case for ApdbSql class using SQLite backend with pix_id_iov
80 indexing.
81 """
83 dia_object_index = "pix_id_iov"
86class ApdbSQLiteTestCaseInsertIds(ApdbSQLiteTestCase):
87 """Test case for ApdbSql class using SQLite backend with use_insert_id."""
89 use_insert_id = True
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."""
96 fsrc_requires_id_list = True
97 dia_object_index = "last_object_table"
98 postgresql: Any
99 use_insert_id = True
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()
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()
115 def setUp(self) -> None:
116 self.server = self.postgresql()
118 def tearDown(self) -> None:
119 self.server = self.postgresql()
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 "use_insert_id": self.use_insert_id,
128 }
129 kw.update(kwargs)
130 return ApdbSqlConfig(**kw)
132 def getDiaObjects_table(self) -> ApdbTables:
133 """Return type of table returned from getDiaObjects method."""
134 return ApdbTables.DiaObjectLast
137@unittest.skipUnless(testing is not None, "testing.postgresql module not found")
138class ApdbPostgresNamespaceTestCase(ApdbPostgresTestCase):
139 """A test case for ApdbSql class using Postgres backend with schema name"""
141 # use mixed case to trigger quoting
142 namespace = "ApdbSchema"
144 def make_config(self, **kwargs: Any) -> ApdbConfig:
145 """Make config class instance used in all tests."""
146 return super().make_config(namespace=self.namespace, **kwargs)
149class ApdbSchemaUpdateSQLiteTestCase(unittest.TestCase, ApdbSchemaUpdateTest):
150 """A test case for schema updates using SQLite backend."""
152 def setUp(self) -> None:
153 self.tempdir = tempfile.mkdtemp()
154 self.db_url = f"sqlite:///{self.tempdir}/apdb.sqlite3"
156 def tearDown(self) -> None:
157 shutil.rmtree(self.tempdir, ignore_errors=True)
159 def make_config(self, **kwargs: Any) -> ApdbConfig:
160 """Make config class instance used in all tests."""
161 kw = {
162 "db_url": self.db_url,
163 "schema_file": TEST_SCHEMA,
164 }
165 kw.update(kwargs)
166 return ApdbSqlConfig(**kw)
169class MyMemoryTestCase(lsst.utils.tests.MemoryTestCase):
170 """Run file leak tests."""
173def setup_module(module: Any) -> None:
174 """Configure pytest."""
175 lsst.utils.tests.init()
178if __name__ == "__main__": 178 ↛ 179line 178 didn't jump to line 179, because the condition on line 178 was never true
179 lsst.utils.tests.init()
180 unittest.main()