Coverage for tests/test_ap_verify_queries.py: 44%
46 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-04-14 02:15 -0700
« prev ^ index » next coverage.py v6.5.0, created at 2023-04-14 02:15 -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# (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 unittest.mock
24from collections.abc import Mapping
25from typing import Any
27import lsst.geom as geom
28import lsst.utils.tests
29import numpy
30import pandas
31from lsst.daf.base import DateTime
32from lsst.dax.apdb import ApdbSql, ApdbSqlConfig
34TEST_SCHEMA = os.path.join(os.path.abspath(os.path.dirname(__file__)), "config/schema.yaml")
37def createTestObjects(
38 n_objects: int, id_column_name: str, extra_fields: Mapping[str, Any]
39) -> pandas.DataFrame:
40 """Create test objects to store in the ApdbSql.
42 Parameters
43 ----------
44 n_objects : `int`
45 Number of objects to create.
46 id_column_name : `str`
47 Name of the ID column.
48 extra_fields : `dict`
49 A `dict` whose keys are field names and whose values are their types.
51 Returns
52 -------
53 sources : `pandas.DataFrame`
54 Tests sources with filled values.
55 """
56 data = {
57 id_column_name: numpy.arange(n_objects, dtype=numpy.int64),
58 "ra": numpy.full(n_objects, 1 * geom.degrees, dtype=numpy.float64),
59 "decl": numpy.full(n_objects, 1 * geom.degrees, dtype=numpy.float64),
60 }
61 for field, type in extra_fields.items():
62 data[field] = numpy.ones(n_objects, dtype=type)
63 df = pandas.DataFrame(data)
64 return df
67class TestApVerifyQueries(unittest.TestCase):
68 def setUp(self) -> None:
69 self.apdbCfg = ApdbSqlConfig()
70 # Create DB in memory.
71 self.apdbCfg.db_url = "sqlite://"
72 self.apdbCfg.schema_file = TEST_SCHEMA
73 self.apdbCfg.dia_object_index = "baseline"
74 self.apdbCfg.dia_object_columns = []
75 self.apdb = ApdbSql(config=self.apdbCfg)
76 self.apdb._schema.makeSchema()
78 def tearDown(self) -> None:
79 del self.apdb
81 def test_count_zero_objects(self) -> None:
82 value = self.apdb.countUnassociatedObjects()
83 self.assertEqual(value, 0)
85 def test_count_objects(self) -> None:
86 n_created = 5
87 objects = createTestObjects(n_created, "diaObjectId", {"nDiaSources": int})
88 objects.at[n_created - 1, "nDiaSources"] = 2
90 # nsecs must be an integer, not 1.4e18
91 dateTime = DateTime(nsecs=1400000000 * 10**9)
92 self.apdb.store(dateTime, objects)
94 value = self.apdb.countUnassociatedObjects()
95 self.assertEqual(n_created - 1, value)
98class MemoryTester(lsst.utils.tests.MemoryTestCase):
99 pass
102def setup_module(module: Any) -> None:
103 lsst.utils.tests.init()
106if __name__ == "__main__": 106 ↛ 107line 106 didn't jump to line 107, because the condition on line 106 was never true
107 lsst.utils.tests.init()
108 unittest.main()