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 "test-flag-map.yaml") 

50 config.doPackageAlerts = doPackageAlerts 

51 return config 

52 

53 def setUp(self): 

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

55 srcSchema = afwTable.SourceTable.makeMinimalSchema() 

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

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

58 self.srcSchema = afwTable.SourceCatalog(srcSchema) 

59 

60 def tearDown(self): 

61 pass 

62 

63 def testRunQuantum(self): 

64 pass 

65 

66 def testRunWithAlerts(self): 

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

68 """ 

69 self._testRun(True) 

70 

71 def testRunWithoutAlerts(self): 

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

73 """ 

74 self._testRun(False) 

75 

76 def _testRun(self, doPackageAlerts=False): 

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

78 """ 

79 config = self._makeDefaultConfig(doPackageAlerts=doPackageAlerts) 

80 task = DiaPipelineTask( 

81 config=config, 

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

83 diffIm = Mock(spec=afwImage.ExposureF) 

84 exposure = Mock(spec=afwImage.ExposureF) 

85 diaSrc = Mock(sepc=afwTable.SourceCatalog) 

86 ccdExposureIdBits = 32 

87 

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

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

90 # appropriately. 

91 subtasksToMock = [ 

92 "diaCatalogLoader", 

93 "diaSourceDpddifier", 

94 "associator", 

95 "diaForcedSource", 

96 ] 

97 if doPackageAlerts: 

98 subtasksToMock.append("alertPackager") 

99 else: 

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

101 

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

103 # execution in the test environment. 

104 with patch.multiple( 

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

106 ): 

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

108 for subtaskName in subtasksToMock: 

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

110 pipeBase.testUtils.assertValidOutput(task, result) 

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

112 self.assertEqual(result.apdbMarker.isolation_level, 

113 "READ_UNCOMMITTED") 

114 

115 

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

117 pass 

118 

119 

120def setup_module(module): 

121 lsst.utils.tests.init() 

122 

123 

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

125 lsst.utils.tests.init() 

126 unittest.main()