Coverage for tests/test_ap_verify_queries.py: 41%

42 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-27 03:01 -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/>. 

21 

22import os 

23import unittest.mock 

24from collections.abc import Mapping 

25from typing import Any 

26 

27import astropy.time 

28import lsst.utils.tests 

29import numpy 

30import pandas 

31from lsst.dax.apdb.sql import ApdbSql, ApdbSqlConfig 

32 

33TEST_SCHEMA = os.path.join(os.path.abspath(os.path.dirname(__file__)), "config/schema.yaml") 

34 

35 

36def createTestObjects( 

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

38) -> pandas.DataFrame: 

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

40 

41 Parameters 

42 ---------- 

43 n_objects : `int` 

44 Number of objects to create. 

45 id_column_name : `str` 

46 Name of the ID column. 

47 extra_fields : `dict` 

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

49 

50 Returns 

51 ------- 

52 sources : `pandas.DataFrame` 

53 Tests sources with filled values. 

54 """ 

55 one_degree = numpy.pi / 180 

56 data = { 

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

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

59 "dec": numpy.full(n_objects, one_degree, 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 

65 

66 

67class TestApVerifyQueries(unittest.TestCase): 

68 """Tests for ap_verify queries.""" 

69 

70 def setUp(self) -> None: 

71 self.apdbCfg = ApdbSqlConfig() 

72 # Create DB in memory. 

73 self.apdbCfg.db_url = "sqlite://" 

74 self.apdbCfg.schema_file = TEST_SCHEMA 

75 self.apdbCfg.dia_object_index = "baseline" 

76 self.apdbCfg.dia_object_columns = [] 

77 self.apdb = ApdbSql(config=self.apdbCfg) 

78 self.apdb._schema.makeSchema() 

79 

80 def tearDown(self) -> None: 

81 del self.apdb 

82 

83 def test_count_zero_objects(self) -> None: 

84 value = self.apdb.countUnassociatedObjects() 

85 self.assertEqual(value, 0) 

86 

87 def test_count_objects(self) -> None: 

88 n_created = 5 

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

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

91 

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

93 self.apdb.store(dateTime, objects) 

94 

95 value = self.apdb.countUnassociatedObjects() 

96 self.assertEqual(n_created - 1, value) 

97 

98 

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

100 """Run file leak tests.""" 

101 

102 

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

104 """Configure pytest.""" 

105 lsst.utils.tests.init() 

106 

107 

108if __name__ == "__main__": 

109 lsst.utils.tests.init() 

110 unittest.main()