Coverage for tests/test_db.py: 38%
38 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-19 01:33 -0800
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-19 01:33 -0800
1# This file is part of felis.
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 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 <https://www.gnu.org/licenses/>.
22import os
23import tempfile
24from unittest import TestCase
26from sqlalchemy import MetaData, create_engine
28from felis import Schema
29from felis.db.schema import create_database
30from felis.db.utils import DatabaseContext
32TESTDIR = os.path.abspath(os.path.dirname(__file__))
33TESTFILE = os.path.join(TESTDIR, "data", "sales.yaml")
36class TestCreateDatabase(TestCase):
37 """Test the creation of a database from a schema using the
38 ``create_database`` utility function.
39 """
41 def check_tables(self, db: DatabaseContext, schema: Schema) -> None:
42 metadata = MetaData()
43 metadata.reflect(db.engine)
44 for table in schema.tables:
45 self.assertIn(table.name, metadata.tables)
47 def check_db(self, db: DatabaseContext, expected_dialect: str = "sqlite") -> None:
48 self.assertIsNotNone(db.engine)
49 self.assertIsNotNone(db.metadata)
50 self.assertIsNotNone(db.connection)
51 self.assertEqual(db.dialect_name, expected_dialect)
53 def test_sqlite_engine_str(self) -> None:
54 """Test creating a database with a SQLite engine using a URI string."""
55 with tempfile.NamedTemporaryFile(delete=True, suffix=".sqlite3") as tmpfile:
56 schema = Schema.from_uri(TESTFILE)
57 db = create_database(schema, f"sqlite:///{tmpfile.name}")
58 self.check_db(db)
59 self.check_tables(db, schema)
61 def test_sqlite_in_memory(self) -> None:
62 """Test creating a database with a SQLite engine using an in-memory
63 database.
64 """
65 schema = Schema.from_uri(TESTFILE)
66 db = create_database(schema)
67 self.check_db(db)
68 self.check_tables(db, schema)
70 def test_sqlite_engine(self) -> None:
71 """Test creating a database with a SQLite engine using an SQLAlchemy
72 engine object.
73 """
74 with tempfile.NamedTemporaryFile(delete=True, suffix=".sqlite3") as tmpfile:
75 schema = Schema.from_uri(TESTFILE)
76 engine = create_engine(f"sqlite:///{tmpfile.name}")
77 db = create_database(schema, engine)
78 self.check_db(db)
79 self.check_tables(db, schema)