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 yield pipeBase.Struct(diaCatalogLoader=mockDiaCatLoader, 

85 dpddifier=mockDpddifier, 

86 associator=mockAssociator, 

87 diaForcedSource=mockForcedSource, 

88 apdb=mockApdb) 

89 

90 def setUp(self): 

91 self.config = self._makeDefaultConfig() 

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

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

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

95 

96 def tearDown(self): 

97 pass 

98 

99 def testRunQuantum(self): 

100 pass 

101 

102 def testRun(self): 

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

104 """ 

105 task = DiaPipelineTask( 

106 config=self.config, 

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

108 diffIm = Mock(spec=afwImage.Exposure) 

109 exposure = Mock(spec=afwImage.ExposureF) 

110 diaSrc = Mock(sepc=afwTable.SourceCatalog) 

111 ccdExposureIdBits = 32 

112 with self.mockPatchSubtasks(task) as subtasks: 

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

114 subtasks.dpddifier.run.assert_called_once() 

115 subtasks.dpddifier.run.assert_called_once() 

116 subtasks.associator.run.assert_called_once() 

117 subtasks.diaForcedSource.run.assert_called_once() 

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

119 self.assertEqual(result.apdb_marker.isolation_level, 

120 "READ_UNCOMMITTED") 

121 

122 

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

124 pass 

125 

126 

127def setup_module(module): 

128 lsst.utils.tests.init() 

129 

130 

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

132 lsst.utils.tests.init() 

133 unittest.main()