Coverage for tests / test_ap_verify_queries.py: 47%

41 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-01 08:19 +0000

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/>. 

21 

22import os 

23import tempfile 

24import unittest.mock 

25from collections.abc import Mapping 

26from typing import Any 

27 

28import astropy.time 

29import numpy 

30import pandas 

31 

32import lsst.utils.tests 

33from lsst.dax.apdb import Apdb 

34from lsst.dax.apdb.sql import ApdbSql 

35 

36TEST_SCHEMA = os.path.join(os.path.abspath(os.path.dirname(__file__)), "config/schema-apdb+sso.yaml") 

37 

38 

39def createTestObjects( 

40 n_objects: int, id_column_name: str, extra_fields: Mapping[str, Any] 

41) -> pandas.DataFrame: 

42 """Create test objects to store in the ApdbSql. 

43 

44 Parameters 

45 ---------- 

46 n_objects : `int` 

47 Number of objects to create. 

48 id_column_name : `str` 

49 Name of the ID column. 

50 extra_fields : `dict` 

51 A `dict` whose keys are field names and whose values are their types. 

52 

53 Returns 

54 ------- 

55 sources : `pandas.DataFrame` 

56 Tests sources with filled values. 

57 """ 

58 one_degree = numpy.pi / 180 

59 data = { 

60 id_column_name: numpy.arange(n_objects, dtype=numpy.int64), 

61 "ra": numpy.full(n_objects, one_degree, dtype=numpy.float64), 

62 "dec": numpy.full(n_objects, one_degree, dtype=numpy.float64), 

63 } 

64 for field, type in extra_fields.items(): 

65 data[field] = numpy.ones(n_objects, dtype=type) 

66 df = pandas.DataFrame(data) 

67 return df 

68 

69 

70class TestApVerifyQueries(unittest.TestCase): 

71 """Tests for ap_verify queries.""" 

72 

73 def setUp(self) -> None: 

74 # Create database. 

75 sqlite_file = tempfile.NamedTemporaryFile() 

76 self.addCleanup(sqlite_file.close) 

77 apdb_cfg = ApdbSql.init_database( 

78 db_url=f"sqlite:///{sqlite_file.name}", 

79 schema_file=TEST_SCHEMA, 

80 ) 

81 self.apdb = Apdb.from_config(apdb_cfg) 

82 

83 def tearDown(self) -> None: 

84 del self.apdb 

85 

86 def test_count_zero_objects(self) -> None: 

87 value = self.apdb.countUnassociatedObjects() 

88 self.assertEqual(value, 0) 

89 

90 def test_count_objects(self) -> None: 

91 n_created = 5 

92 objects = createTestObjects(n_created, "diaObjectId", {"nDiaSources": int}) 

93 objects.at[n_created - 1, "nDiaSources"] = 2 

94 

95 dateTime = astropy.time.Time(1400000000, format="unix_tai") 

96 self.apdb.store(dateTime, objects) 

97 

98 value = self.apdb.countUnassociatedObjects() 

99 self.assertEqual(n_created - 1, value) 

100 

101 

102class MemoryTester(lsst.utils.tests.MemoryTestCase): 

103 """Run file leak tests.""" 

104 

105 

106def setup_module(module: Any) -> None: 

107 """Configure pytest.""" 

108 lsst.utils.tests.init() 

109 

110 

111if __name__ == "__main__": 

112 lsst.utils.tests.init() 

113 unittest.main()