Coverage for tests/test_reports.py: 8%

72 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-07 12:18 +0000

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 

31 

32 

33class ReportsTestCase(unittest.TestCase): 

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

35 

36 def test_quantumReport(self): 

37 """Test for QuantumReport class.""" 

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

39 taskLabel = "task" 

40 

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

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

43 self.assertEqual(qr.dataId, dataId) 

44 self.assertEqual(qr.taskLabel, taskLabel) 

45 self.assertIsNone(qr.exitCode) 

46 self.assertIsNone(qr.exceptionInfo) 

47 

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

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

50 

51 qr = QuantumReport.from_exception( 

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

53 ) 

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

55 self.assertEqual(qr.dataId, dataId) 

56 self.assertEqual(qr.taskLabel, taskLabel) 

57 self.assertIsNone(qr.exitCode) 

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

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

60 

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

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

63 self.assertEqual(qr.dataId, dataId) 

64 self.assertEqual(qr.taskLabel, taskLabel) 

65 self.assertEqual(qr.exitCode, 0) 

66 self.assertIsNone(qr.exceptionInfo) 

67 

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

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

70 self.assertEqual(qr.dataId, dataId) 

71 self.assertEqual(qr.taskLabel, taskLabel) 

72 self.assertEqual(qr.exitCode, 1) 

73 self.assertIsNone(qr.exceptionInfo) 

74 

75 def test_report(self): 

76 """Test for Report class.""" 

77 report = Report() 

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

79 self.assertIsNotNone(report.cmdLine) 

80 self.assertIsNone(report.exitCode) 

81 self.assertIsNone(report.exceptionInfo) 

82 

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

84 taskLabel = "task" 

85 

86 qr = QuantumReport.from_exception( 

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

88 ) 

89 report = Report(status=ExecutionStatus.FAILURE, exitCode=-1) 

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

91 report.quantaReports.append(qr) 

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

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

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

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

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

97 

98 def test_json(self): 

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

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

101 taskLabel = "task" 

102 

103 qr = QuantumReport.from_exception( 

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

105 ) 

106 report = Report(status=ExecutionStatus.FAILURE, exitCode=-1) 

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

108 report.quantaReports.append(qr) 

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

110 self.assertIsInstance(json, str) 

111 

112 report = Report.model_validate_json(json) 

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

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

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

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

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

118 qr = report.quantaReports[0] 

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

120 self.assertEqual(qr.dataId, dataId) 

121 self.assertEqual(qr.taskLabel, taskLabel) 

122 self.assertIsNone(qr.exitCode) 

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

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

125 

126 

127if __name__ == "__main__": 

128 unittest.main()