Coverage for tests/test_ap_verify_queries.py: 43%

45 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-01-31 10:44 +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 unittest.mock 

24from collections.abc import Mapping 

25from typing import Any 

26 

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 

33 

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

35 

36 

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. 

41 

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. 

50 

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 "dec": 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 

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 # nsecs must be an integer, not 1.4e18 

93 dateTime = DateTime(nsecs=1400000000 * 10**9) 

94 self.apdb.store(dateTime, objects) 

95 

96 value = self.apdb.countUnassociatedObjects() 

97 self.assertEqual(n_created - 1, value) 

98 

99 

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

101 """Run file leak tests.""" 

102 

103 

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

105 """Configure pytest.""" 

106 lsst.utils.tests.init() 

107 

108 

109if __name__ == "__main__": 109 ↛ 110line 109 didn't jump to line 110, because the condition on line 109 was never true

110 lsst.utils.tests.init() 

111 unittest.main()