Coverage for tests/test_apdbSqlSchema.py: 22%
76 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-06 02:50 -0700
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-06 02:50 -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.apdbSchema import ApdbTables
32from lsst.dax.apdb.apdbSqlSchema import ApdbSqlSchema, ExtraTables
33from sqlalchemy import create_engine
35TEST_SCHEMA = os.path.join(os.path.abspath(os.path.dirname(__file__)), "config/schema.yaml")
38class ApdbSchemaTestCase(unittest.TestCase):
39 """Test case for ApdbSqlSchema class."""
41 # number of columns as defined in tests/config/schema.yaml
42 table_column_count = {
43 ApdbTables.DiaObject: 8,
44 ApdbTables.DiaObjectLast: 5,
45 ApdbTables.DiaSource: 10,
46 ApdbTables.DiaForcedSource: 4,
47 ApdbTables.SSObject: 3,
48 }
50 def _assertTable(self, table: sqlalchemy.schema.Table, name: str, ncol: int) -> None:
51 """validation for tables schema.
53 Parameters
54 ----------
55 table : `sqlalchemy.Table`
56 name : `str`
57 Expected table name
58 ncol : `int`
59 Expected number of columns
60 """
61 self.assertIsNotNone(table)
62 self.assertEqual(table.name, name)
63 self.assertEqual(len(table.columns), ncol)
65 def test_makeSchema(self) -> None:
66 """Test for creating schemas.
68 Schema is defined in YAML files, some checks here depend on that
69 configuration and will need to be updated when configuration changes.
70 """
71 engine = create_engine("sqlite://")
73 # create standard (baseline) schema
74 schema = ApdbSqlSchema(
75 engine=engine, dia_object_index="baseline", htm_index_column="pixelId", schema_file=TEST_SCHEMA
76 )
77 schema.makeSchema()
78 table = schema.get_table(ApdbTables.DiaObject)
79 # DiaObject table adds pixelId column.
80 self._assertTable(table, "DiaObject", self.table_column_count[ApdbTables.DiaObject] + 1)
81 self.assertEqual(len(table.primary_key), 2)
82 self.assertEqual(
83 len(schema.get_apdb_columns(ApdbTables.DiaObject)), self.table_column_count[ApdbTables.DiaObject]
84 )
85 with self.assertRaisesRegex(ValueError, ".*does not exist in the schema"):
86 schema.get_table(ApdbTables.DiaObjectLast)
87 # DiaSource table also adds pixelId column.
88 self._assertTable(
89 schema.get_table(ApdbTables.DiaSource),
90 "DiaSource",
91 self.table_column_count[ApdbTables.DiaSource] + 1,
92 )
93 self.assertEqual(
94 len(schema.get_apdb_columns(ApdbTables.DiaSource)), self.table_column_count[ApdbTables.DiaSource]
95 )
96 self._assertTable(
97 schema.get_table(ApdbTables.DiaForcedSource),
98 "DiaForcedSource",
99 self.table_column_count[ApdbTables.DiaForcedSource],
100 )
101 self.assertEqual(
102 len(schema.get_apdb_columns(ApdbTables.DiaForcedSource)),
103 self.table_column_count[ApdbTables.DiaForcedSource],
104 )
105 for table_enum in ExtraTables:
106 with self.assertRaisesRegex(ValueError, ".*does not exist in the schema"):
107 schema.get_table(table_enum)
109 # create schema using prefix
110 schema = ApdbSqlSchema(
111 engine=engine,
112 dia_object_index="baseline",
113 htm_index_column="pixelId",
114 schema_file=TEST_SCHEMA,
115 prefix="Pfx",
116 )
117 # Drop existing tables (but we don't check it here)
118 schema.makeSchema(drop=True)
119 self._assertTable(
120 schema.get_table(ApdbTables.DiaObject),
121 "PfxDiaObject",
122 self.table_column_count[ApdbTables.DiaObject] + 1,
123 )
124 with self.assertRaisesRegex(ValueError, ".*does not exist in the schema"):
125 schema.get_table(ApdbTables.DiaObjectLast)
126 self._assertTable(
127 schema.get_table(ApdbTables.DiaSource),
128 "PfxDiaSource",
129 self.table_column_count[ApdbTables.DiaSource] + 1,
130 )
131 self._assertTable(
132 schema.get_table(ApdbTables.DiaForcedSource),
133 "PfxDiaForcedSource",
134 self.table_column_count[ApdbTables.DiaForcedSource],
135 )
137 # use different indexing for DiaObject, changes number of PK columns
138 schema = ApdbSqlSchema(
139 engine=engine, dia_object_index="pix_id_iov", htm_index_column="pixelId", schema_file=TEST_SCHEMA
140 )
141 schema.makeSchema(drop=True)
142 table = schema.get_table(ApdbTables.DiaObject)
143 self._assertTable(table, "DiaObject", self.table_column_count[ApdbTables.DiaObject] + 1)
144 self.assertEqual(len(table.primary_key), 3)
145 with self.assertRaisesRegex(ValueError, ".*does not exist in the schema"):
146 schema.get_table(ApdbTables.DiaObjectLast)
147 self._assertTable(
148 schema.get_table(ApdbTables.DiaSource),
149 "DiaSource",
150 self.table_column_count[ApdbTables.DiaSource] + 1,
151 )
152 self._assertTable(
153 schema.get_table(ApdbTables.DiaForcedSource),
154 "DiaForcedSource",
155 self.table_column_count[ApdbTables.DiaForcedSource],
156 )
158 # use DiaObjectLast table for DiaObject
159 schema = ApdbSqlSchema(
160 engine=engine,
161 dia_object_index="last_object_table",
162 htm_index_column="pixelId",
163 schema_file=TEST_SCHEMA,
164 )
165 schema.makeSchema(drop=True)
166 table = schema.get_table(ApdbTables.DiaObject)
167 self._assertTable(table, "DiaObject", self.table_column_count[ApdbTables.DiaObject] + 1)
168 self.assertEqual(len(table.primary_key), 2)
169 table = schema.get_table(ApdbTables.DiaObjectLast)
170 self._assertTable(table, "DiaObjectLast", self.table_column_count[ApdbTables.DiaObjectLast] + 1)
171 self.assertEqual(len(table.primary_key), 2)
172 self._assertTable(
173 schema.get_table(ApdbTables.DiaSource),
174 "DiaSource",
175 self.table_column_count[ApdbTables.DiaSource] + 1,
176 )
177 self._assertTable(
178 schema.get_table(ApdbTables.DiaForcedSource),
179 "DiaForcedSource",
180 self.table_column_count[ApdbTables.DiaForcedSource],
181 )
183 # Add history_id tables
184 schema = ApdbSqlSchema(
185 engine=engine,
186 dia_object_index="last_object_table",
187 htm_index_column="pixelId",
188 schema_file=TEST_SCHEMA,
189 use_insert_id=True,
190 )
191 schema.makeSchema(drop=True)
192 self._assertTable(schema.get_table(ExtraTables.DiaInsertId), "DiaInsertId", 2)
193 self.assertEqual(len(schema.get_apdb_columns(ExtraTables.DiaInsertId)), 2)
194 self._assertTable(schema.get_table(ExtraTables.DiaObjectInsertId), "DiaObjectInsertId", 3)
195 self.assertEqual(len(schema.get_apdb_columns(ExtraTables.DiaObjectInsertId)), 3)
196 self._assertTable(schema.get_table(ExtraTables.DiaSourceInsertId), "DiaSourceInsertId", 2)
197 self.assertEqual(len(schema.get_apdb_columns(ExtraTables.DiaSourceInsertId)), 2)
198 self._assertTable(schema.get_table(ExtraTables.DiaForcedSourceInsertId), "DiaFSourceInsertId", 3)
199 self.assertEqual(len(schema.get_apdb_columns(ExtraTables.DiaForcedSourceInsertId)), 3)
202class MyMemoryTestCase(lsst.utils.tests.MemoryTestCase):
203 pass
206def setup_module(module: Any) -> None:
207 lsst.utils.tests.init()
210if __name__ == "__main__": 210 ↛ 211line 210 didn't jump to line 211, because the condition on line 210 was never true
211 lsst.utils.tests.init()
212 unittest.main()