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 unittest 

23import pandas as pd 

24 

25import lsst.afw.image as afwImage 

26import lsst.afw.table as afwTable 

27import lsst.pipe.base as pipeBase 

28import lsst.utils.tests 

29from unittest.mock import patch, Mock, DEFAULT 

30 

31from lsst.ap.association import DiaPipelineTask 

32 

33 

34class TestDiaPipelineTask(unittest.TestCase): 

35 

36 @classmethod 

37 def _makeDefaultConfig(cls, doPackageAlerts=False): 

38 config = DiaPipelineTask.ConfigClass() 

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

40 config.apdb.isolation_level = "READ_UNCOMMITTED" 

41 config.doPackageAlerts = doPackageAlerts 

42 return config 

43 

44 def setUp(self): 

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

46 srcSchema = afwTable.SourceTable.makeMinimalSchema() 

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

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

49 self.srcSchema = afwTable.SourceCatalog(srcSchema) 

50 

51 def tearDown(self): 

52 pass 

53 

54 def testRunQuantum(self): 

55 pass 

56 

57 def testRunWithAlerts(self): 

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

59 """ 

60 self._testRun(True) 

61 

62 def testRunWithoutAlerts(self): 

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

64 """ 

65 self._testRun(False) 

66 

67 def _testRun(self, doPackageAlerts=False): 

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

69 """ 

70 config = self._makeDefaultConfig(doPackageAlerts=doPackageAlerts) 

71 task = DiaPipelineTask(config=config) 

72 diffIm = Mock(spec=afwImage.ExposureF) 

73 exposure = Mock(spec=afwImage.ExposureF) 

74 template = Mock(spec=afwImage.ExposureF) 

75 diaSrc = Mock(sepc=pd.DataFrame) 

76 ccdExposureIdBits = 32 

77 

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

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

80 # appropriately. 

81 subtasksToMock = [ 

82 "diaCatalogLoader", 

83 "associator", 

84 "diaForcedSource", 

85 ] 

86 if doPackageAlerts: 

87 subtasksToMock.append("alertPackager") 

88 else: 

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

90 

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

92 # execution in the test environment. 

93 with patch.multiple( 

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

95 ): 

96 result = task.run(diaSrc, 

97 diffIm, 

98 exposure, 

99 template, 

100 ccdExposureIdBits) 

101 for subtaskName in subtasksToMock: 

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

103 pipeBase.testUtils.assertValidOutput(task, result) 

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

105 self.assertEqual(result.apdbMarker.isolation_level, 

106 "READ_UNCOMMITTED") 

107 

108 

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

110 pass 

111 

112 

113def setup_module(module): 

114 lsst.utils.tests.init() 

115 

116 

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

118 lsst.utils.tests.init() 

119 unittest.main()