Coverage for tests/test_apdbSqlSchema.py: 33%
55 statements
« prev ^ index » next coverage.py v6.5.0, created at 2024-03-20 00:41 -0700
« prev ^ index » next coverage.py v6.5.0, created at 2024-03-20 00:41 -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 ApdbSqlSchema class.
23"""
25import os
26import unittest
27from typing import Any
29import lsst.utils.tests
30import sqlalchemy
31from lsst.dax.apdb.apdbSqlSchema import ApdbSqlSchema
32from sqlalchemy import create_engine
34TEST_SCHEMA = os.path.join(os.path.abspath(os.path.dirname(__file__)), "config/schema.yaml")
37class ApdbSchemaTestCase(unittest.TestCase):
38 """A test case for ApdbSqlSchema class
39 """
41 @classmethod
42 def setUpClass(cls) -> None:
43 pass
45 def _assertTable(self, table: sqlalchemy.schema.Table, name: str, ncol: int) -> None:
46 """validation for tables schema.
48 Parameters
49 ----------
50 table : `sqlalchemy.Table`
51 name : `str`
52 Expected table name
53 ncol : `int`
54 Expected number of columns
55 """
56 self.assertIsNotNone(table)
57 self.assertEqual(table.name, name)
58 self.assertEqual(len(table.columns), ncol)
60 def test_makeSchema(self) -> None:
61 """Test for creating schemas.
63 Schema is defined in YAML files, some checks here depend on that
64 configuration and will need to be updated when configuration changes.
65 """
66 engine = create_engine('sqlite://')
68 # create standard (baseline) schema
69 schema = ApdbSqlSchema(engine=engine,
70 dia_object_index="baseline",
71 htm_index_column="pixelId",
72 schema_file=TEST_SCHEMA)
73 schema.makeSchema()
74 self._assertTable(schema.objects, "DiaObject", 9)
75 self.assertEqual(len(schema.objects.primary_key), 2)
76 self.assertIsNone(schema.objects_last)
77 self._assertTable(schema.sources, "DiaSource", 11)
78 self._assertTable(schema.forcedSources, "DiaForcedSource", 4)
80 # create schema using prefix
81 schema = ApdbSqlSchema(engine=engine,
82 dia_object_index="baseline",
83 htm_index_column="pixelId",
84 schema_file=TEST_SCHEMA,
85 prefix="Pfx")
86 # Drop existing tables (but we don't check it here)
87 schema.makeSchema(drop=True)
88 self._assertTable(schema.objects, "PfxDiaObject", 9)
89 self.assertIsNone(schema.objects_last)
90 self._assertTable(schema.sources, "PfxDiaSource", 11)
91 self._assertTable(schema.forcedSources, "PfxDiaForcedSource", 4)
93 # use different indexing for DiaObject, need extra schema for that
94 schema = ApdbSqlSchema(engine=engine,
95 dia_object_index="pix_id_iov",
96 htm_index_column="pixelId",
97 schema_file=TEST_SCHEMA)
98 schema.makeSchema(drop=True)
99 self._assertTable(schema.objects, "DiaObject", 9)
100 self.assertEqual(len(schema.objects.primary_key), 3)
101 self.assertIsNone(schema.objects_last)
102 self._assertTable(schema.sources, "DiaSource", 11)
103 self._assertTable(schema.forcedSources, "DiaForcedSource", 4)
105 # use DiaObjectLast table for DiaObject
106 schema = ApdbSqlSchema(engine=engine,
107 dia_object_index="last_object_table",
108 htm_index_column="pixelId",
109 schema_file=TEST_SCHEMA)
110 schema.makeSchema(drop=True)
111 self._assertTable(schema.objects, "DiaObject", 9)
112 self.assertEqual(len(schema.objects.primary_key), 2)
113 self._assertTable(schema.objects_last, "DiaObjectLast", 6)
114 assert schema.objects_last is not None
115 self.assertEqual(len(schema.objects_last.primary_key), 2)
116 self._assertTable(schema.sources, "DiaSource", 11)
117 self._assertTable(schema.forcedSources, "DiaForcedSource", 4)
120class MyMemoryTestCase(lsst.utils.tests.MemoryTestCase):
121 pass
124def setup_module(module: Any) -> None:
125 lsst.utils.tests.init()
128if __name__ == "__main__": 128 ↛ 129line 128 didn't jump to line 129, because the condition on line 128 was never true
129 lsst.utils.tests.init()
130 unittest.main()