Coverage for tests/test_apdbSqlSchema.py : 32%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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
28from lsst.dax.apdb.apdbSqlSchema import ApdbSqlSchema
29from lsst.utils import getPackageDir
30import lsst.utils.tests
31from sqlalchemy import create_engine
34def _data_file_name(basename):
35 """Return path name of a data file.
36 """
37 return os.path.join(getPackageDir("dax_apdb"), "data", basename)
40class ApdbSchemaTestCase(unittest.TestCase):
41 """A test case for ApdbSqlSchema class
42 """
44 @classmethod
45 def setUpClass(cls):
46 pass
48 def _assertTable(self, table, name, ncol):
49 """validation for tables schema.
51 Parameters
52 ----------
53 table : `sqlalchemy.Table`
54 name : `str`
55 Expected table name
56 ncol : `int`
57 Expected number of columns
58 """
59 self.assertIsNotNone(table)
60 self.assertEqual(table.name, name)
61 self.assertEqual(len(table.columns), ncol)
63 def test_makeSchema(self):
64 """Test for creating schemas.
66 Schema is defined in YAML files, some checks here depend on that
67 configuration and will need to be updated when configuration changes.
68 """
69 engine = create_engine('sqlite://')
71 # create standard (baseline) schema
72 schema = ApdbSqlSchema(engine=engine,
73 dia_object_index="baseline",
74 schema_file=_data_file_name("apdb-schema.yaml"))
75 schema.makeSchema()
76 self._assertTable(schema.objects, "DiaObject", 92)
77 self.assertEqual(len(schema.objects.primary_key), 2)
78 self.assertIsNone(schema.objects_last)
79 self._assertTable(schema.sources, "DiaSource", 108)
80 self._assertTable(schema.forcedSources, "DiaForcedSource", 8)
82 # create schema using prefix
83 schema = ApdbSqlSchema(engine=engine,
84 dia_object_index="baseline",
85 schema_file=_data_file_name("apdb-schema.yaml"),
86 prefix="Pfx")
87 # Drop existing tables (but we don't check it here)
88 schema.makeSchema(drop=True)
89 self._assertTable(schema.objects, "PfxDiaObject", 92)
90 self.assertIsNone(schema.objects_last)
91 self._assertTable(schema.sources, "PfxDiaSource", 108)
92 self._assertTable(schema.forcedSources, "PfxDiaForcedSource", 8)
94 # use different indexing for DiaObject, need extra schema for that
95 schema = ApdbSqlSchema(engine=engine,
96 dia_object_index="pix_id_iov",
97 schema_file=_data_file_name("apdb-schema.yaml"),
98 extra_schema_file=_data_file_name("apdb-schema-extra.yaml"))
99 schema.makeSchema(drop=True)
100 self._assertTable(schema.objects, "DiaObject", 94)
101 self.assertEqual(len(schema.objects.primary_key), 3)
102 self.assertIsNone(schema.objects_last)
103 self._assertTable(schema.sources, "DiaSource", 108)
104 self._assertTable(schema.forcedSources, "DiaForcedSource", 8)
106 # use DiaObjectLast table for DiaObject, need extra schema for that
107 schema = ApdbSqlSchema(engine=engine,
108 dia_object_index="last_object_table",
109 schema_file=_data_file_name("apdb-schema.yaml"),
110 extra_schema_file=_data_file_name("apdb-schema-extra.yaml"))
111 schema.makeSchema(drop=True)
112 self._assertTable(schema.objects, "DiaObject", 94)
113 self.assertEqual(len(schema.objects.primary_key), 2)
114 self._assertTable(schema.objects_last, "DiaObjectLast", 18)
115 self.assertEqual(len(schema.objects_last.primary_key), 2)
116 self._assertTable(schema.sources, "DiaSource", 108)
117 self._assertTable(schema.forcedSources, "DiaForcedSource", 8)
120class MyMemoryTestCase(lsst.utils.tests.MemoryTestCase):
121 pass
124def setup_module(module):
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()