Coverage for tests/test_reports.py: 8%
72 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-08-06 02:30 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-08-06 02:30 +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 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/>.
22import unittest
24from lsst.ctrl.mpexec import ExecutionStatus, QuantumReport, Report
27class ReportsTestCase(unittest.TestCase):
28 """A test case for reports module"""
30 def test_quantumReport(self):
31 """Test for QuantumReport class"""
32 dataId = {"instrument": "LSST"}
33 taskLabel = "task"
35 qr = QuantumReport(dataId=dataId, taskLabel=taskLabel)
36 self.assertEqual(qr.status, ExecutionStatus.SUCCESS)
37 self.assertEqual(qr.dataId, dataId)
38 self.assertEqual(qr.taskLabel, taskLabel)
39 self.assertIsNone(qr.exitCode)
40 self.assertIsNone(qr.exceptionInfo)
42 qr = QuantumReport(status=ExecutionStatus.TIMEOUT, dataId=dataId, taskLabel=taskLabel)
43 self.assertEqual(qr.status, ExecutionStatus.TIMEOUT)
45 qr = QuantumReport.from_exception(
46 exception=RuntimeError("runtime error"), dataId=dataId, taskLabel=taskLabel
47 )
48 self.assertEqual(qr.status, ExecutionStatus.FAILURE)
49 self.assertEqual(qr.dataId, dataId)
50 self.assertEqual(qr.taskLabel, taskLabel)
51 self.assertIsNone(qr.exitCode)
52 self.assertEqual(qr.exceptionInfo.className, "RuntimeError")
53 self.assertEqual(qr.exceptionInfo.message, "runtime error")
55 qr = QuantumReport.from_exit_code(exitCode=0, dataId=dataId, taskLabel=taskLabel)
56 self.assertEqual(qr.status, ExecutionStatus.SUCCESS)
57 self.assertEqual(qr.dataId, dataId)
58 self.assertEqual(qr.taskLabel, taskLabel)
59 self.assertEqual(qr.exitCode, 0)
60 self.assertIsNone(qr.exceptionInfo)
62 qr = QuantumReport.from_exit_code(exitCode=1, dataId=dataId, taskLabel=taskLabel)
63 self.assertEqual(qr.status, ExecutionStatus.FAILURE)
64 self.assertEqual(qr.dataId, dataId)
65 self.assertEqual(qr.taskLabel, taskLabel)
66 self.assertEqual(qr.exitCode, 1)
67 self.assertIsNone(qr.exceptionInfo)
69 def test_report(self):
70 """Test for Report class"""
71 report = Report()
72 self.assertEqual(report.status, ExecutionStatus.SUCCESS)
73 self.assertIsNotNone(report.cmdLine)
74 self.assertIsNone(report.exitCode)
75 self.assertIsNone(report.exceptionInfo)
77 dataId = {"instrument": "LSST"}
78 taskLabel = "task"
80 qr = QuantumReport.from_exception(
81 exception=RuntimeError("runtime error"), dataId=dataId, taskLabel=taskLabel
82 )
83 report = Report(status=ExecutionStatus.FAILURE, exitCode=-1)
84 report.set_exception(RuntimeError("runtime error"))
85 report.quantaReports.append(qr)
86 self.assertEqual(report.status, ExecutionStatus.FAILURE)
87 self.assertEqual(report.exitCode, -1)
88 self.assertEqual(report.exceptionInfo.className, "RuntimeError")
89 self.assertEqual(report.exceptionInfo.message, "runtime error")
90 self.assertEqual(len(report.quantaReports), 1)
92 def test_json(self):
93 """Test for conversion to/from json"""
94 dataId = {"instrument": "LSST"}
95 taskLabel = "task"
97 qr = QuantumReport.from_exception(
98 exception=RuntimeError("runtime error"), dataId=dataId, taskLabel=taskLabel
99 )
100 report = Report(status=ExecutionStatus.FAILURE, exitCode=-1)
101 report.set_exception(RuntimeError("runtime error"))
102 report.quantaReports.append(qr)
103 json = report.model_dump_json(exclude_none=True, indent=2)
104 self.assertIsInstance(json, str)
106 report = Report.model_validate_json(json)
107 self.assertEqual(report.status, ExecutionStatus.FAILURE)
108 self.assertEqual(report.exitCode, -1)
109 self.assertEqual(report.exceptionInfo.className, "RuntimeError")
110 self.assertEqual(report.exceptionInfo.message, "runtime error")
111 self.assertEqual(len(report.quantaReports), 1)
112 qr = report.quantaReports[0]
113 self.assertEqual(qr.status, ExecutionStatus.FAILURE)
114 self.assertEqual(qr.dataId, dataId)
115 self.assertEqual(qr.taskLabel, taskLabel)
116 self.assertIsNone(qr.exitCode)
117 self.assertEqual(qr.exceptionInfo.className, "RuntimeError")
118 self.assertEqual(qr.exceptionInfo.message, "runtime error")
121if __name__ == "__main__":
122 unittest.main()