Coverage for tests/test_query_direct_postgresql.py: 64%
50 statements
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-08 02:50 -0700
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-08 02:50 -0700
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://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 software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <https://www.gnu.org/licenses/>.
28"""Tests for DirectButler._query with PostgreSQL.
29"""
31from __future__ import annotations
33import gc
34import os
35import secrets
36import unittest
38try:
39 # It's possible but silly to have testing.postgresql installed without
40 # having the postgresql server installed (because then nothing in
41 # testing.postgresql would work), so we use the presence of that module
42 # to test whether we can expect the server to be available.
43 import testing.postgresql
44except ImportError:
45 testing = None
46import sqlalchemy
47from lsst.daf.butler import Butler, ButlerConfig, StorageClassFactory
48from lsst.daf.butler.datastore import NullDatastore
49from lsst.daf.butler.direct_butler import DirectButler
50from lsst.daf.butler.registry.sql_registry import SqlRegistry
51from lsst.daf.butler.tests.butler_queries import ButlerQueryTests
52from lsst.daf.butler.tests.utils import makeTestTempDir, removeTestTempDir
54TESTDIR = os.path.abspath(os.path.dirname(__file__))
57def _start_server(root):
58 """Start a PostgreSQL server and create a database within it, returning
59 an object encapsulating both.
60 """
61 server = testing.postgresql.Postgresql(base_dir=root)
62 engine = sqlalchemy.create_engine(server.url())
63 with engine.begin() as connection:
64 connection.execute(sqlalchemy.text("CREATE EXTENSION btree_gist;"))
65 return server
68@unittest.skipUnless(testing is not None, "testing.postgresql module not found")
69class DirectButlerPostgreSQLTests(ButlerQueryTests, unittest.TestCase):
70 """Tests for DirectButler._query with PostgreSQL."""
72 data_dir = os.path.join(TESTDIR, "data/registry")
74 @classmethod
75 def setUpClass(cls):
76 cls.root = makeTestTempDir(TESTDIR)
77 cls.server = _start_server(cls.root)
79 @classmethod
80 def tearDownClass(cls):
81 # Clean up any lingering SQLAlchemy engines/connections
82 # so they're closed before we shut down the server.
83 gc.collect()
84 cls.server.stop()
85 removeTestTempDir(cls.root)
87 def make_butler(self, *args: str) -> Butler:
88 config = ButlerConfig()
89 config[".registry.db"] = self.server.url()
90 config[".registry.namespace"] = f"namespace_{secrets.token_hex(8).lower()}"
91 registry = SqlRegistry.createFromConfig(config)
92 for arg in args:
93 self.load_data(registry, arg)
94 return DirectButler(
95 config=config,
96 registry=registry,
97 datastore=NullDatastore(None, None),
98 storageClasses=StorageClassFactory(),
99 )
101 # TODO (DM-43697): these tests fail due to something going awry with cursor
102 # and temporary table lifetime management; PostgreSQL says we can't drop
103 # temp tables because there are still active queries against them. The
104 # logic looks fine but is obfuscated by the many levels of competing
105 # context managers in the Database class, so we'll punt this to DM-43697.
107 @unittest.expectedFailure
108 def test_data_coordinate_upload_force_temp_table(self) -> None:
109 super().test_data_coordinate_upload_force_temp_table()
111 @unittest.expectedFailure
112 def test_materialization(self) -> None:
113 return super().test_materialization()
116if __name__ == "__main__":
117 unittest.main()