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#!/usr/bin/env python 

2 

3""" 

4camcops_server/tasks/tests/rapid3_tests.py 

5 

6=============================================================================== 

7 

8 Copyright (C) 2012-2020 Rudolf Cardinal (rudolf@pobox.com). 

9 

10 This file is part of CamCOPS. 

11 

12 CamCOPS is free software: you can redistribute it and/or modify 

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

14 the Free Software Foundation, either version 3 of the License, or 

15 (at your option) any later version. 

16 

17 CamCOPS is distributed in the hope that it will be useful, 

18 but WITHOUT ANY WARRANTY; without even the implied warranty of 

19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

20 GNU General Public License for more details. 

21 

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

23 along with CamCOPS. If not, see <https://www.gnu.org/licenses/>. 

24 

25=============================================================================== 

26 

27""" 

28 

29from unittest import mock 

30import unittest 

31 

32from camcops_server.tasks.rapid3 import Rapid3 

33 

34 

35# ============================================================================= 

36# Unit tests 

37# ============================================================================= 

38 

39class Rapid3Tests(unittest.TestCase): 

40 def setUp(self) -> None: 

41 super().setUp() 

42 

43 self.request = mock.Mock() 

44 

45 def test_rapid3_calculation(self) -> None: 

46 rapid3 = Rapid3() 

47 

48 # a-j total 13 

49 # expected FN = 13/3 = 4.3 (1 dp) 

50 rapid3.q1a = 1 

51 rapid3.q1b = 2 

52 rapid3.q1c = 3 

53 rapid3.q1d = 0 

54 rapid3.q1e = 1 

55 rapid3.q1f = 0 

56 rapid3.q1g = 3 

57 rapid3.q1h = 0 

58 rapid3.q1i = 1 

59 rapid3.q1j = 2 

60 

61 # k-m not scored formally 

62 rapid3.q1k = 3 

63 rapid3.q1l = 0 

64 rapid3.q1m = 1 

65 

66 rapid3.q2 = 0.5 

67 rapid3.q3 = 2.0 

68 

69 # cumulative = 4.3 + 0.5 + 2.0 = 6.8 

70 

71 self.assertEqual(rapid3.rapid3(), 6.8) 

72 

73 def test_rapid3_none_when_field_none(self) -> None: 

74 rapid3 = Rapid3() 

75 

76 self.assertIsNone(rapid3.rapid3()) 

77 

78 def test_complete_when_all_answers_valid(self) -> None: 

79 rapid3 = Rapid3() 

80 

81 rapid3.q1a = 0 

82 rapid3.q1b = 0 

83 rapid3.q1c = 0 

84 rapid3.q1d = 0 

85 rapid3.q1e = 0 

86 rapid3.q1f = 0 

87 rapid3.q1g = 0 

88 rapid3.q1h = 0 

89 rapid3.q1i = 0 

90 rapid3.q1j = 0 

91 

92 rapid3.q1k = 0 

93 rapid3.q1l = 0 

94 rapid3.q1m = 0 

95 

96 rapid3.q2 = 0.0 

97 rapid3.q3 = 0.0 

98 

99 self.assertTrue(rapid3.is_complete()) 

100 

101 def test_incomplete_when_any_field_none(self) -> None: 

102 all_fields = [ 

103 "q1a", 

104 "q1b", 

105 "q1c", 

106 "q1d", 

107 "q1e", 

108 "q1f", 

109 "q1g", 

110 "q1h", 

111 "q1i", 

112 "q1j", 

113 "q1k", 

114 "q1l", 

115 "q1m", 

116 "q2", 

117 "q3", 

118 ] 

119 

120 for none_field in all_fields: 

121 rapid3 = Rapid3() 

122 

123 for field in all_fields: 

124 setattr(rapid3, field, 0.0) 

125 

126 setattr(rapid3, none_field, None) 

127 self.assertFalse(rapid3.is_complete(), 

128 msg=f"Failed when setting {none_field} to None") 

129 

130 def test_incomplete_when_any_field_invalid(self) -> None: 

131 all_fields = [ 

132 "q1a", 

133 "q1b", 

134 "q1c", 

135 "q1d", 

136 "q1e", 

137 "q1f", 

138 "q1g", 

139 "q1h", 

140 "q1i", 

141 "q1j", 

142 "q1k", 

143 "q1l", 

144 "q1m", 

145 "q2", 

146 "q3", 

147 ] 

148 

149 for invalid_field in all_fields: 

150 rapid3 = Rapid3() 

151 

152 for field in all_fields: 

153 setattr(rapid3, field, 0.0) 

154 

155 setattr(rapid3, invalid_field, 10.5) 

156 self.assertFalse( 

157 rapid3.is_complete(), 

158 msg=f"Failed when setting {invalid_field} invalid") 

159 

160 def test_disease_severity_n_a_for_none(self) -> None: 

161 rapid3 = Rapid3() 

162 

163 with mock.patch.object(rapid3, "rapid3") as mock_rapid3: 

164 mock_rapid3.return_value = None 

165 with mock.patch.object(rapid3, "wxstring") as mock_wxstring: 

166 rapid3.disease_severity(self.request) 

167 

168 mock_wxstring.assert_called_once_with(self.request, "n_a") 

169 

170 def test_disease_severity_near_remission_for_3(self) -> None: 

171 rapid3 = Rapid3() 

172 

173 with mock.patch.object(rapid3, "rapid3") as mock_rapid3: 

174 mock_rapid3.return_value = 3.0 

175 with mock.patch.object(rapid3, "wxstring") as mock_wxstring: 

176 rapid3.disease_severity(self.request) 

177 

178 mock_wxstring.assert_called_once_with(self.request, "near_remission") 

179 

180 def test_disease_severity_low_for_6(self) -> None: 

181 rapid3 = Rapid3() 

182 

183 with mock.patch.object(rapid3, "rapid3") as mock_rapid3: 

184 mock_rapid3.return_value = 6 

185 with mock.patch.object(rapid3, "wxstring") as mock_wxstring: 

186 rapid3.disease_severity(self.request) 

187 

188 mock_wxstring.assert_called_once_with(self.request, "low_severity") 

189 

190 def test_disease_severity_moderate_for_12(self) -> None: 

191 rapid3 = Rapid3() 

192 

193 with mock.patch.object(rapid3, "rapid3") as mock_rapid3: 

194 mock_rapid3.return_value = 12 

195 with mock.patch.object(rapid3, "wxstring") as mock_wxstring: 

196 rapid3.disease_severity(self.request) 

197 

198 mock_wxstring.assert_called_once_with(self.request, "moderate_severity") 

199 

200 def test_disease_severity_high_for_12point1(self) -> None: 

201 rapid3 = Rapid3() 

202 

203 with mock.patch.object(rapid3, "rapid3") as mock_rapid3: 

204 mock_rapid3.return_value = 12.1 

205 with mock.patch.object(rapid3, "wxstring") as mock_wxstring: 

206 rapid3.disease_severity(self.request) 

207 

208 mock_wxstring.assert_called_once_with(self.request, "high_severity")