Coverage for tasks/tests/rapid3_tests.py: 16%
98 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-08 23:14 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-08 23:14 +0000
1#!/usr/bin/env python
3"""
4camcops_server/tasks/tests/rapid3_tests.py
6===============================================================================
8 Copyright (C) 2012, University of Cambridge, Department of Psychiatry.
9 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
11 This file is part of CamCOPS.
13 CamCOPS is free software: you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
18 CamCOPS is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with CamCOPS. If not, see <https://www.gnu.org/licenses/>.
26===============================================================================
28"""
30from unittest import mock
31import unittest
33from camcops_server.tasks.rapid3 import Rapid3
36# =============================================================================
37# Unit tests
38# =============================================================================
41class Rapid3Tests(unittest.TestCase):
42 def setUp(self) -> None:
43 super().setUp()
45 self.request = mock.Mock()
47 def test_rapid3_calculation(self) -> None:
48 rapid3 = Rapid3()
50 # a-j total 13
51 # expected FN = 13/3 = 4.3 (1 dp)
52 rapid3.q1a = 1
53 rapid3.q1b = 2
54 rapid3.q1c = 3
55 rapid3.q1d = 0
56 rapid3.q1e = 1
57 rapid3.q1f = 0
58 rapid3.q1g = 3
59 rapid3.q1h = 0
60 rapid3.q1i = 1
61 rapid3.q1j = 2
63 # k-m not scored formally
64 rapid3.q1k = 3
65 rapid3.q1l = 0
66 rapid3.q1m = 1
68 rapid3.q2 = 0.5
69 rapid3.q3 = 2.0
71 # cumulative = 4.3 + 0.5 + 2.0 = 6.8
73 self.assertEqual(rapid3.rapid3(), 6.8)
75 def test_rapid3_none_when_field_none(self) -> None:
76 rapid3 = Rapid3()
78 self.assertIsNone(rapid3.rapid3())
80 def test_complete_when_all_answers_valid(self) -> None:
81 rapid3 = Rapid3()
83 rapid3.q1a = 0
84 rapid3.q1b = 0
85 rapid3.q1c = 0
86 rapid3.q1d = 0
87 rapid3.q1e = 0
88 rapid3.q1f = 0
89 rapid3.q1g = 0
90 rapid3.q1h = 0
91 rapid3.q1i = 0
92 rapid3.q1j = 0
94 rapid3.q1k = 0
95 rapid3.q1l = 0
96 rapid3.q1m = 0
98 rapid3.q2 = 0.0
99 rapid3.q3 = 0.0
101 self.assertTrue(rapid3.is_complete())
103 def test_incomplete_when_any_field_none(self) -> None:
104 all_fields = [
105 "q1a",
106 "q1b",
107 "q1c",
108 "q1d",
109 "q1e",
110 "q1f",
111 "q1g",
112 "q1h",
113 "q1i",
114 "q1j",
115 "q1k",
116 "q1l",
117 "q1m",
118 "q2",
119 "q3",
120 ]
122 for none_field in all_fields:
123 rapid3 = Rapid3()
125 for field in all_fields:
126 setattr(rapid3, field, 0.0)
128 setattr(rapid3, none_field, None)
129 self.assertFalse(
130 rapid3.is_complete(),
131 msg=f"Failed when setting {none_field} to None",
132 )
134 def test_incomplete_when_any_field_invalid(self) -> None:
135 all_fields = [
136 "q1a",
137 "q1b",
138 "q1c",
139 "q1d",
140 "q1e",
141 "q1f",
142 "q1g",
143 "q1h",
144 "q1i",
145 "q1j",
146 "q1k",
147 "q1l",
148 "q1m",
149 "q2",
150 "q3",
151 ]
153 for invalid_field in all_fields:
154 rapid3 = Rapid3()
156 for field in all_fields:
157 setattr(rapid3, field, 0.0)
159 setattr(rapid3, invalid_field, 10.5)
160 self.assertFalse(
161 rapid3.is_complete(),
162 msg=f"Failed when setting {invalid_field} invalid",
163 )
165 def test_disease_severity_n_a_for_none(self) -> None:
166 rapid3 = Rapid3()
168 with mock.patch.object(rapid3, "rapid3") as mock_rapid3:
169 mock_rapid3.return_value = None
170 with mock.patch.object(rapid3, "wxstring") as mock_wxstring:
171 rapid3.disease_severity(self.request)
173 mock_wxstring.assert_called_once_with(self.request, "n_a")
175 def test_disease_severity_near_remission_for_3(self) -> None:
176 rapid3 = Rapid3()
178 with mock.patch.object(rapid3, "rapid3") as mock_rapid3:
179 mock_rapid3.return_value = 3.0
180 with mock.patch.object(rapid3, "wxstring") as mock_wxstring:
181 rapid3.disease_severity(self.request)
183 mock_wxstring.assert_called_once_with(self.request, "near_remission")
185 def test_disease_severity_low_for_6(self) -> None:
186 rapid3 = Rapid3()
188 with mock.patch.object(rapid3, "rapid3") as mock_rapid3:
189 mock_rapid3.return_value = 6
190 with mock.patch.object(rapid3, "wxstring") as mock_wxstring:
191 rapid3.disease_severity(self.request)
193 mock_wxstring.assert_called_once_with(self.request, "low_severity")
195 def test_disease_severity_moderate_for_12(self) -> None:
196 rapid3 = Rapid3()
198 with mock.patch.object(rapid3, "rapid3") as mock_rapid3:
199 mock_rapid3.return_value = 12
200 with mock.patch.object(rapid3, "wxstring") as mock_wxstring:
201 rapid3.disease_severity(self.request)
203 mock_wxstring.assert_called_once_with(
204 self.request, "moderate_severity"
205 )
207 def test_disease_severity_high_for_12point1(self) -> None:
208 rapid3 = Rapid3()
210 with mock.patch.object(rapid3, "rapid3") as mock_rapid3:
211 mock_rapid3.return_value = 12.1
212 with mock.patch.object(rapid3, "wxstring") as mock_wxstring:
213 rapid3.disease_severity(self.request)
215 mock_wxstring.assert_called_once_with(self.request, "high_severity")