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 os 

23import unittest 

24 

25import lsst.afw.image as afwImage 

26import lsst.afw.table as afwTable 

27import lsst.pipe.base as pipeBase 

28from lsst.utils import getPackageDir 

29import lsst.utils.tests 

30from unittest.mock import patch, Mock, DEFAULT 

31 

32from lsst.ap.association import DiaPipelineTask 

33 

34 

35class TestDiaPipelineTask(unittest.TestCase): 

36 

37 @classmethod 

38 def _makeDefaultConfig(cls, doPackageAlerts=False): 

39 config = DiaPipelineTask.ConfigClass() 

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

41 config.apdb.isolation_level = "READ_UNCOMMITTED" 

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

43 "parent": "parent", 

44 "coord_ra": "coord_ra", 

45 "coord_dec": "coord_dec"} 

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

47 getPackageDir("ap_association"), 

48 "tests", 

49 "data", 

50 "test-flag-map.yaml") 

51 config.doPackageAlerts = doPackageAlerts 

52 return config 

53 

54 def setUp(self): 

55 # schemas are persisted in both Gen 2 and Gen 3 butler as prototypical catalogs 

56 srcSchema = afwTable.SourceTable.makeMinimalSchema() 

57 srcSchema.addField("base_PixelFlags_flag", type="Flag") 

58 srcSchema.addField("base_PixelFlags_flag_offimage", type="Flag") 

59 self.srcSchema = afwTable.SourceCatalog(srcSchema) 

60 

61 def tearDown(self): 

62 pass 

63 

64 def testRunQuantum(self): 

65 pass 

66 

67 def testRunWithAlerts(self): 

68 """Test running while creating and packaging alerts. 

69 """ 

70 self._testRun(True) 

71 

72 def testRunWithoutAlerts(self): 

73 """Test running without creating and packaging alerts. 

74 """ 

75 self._testRun(False) 

76 

77 def _testRun(self, doPackageAlerts=False): 

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

79 """ 

80 config = self._makeDefaultConfig(doPackageAlerts=doPackageAlerts) 

81 task = DiaPipelineTask( 

82 config=config, 

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

84 diffIm = Mock(spec=afwImage.ExposureF) 

85 exposure = Mock(spec=afwImage.ExposureF) 

86 template = Mock(spec=afwImage.ExposureF) 

87 diaSrc = Mock(sepc=afwTable.SourceCatalog) 

88 ccdExposureIdBits = 32 

89 

90 # Each of these subtasks should be called once during diaPipe 

91 # execution. We use mocks here to check they are being executed 

92 # appropriately. 

93 subtasksToMock = [ 

94 "diaCatalogLoader", 

95 "diaSourceDpddifier", 

96 "associator", 

97 "diaForcedSource", 

98 ] 

99 if doPackageAlerts: 

100 subtasksToMock.append("alertPackager") 

101 else: 

102 self.assertFalse(hasattr(task, "alertPackager")) 

103 

104 # apdb isn't a subtask, but still needs to be mocked out for correct 

105 # execution in the test environment. 

106 with patch.multiple( 

107 task, **{task: DEFAULT for task in subtasksToMock + ["apdb"]} 

108 ): 

109 result = task.run(diaSrc, 

110 diffIm, 

111 exposure, 

112 template, 

113 ccdExposureIdBits) 

114 for subtaskName in subtasksToMock: 

115 getattr(task, subtaskName).run.assert_called_once() 

116 pipeBase.testUtils.assertValidOutput(task, result) 

117 self.assertEqual(result.apdbMarker.db_url, "sqlite://") 

118 self.assertEqual(result.apdbMarker.isolation_level, 

119 "READ_UNCOMMITTED") 

120 

121 

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

123 pass 

124 

125 

126def setup_module(module): 

127 lsst.utils.tests.init() 

128 

129 

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

131 lsst.utils.tests.init() 

132 unittest.main()