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 ap_association. 

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 contextlib 

23import os 

24import unittest 

25 

26import lsst.afw.image as afwImage 

27import lsst.afw.table as afwTable 

28import lsst.pipe.base as pipeBase 

29from lsst.utils import getPackageDir 

30import lsst.utils.tests 

31from unittest.mock import patch, Mock 

32 

33from lsst.ap.association import DiaPipelineTask 

34 

35 

36class TestDiaPipelineTask(unittest.TestCase): 

37 

38 @classmethod 

39 def _makeDefaultConfig(cls): 

40 config = DiaPipelineTask.ConfigClass() 

41 config.apdb.db_url = "sqlite://" 

42 config.apdb.isolation_level = "READ_UNCOMMITTED" 

43 config.diaSourceDpddifier.copyColumns = {"id": "id", 

44 "parent": "parent", 

45 "coord_ra": "coord_ra", 

46 "coord_dec": "coord_dec"} 

47 config.diaSourceDpddifier.flagMap = os.path.join( 

48 getPackageDir("ap_association"), 

49 "tests", 

50 "test-flag-map.yaml") 

51 return config 

52 

53 @contextlib.contextmanager 

54 def mockPatchSubtasks(self, task): 

55 """Make mocks for all the ap_pipe subtasks. 

56 

57 This is needed because the task itself cannot be a mock. 

58 The task's subtasks do not exist until the task is created, so 

59 this allows us to mock them instead. 

60 

61 Parameters 

62 ---------- 

63 task : `lsst.ap.association.DiaPipelineTask` 

64 The task whose subtasks will be mocked. 

65 

66 Yields 

67 ------ 

68 subtasks : `lsst.pipe.base.Struct` 

69 All mocks created by this context manager, including: 

70 

71 ``diaCatalogLoader`` 

72 ``dpddifier`` 

73 ``associator`` 

74 ``forcedSource`` 

75 a mock for the corresponding subtask. Mocks do not return any 

76 particular value, but have mocked methods that can be queried 

77 for calls by ApPipeTask 

78 """ 

79 with patch.object(task, "diaCatalogLoader") as mockDiaCatLoader, \ 

80 patch.object(task, "diaSourceDpddifier") as mockDpddifier, \ 

81 patch.object(task, "associator") as mockAssociator, \ 

82 patch.object(task, "diaForcedSource") as mockForcedSource, \ 

83 patch.object(task, "apdb") as mockApdb, \ 

84 patch.object(task, "alertPackager") as mockAlertPackager: 

85 yield pipeBase.Struct(diaCatalogLoader=mockDiaCatLoader, 

86 dpddifier=mockDpddifier, 

87 associator=mockAssociator, 

88 diaForcedSource=mockForcedSource, 

89 apdb=mockApdb, 

90 alertPackager=mockAlertPackager) 

91 

92 def setUp(self): 

93 self.config = self._makeDefaultConfig() 

94 self.srcSchema = afwTable.SourceTable.makeMinimalSchema() 

95 self.srcSchema.addField("base_PixelFlags_flag", type="Flag") 

96 self.srcSchema.addField("base_PixelFlags_flag_offimage", type="Flag") 

97 

98 def tearDown(self): 

99 pass 

100 

101 def testRunQuantum(self): 

102 pass 

103 

104 def testRun(self): 

105 """Test the normal workflow of each ap_pipe step. 

106 """ 

107 task = DiaPipelineTask( 

108 config=self.config, 

109 initInputs={"diaSourceSchema": self.srcSchema}) 

110 diffIm = Mock(spec=afwImage.Exposure) 

111 exposure = Mock(spec=afwImage.ExposureF) 

112 diaSrc = Mock(sepc=afwTable.SourceCatalog) 

113 ccdExposureIdBits = 32 

114 with self.mockPatchSubtasks(task) as subtasks: 

115 result = task.run(diaSrc, diffIm, exposure, ccdExposureIdBits) 

116 subtasks.dpddifier.run.assert_called_once() 

117 subtasks.dpddifier.run.assert_called_once() 

118 subtasks.associator.run.assert_called_once() 

119 subtasks.diaForcedSource.run.assert_called_once() 

120 subtasks.alertPackager.run.assert_called_once() 

121 self.assertEqual(result.apdb_marker.db_url, "sqlite://") 

122 self.assertEqual(result.apdb_marker.isolation_level, 

123 "READ_UNCOMMITTED") 

124 

125 

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

127 pass 

128 

129 

130def setup_module(module): 

131 lsst.utils.tests.init() 

132 

133 

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

135 lsst.utils.tests.init() 

136 unittest.main()