Coverage for tests/test_apdbSql.py: 70%
80 statements
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-06 10:33 +0000
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-06 10:33 +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(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 make_config(self, **kwargs: Any) -> ApdbConfig:
52 """Make config class instance used in all tests."""
53 kw = {
54 "db_url": "sqlite://",
55 "schema_file": TEST_SCHEMA,
56 "dia_object_index": self.dia_object_index,
57 "use_insert_id": self.use_insert_id,
58 }
59 kw.update(kwargs)
60 return ApdbSqlConfig(**kw)
62 def getDiaObjects_table(self) -> ApdbTables:
63 """Return type of table returned from getDiaObjects method."""
64 return ApdbTables.DiaObject
67class ApdbSQLiteTestCaseLastObject(ApdbSQLiteTestCase):
68 """A test case for ApdbSql class using SQLite backend and DiaObjectLast
69 table.
70 """
72 dia_object_index = "last_object_table"
74 def getDiaObjects_table(self) -> ApdbTables:
75 """Return type of table returned from getDiaObjects method."""
76 return ApdbTables.DiaObjectLast
79class ApdbSQLiteTestCasePixIdIovIndex(ApdbSQLiteTestCase):
80 """A test case for ApdbSql class using SQLite backend with pix_id_iov
81 indexing.
82 """
84 dia_object_index = "pix_id_iov"
87class ApdbSQLiteTestCaseInsertIds(ApdbSQLiteTestCase):
88 """Test case for ApdbSql class using SQLite backend with use_insert_id."""
90 use_insert_id = True
93@unittest.skipUnless(testing is not None, "testing.postgresql module not found")
94class ApdbPostgresTestCase(ApdbTest, unittest.TestCase):
95 """A test case for ApdbSql class using Postgres backend."""
97 fsrc_requires_id_list = True
98 dia_object_index = "last_object_table"
99 postgresql: Any
100 use_insert_id = True
101 allow_visit_query = False
103 @classmethod
104 def setUpClass(cls) -> None:
105 # Create the postgres test server.
106 cls.postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True)
107 super().setUpClass()
109 @classmethod
110 def tearDownClass(cls) -> None:
111 # Clean up any lingering SQLAlchemy engines/connections
112 # so they're closed before we shut down the server.
113 gc.collect()
114 cls.postgresql.clear_cache()
115 super().tearDownClass()
117 def setUp(self) -> None:
118 self.server = self.postgresql()
120 def tearDown(self) -> None:
121 self.server = self.postgresql()
123 def make_config(self, **kwargs: Any) -> ApdbConfig:
124 """Make config class instance used in all tests."""
125 kw = {
126 "db_url": self.server.url(),
127 "schema_file": TEST_SCHEMA,
128 "dia_object_index": self.dia_object_index,
129 "use_insert_id": self.use_insert_id,
130 }
131 kw.update(kwargs)
132 return ApdbSqlConfig(**kw)
134 def getDiaObjects_table(self) -> ApdbTables:
135 """Return type of table returned from getDiaObjects method."""
136 return ApdbTables.DiaObjectLast
139@unittest.skipUnless(testing is not None, "testing.postgresql module not found")
140class ApdbPostgresNamespaceTestCase(ApdbPostgresTestCase):
141 """A test case for ApdbSql class using Postgres backend with schema name"""
143 # use mixed case to trigger quoting
144 namespace = "ApdbSchema"
146 def make_config(self, **kwargs: Any) -> ApdbConfig:
147 """Make config class instance used in all tests."""
148 return super().make_config(namespace=self.namespace, **kwargs)
151class ApdbSchemaUpdateSQLiteTestCase(ApdbSchemaUpdateTest, unittest.TestCase):
152 """A test case for schema updates using SQLite backend."""
154 def setUp(self) -> None:
155 self.tempdir = tempfile.mkdtemp()
156 self.db_url = f"sqlite:///{self.tempdir}/apdb.sqlite3"
158 def tearDown(self) -> None:
159 shutil.rmtree(self.tempdir, ignore_errors=True)
161 def make_config(self, **kwargs: Any) -> ApdbConfig:
162 """Make config class instance used in all tests."""
163 kw = {
164 "db_url": self.db_url,
165 "schema_file": TEST_SCHEMA,
166 }
167 kw.update(kwargs)
168 return ApdbSqlConfig(**kw)
171class MyMemoryTestCase(lsst.utils.tests.MemoryTestCase):
172 """Run file leak tests."""
175def setup_module(module: Any) -> None:
176 """Configure pytest."""
177 lsst.utils.tests.init()
180if __name__ == "__main__": 180 ↛ 181line 180 didn't jump to line 181, because the condition on line 180 was never true
181 lsst.utils.tests.init()
182 unittest.main()