Coverage for tests/test_ap_verify_queries.py: 44%

46 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-11 02:40 -0800

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 numpy 

23import os 

24import pandas 

25import unittest.mock 

26import lsst.utils.tests 

27from collections.abc import Mapping 

28from typing import Any 

29 

30import lsst.geom as geom 

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

65 

66 

67class TestApVerifyQueries(unittest.TestCase): 

68 

69 def setUp(self) -> None: 

70 self.apdbCfg = ApdbSqlConfig() 

71 # Create DB in memory. 

72 self.apdbCfg.db_url = 'sqlite://' 

73 self.apdbCfg.schema_file = TEST_SCHEMA 

74 self.apdbCfg.dia_object_index = "baseline" 

75 self.apdbCfg.dia_object_columns = [] 

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

77 self.apdb._schema.makeSchema() 

78 

79 def tearDown(self) -> None: 

80 del self.apdb 

81 

82 def test_count_zero_objects(self) -> None: 

83 value = self.apdb.countUnassociatedObjects() 

84 self.assertEqual(value, 0) 

85 

86 def test_count_objects(self) -> None: 

87 n_created = 5 

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

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

90 

91 # nsecs must be an integer, not 1.4e18 

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

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 pass 

101 

102 

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

104 lsst.utils.tests.init() 

105 

106 

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

108 lsst.utils.tests.init() 

109 unittest.main()