Coverage for tests/test_ap_verify_queries.py: 32%

Shortcuts 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

61 statements  

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 unittest.mock 

23import lsst.utils.tests 

24 

25import lsst.afw.table as afwTable 

26import lsst.afw.image as afwImage 

27import lsst.geom as geom 

28import lsst.daf.base as dafBase 

29from lsst.dax.apdb import Apdb, ApdbConfig 

30 

31 

32def createTestObjects(n_objects, extra_fields): 

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

34 

35 Parameters 

36 ---------- 

37 n_objects : `int` 

38 Number of objects to create. 

39 extra_fields : `dict` 

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

41 

42 Returns 

43 ------- 

44 sources : `lsst.afw.table.SourceCatalog` 

45 Tests sources with filled values. 

46 """ 

47 schema = afwTable.SourceTable.makeMinimalSchema() 

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

49 schema.addField(field, type=type) 

50 sources = afwTable.SourceCatalog(schema) 

51 

52 for src_idx in range(n_objects): 

53 src = sources.addNew() 

54 for subSchema in schema: 

55 if subSchema.getField().getTypeString() == "Angle": 

56 src[subSchema.getField().getName()] = 1 * geom.degrees 

57 else: 

58 src[subSchema.getField().getName()] = 1 

59 src['id'] = src_idx 

60 

61 return sources 

62 

63 

64class TestApVerifyQueries(unittest.TestCase): 

65 

66 def setUp(self): 

67 self.apdbCfg = ApdbConfig() 

68 # Create DB in memory. 

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

70 self.apdbCfg.isolation_level = "READ_UNCOMMITTED" 

71 self.apdbCfg.dia_object_index = "baseline" 

72 self.apdbCfg.dia_object_columns = [] 

73 self.apdb = Apdb( 

74 config=self.apdbCfg, 

75 afw_schemas=dict(DiaObject=afwTable.SourceTable.makeMinimalSchema(), 

76 DiaSource=afwTable.SourceTable.makeMinimalSchema())) 

77 self.apdb._schema.makeSchema() 

78 

79 def tearDown(self): 

80 del self.apdb 

81 

82 def test_count_zero_objects(self): 

83 value = self.apdb.countUnassociatedObjects() 

84 self.assertEqual(value, 0) 

85 

86 def test_count_objects(self): 

87 n_created = 5 

88 sources = createTestObjects(n_created, {'nDiaSources': 'I'}) 

89 sources[-1]['nDiaSources'] = 2 

90 

91 # nsecs must be an integer, not 1.4e18 

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

93 self.apdb.storeDiaObjects(sources, dateTime.toPython()) 

94 

95 value = self.apdb.countUnassociatedObjects() 

96 self.assertEqual(n_created - 1, value) 

97 

98 @staticmethod 

99 def _makeVisitInfo(exposureId): 

100 # Real VisitInfo hard to create 

101 visitInfo = unittest.mock.NonCallableMock( 

102 afwImage.VisitInfo, 

103 **{"getExposureId.return_value": exposureId} 

104 ) 

105 return visitInfo 

106 

107 def test_isExposureProcessed(self): 

108 n_created = 5 

109 sources = createTestObjects(n_created, {'ccdVisitId': 'I'}) 

110 for source in sources: 

111 source['ccdVisitId'] = 2381 

112 

113 self.apdb.storeDiaSources(sources) 

114 

115 self.assertTrue(self.apdb.isVisitProcessed(TestApVerifyQueries._makeVisitInfo(2381))) 

116 self.assertFalse(self.apdb.isVisitProcessed(TestApVerifyQueries._makeVisitInfo(42))) 

117 

118 

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

120 pass 

121 

122 

123def setup_module(module): 

124 lsst.utils.tests.init() 

125 

126 

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

128 lsst.utils.tests.init() 

129 unittest.main()