Coverage for tests/test_reports.py: 10%

73 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-05-03 02:52 -0700

1# This file is part of ctrl_mpexec. 

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 software is dual licensed under the GNU General Public License and also 

10# under a 3-clause BSD license. Recipients may choose which of these licenses 

11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt, 

12# respectively. If you choose the GPL option then the following text applies 

13# (but note that there is still no warranty even if you opt for BSD instead): 

14# 

15# This program is free software: you can redistribute it and/or modify 

16# it under the terms of the GNU General Public License as published by 

17# the Free Software Foundation, either version 3 of the License, or 

18# (at your option) any later version. 

19# 

20# This program is distributed in the hope that it will be useful, 

21# but WITHOUT ANY WARRANTY; without even the implied warranty of 

22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

23# GNU General Public License for more details. 

24# 

25# You should have received a copy of the GNU General Public License 

26# along with this program. If not, see <https://www.gnu.org/licenses/>. 

27 

28import unittest 

29 

30from lsst.ctrl.mpexec import ExecutionStatus, QuantumReport, Report 

31from lsst.pipe.base import QgraphSummary 

32 

33 

34class ReportsTestCase(unittest.TestCase): 

35 """A test case for reports module.""" 

36 

37 def test_quantumReport(self): 

38 """Test for QuantumReport class.""" 

39 dataId = {"instrument": "LSST"} 

40 taskLabel = "task" 

41 

42 qr = QuantumReport(dataId=dataId, taskLabel=taskLabel) 

43 self.assertEqual(qr.status, ExecutionStatus.SUCCESS) 

44 self.assertEqual(qr.dataId, dataId) 

45 self.assertEqual(qr.taskLabel, taskLabel) 

46 self.assertIsNone(qr.exitCode) 

47 self.assertIsNone(qr.exceptionInfo) 

48 

49 qr = QuantumReport(status=ExecutionStatus.TIMEOUT, dataId=dataId, taskLabel=taskLabel) 

50 self.assertEqual(qr.status, ExecutionStatus.TIMEOUT) 

51 

52 qr = QuantumReport.from_exception( 

53 exception=RuntimeError("runtime error"), dataId=dataId, taskLabel=taskLabel 

54 ) 

55 self.assertEqual(qr.status, ExecutionStatus.FAILURE) 

56 self.assertEqual(qr.dataId, dataId) 

57 self.assertEqual(qr.taskLabel, taskLabel) 

58 self.assertIsNone(qr.exitCode) 

59 self.assertEqual(qr.exceptionInfo.className, "RuntimeError") 

60 self.assertEqual(qr.exceptionInfo.message, "runtime error") 

61 

62 qr = QuantumReport.from_exit_code(exitCode=0, dataId=dataId, taskLabel=taskLabel) 

63 self.assertEqual(qr.status, ExecutionStatus.SUCCESS) 

64 self.assertEqual(qr.dataId, dataId) 

65 self.assertEqual(qr.taskLabel, taskLabel) 

66 self.assertEqual(qr.exitCode, 0) 

67 self.assertIsNone(qr.exceptionInfo) 

68 

69 qr = QuantumReport.from_exit_code(exitCode=1, dataId=dataId, taskLabel=taskLabel) 

70 self.assertEqual(qr.status, ExecutionStatus.FAILURE) 

71 self.assertEqual(qr.dataId, dataId) 

72 self.assertEqual(qr.taskLabel, taskLabel) 

73 self.assertEqual(qr.exitCode, 1) 

74 self.assertIsNone(qr.exceptionInfo) 

75 

76 def test_report(self): 

77 """Test for Report class.""" 

78 report = Report(qgraphSummary=QgraphSummary(graphID="uuid")) 

79 self.assertEqual(report.status, ExecutionStatus.SUCCESS) 

80 self.assertIsNotNone(report.cmdLine) 

81 self.assertIsNone(report.exitCode) 

82 self.assertIsNone(report.exceptionInfo) 

83 

84 dataId = {"instrument": "LSST"} 

85 taskLabel = "task" 

86 

87 qr = QuantumReport.from_exception( 

88 exception=RuntimeError("runtime error"), dataId=dataId, taskLabel=taskLabel 

89 ) 

90 report = Report( 

91 status=ExecutionStatus.FAILURE, exitCode=-1, qgraphSummary=QgraphSummary(graphID="uuid") 

92 ) 

93 report.set_exception(RuntimeError("runtime error")) 

94 report.quantaReports.append(qr) 

95 self.assertEqual(report.status, ExecutionStatus.FAILURE) 

96 self.assertEqual(report.exitCode, -1) 

97 self.assertEqual(report.exceptionInfo.className, "RuntimeError") 

98 self.assertEqual(report.exceptionInfo.message, "runtime error") 

99 self.assertEqual(len(report.quantaReports), 1) 

100 

101 def test_json(self): 

102 """Test for conversion to/from JSON.""" 

103 dataId = {"instrument": "LSST"} 

104 taskLabel = "task" 

105 

106 qr = QuantumReport.from_exception( 

107 exception=RuntimeError("runtime error"), dataId=dataId, taskLabel=taskLabel 

108 ) 

109 report = Report( 

110 status=ExecutionStatus.FAILURE, exitCode=-1, qgraphSummary=QgraphSummary(graphID="uuid") 

111 ) 

112 report.set_exception(RuntimeError("runtime error")) 

113 report.quantaReports.append(qr) 

114 json = report.model_dump_json(exclude_none=True, indent=2) 

115 self.assertIsInstance(json, str) 

116 

117 report = Report.model_validate_json(json) 

118 self.assertEqual(report.status, ExecutionStatus.FAILURE) 

119 self.assertEqual(report.exitCode, -1) 

120 self.assertEqual(report.exceptionInfo.className, "RuntimeError") 

121 self.assertEqual(report.exceptionInfo.message, "runtime error") 

122 self.assertEqual(len(report.quantaReports), 1) 

123 qr = report.quantaReports[0] 

124 self.assertEqual(qr.status, ExecutionStatus.FAILURE) 

125 self.assertEqual(qr.dataId, dataId) 

126 self.assertEqual(qr.taskLabel, taskLabel) 

127 self.assertIsNone(qr.exitCode) 

128 self.assertEqual(qr.exceptionInfo.className, "RuntimeError") 

129 self.assertEqual(qr.exceptionInfo.message, "runtime error") 

130 

131 

132if __name__ == "__main__": 

133 unittest.main()