Coverage for tests/test_ap_verify_queries.py: 46%

44 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-08-18 11:51 -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 numpy 

23import os 

24import pandas 

25import unittest.mock 

26import lsst.utils.tests 

27 

28import lsst.geom as geom 

29from lsst.daf.base import DateTime 

30from lsst.dax.apdb import ApdbSql, ApdbSqlConfig 

31 

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

33 

34 

35def createTestObjects(n_objects, id_column_name, extra_fields): 

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

37 

38 Parameters 

39 ---------- 

40 n_objects : `int` 

41 Number of objects to create. 

42 id_column_name : `str` 

43 Name of the ID column. 

44 extra_fields : `dict` 

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

46 

47 Returns 

48 ------- 

49 sources : `pandas.DataFrame` 

50 Tests sources with filled values. 

51 """ 

52 data = { 

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

54 "ra": numpy.full(n_objects, 1 * geom.degrees, dtype=numpy.float64), 

55 "decl": numpy.full(n_objects, 1 * geom.degrees, dtype=numpy.float64), 

56 } 

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

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

59 df = pandas.DataFrame(data) 

60 return df 

61 

62 

63class TestApVerifyQueries(unittest.TestCase): 

64 

65 def setUp(self): 

66 self.apdbCfg = ApdbSqlConfig() 

67 # Create DB in memory. 

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

69 self.apdbCfg.schema_file = TEST_SCHEMA 

70 self.apdbCfg.dia_object_index = "baseline" 

71 self.apdbCfg.dia_object_columns = [] 

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

73 self.apdb._schema.makeSchema() 

74 

75 def tearDown(self): 

76 del self.apdb 

77 

78 def test_count_zero_objects(self): 

79 value = self.apdb.countUnassociatedObjects() 

80 self.assertEqual(value, 0) 

81 

82 def test_count_objects(self): 

83 n_created = 5 

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

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

86 

87 # nsecs must be an integer, not 1.4e18 

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

89 self.apdb.store(dateTime, objects) 

90 

91 value = self.apdb.countUnassociatedObjects() 

92 self.assertEqual(n_created - 1, value) 

93 

94 

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

96 pass 

97 

98 

99def setup_module(module): 

100 lsst.utils.tests.init() 

101 

102 

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

104 lsst.utils.tests.init() 

105 unittest.main()