Coverage for tests/test_apdbSql.py: 69%
83 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-03-22 02:30 -0700
« prev ^ index » next coverage.py v7.4.4, created at 2024-03-22 02:30 -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/>.
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(ApdbTest, unittest.TestCase):
45 """A test case for ApdbSql class using SQLite backend."""
47 fsrc_requires_id_list = True
48 dia_object_index = "baseline"
49 allow_visit_query = False
51 def setUp(self) -> None:
52 self.tempdir = tempfile.mkdtemp()
53 self.db_url = f"sqlite:///{self.tempdir}/apdb.sqlite3"
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)
66 def getDiaObjects_table(self) -> ApdbTables:
67 """Return type of table returned from getDiaObjects method."""
68 return ApdbTables.DiaObject
71class ApdbSQLiteTestCaseLastObject(ApdbSQLiteTestCase):
72 """A test case for ApdbSql class using SQLite backend and DiaObjectLast
73 table.
74 """
76 dia_object_index = "last_object_table"
78 def getDiaObjects_table(self) -> ApdbTables:
79 """Return type of table returned from getDiaObjects method."""
80 return ApdbTables.DiaObjectLast
83class ApdbSQLiteTestCasePixIdIovIndex(ApdbSQLiteTestCase):
84 """A test case for ApdbSql class using SQLite backend with pix_id_iov
85 indexing.
86 """
88 dia_object_index = "pix_id_iov"
91class ApdbSQLiteTestCaseInsertIds(ApdbSQLiteTestCase):
92 """Test case for ApdbSql class using SQLite backend with use_insert_id."""
94 use_insert_id = True
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."""
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
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()
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()
121 def setUp(self) -> None:
122 self.server = self.postgresql()
124 def tearDown(self) -> None:
125 self.server = self.postgresql()
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)
138 def getDiaObjects_table(self) -> ApdbTables:
139 """Return type of table returned from getDiaObjects method."""
140 return ApdbTables.DiaObjectLast
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"""
147 # use mixed case to trigger quoting
148 namespace = "ApdbSchema"
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)
155class ApdbSchemaUpdateSQLiteTestCase(ApdbSchemaUpdateTest, unittest.TestCase):
156 """A test case for schema updates using SQLite backend."""
158 def setUp(self) -> None:
159 self.tempdir = tempfile.mkdtemp()
160 self.db_url = f"sqlite:///{self.tempdir}/apdb.sqlite3"
162 def tearDown(self) -> None:
163 shutil.rmtree(self.tempdir, ignore_errors=True)
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)
175class MyMemoryTestCase(lsst.utils.tests.MemoryTestCase):
176 """Run file leak tests."""
179def setup_module(module: Any) -> None:
180 """Configure pytest."""
181 lsst.utils.tests.init()
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()