Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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 pandas 

24import unittest.mock 

25import lsst.utils.tests 

26 

27import lsst.geom as geom 

28from lsst.daf.base import DateTime 

29from lsst.dax.apdb import ApdbSql, ApdbSqlConfig 

30 

31 

32def createTestObjects(n_objects, id_column_name, extra_fields): 

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

34 

35 Parameters 

36 ---------- 

37 n_objects : `int` 

38 Number of objects to create. 

39 id_column_name : `str` 

40 Name of the ID column. 

41 extra_fields : `dict` 

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

43 

44 Returns 

45 ------- 

46 sources : `pandas.DataFrame` 

47 Tests sources with filled values. 

48 """ 

49 data = { 

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

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

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

53 } 

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

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

56 df = pandas.DataFrame(data) 

57 return df 

58 

59 

60class TestApVerifyQueries(unittest.TestCase): 

61 

62 def setUp(self): 

63 self.apdbCfg = ApdbSqlConfig() 

64 # Create DB in memory. 

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

66 self.apdbCfg.dia_object_index = "baseline" 

67 self.apdbCfg.dia_object_columns = [] 

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

69 self.apdb._schema.makeSchema() 

70 

71 def tearDown(self): 

72 del self.apdb 

73 

74 def test_count_zero_objects(self): 

75 value = self.apdb.countUnassociatedObjects() 

76 self.assertEqual(value, 0) 

77 

78 def test_count_objects(self): 

79 n_created = 5 

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

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

82 

83 # nsecs must be an integer, not 1.4e18 

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

85 self.apdb.store(dateTime, objects) 

86 

87 value = self.apdb.countUnassociatedObjects() 

88 self.assertEqual(n_created - 1, value) 

89 

90 

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

92 pass 

93 

94 

95def setup_module(module): 

96 lsst.utils.tests.init() 

97 

98 

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

100 lsst.utils.tests.init() 

101 unittest.main()