Coverage for tasks/tests/rapid3_tests.py: 15%

1"""

2 camcops_server/tasks/tests/rapid3_tests.py

3

4===============================================================================

5

6 Copyright (C) 2012, University of Cambridge, Department of Psychiatry.

7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).

8

9 This file is part of CamCOPS.

10

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

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

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

14 (at your option) any later version.

15

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

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

18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

19 GNU General Public License for more details.

20

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

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

23

24===============================================================================

25

26"""

27

28 from unittest import mock

29 import unittest

30

31 from camcops_server.tasks.rapid3 import Rapid3

32

33

34# =============================================================================

35# Unit tests

36# =============================================================================

37

38

39 class 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(

128 rapid3.is_complete(),

129 msg=f"Failed when setting {none_field} to None",

130 )

131

132 def test_incomplete_when_any_field_invalid(self) -> None:

133 all_fields = [

134 "q1a",

135 "q1b",

136 "q1c",

137 "q1d",

138 "q1e",

139 "q1f",

140 "q1g",

141 "q1h",

142 "q1i",

143 "q1j",

144 "q1k",

145 "q1l",

146 "q1m",

147 "q2",

148 "q3",

149 ]

150

151 for invalid_field in all_fields:

152 rapid3 = Rapid3()

153

154 for field in all_fields:

155 setattr(rapid3, field, 0.0)

156

157 setattr(rapid3, invalid_field, 10.5)

158 self.assertFalse(

159 rapid3.is_complete(),

160 msg=f"Failed when setting {invalid_field} invalid",

161 )

162

163 def test_disease_severity_n_a_for_none(self) -> None:

164 rapid3 = Rapid3()

165

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

167 mock_rapid3.return_value = None

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

169 rapid3.disease_severity(self.request)

170

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

172

173 def test_disease_severity_near_remission_for_3(self) -> None:

174 rapid3 = Rapid3()

175

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

177 mock_rapid3.return_value = 3.0

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

179 rapid3.disease_severity(self.request)

180

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

182

183 def test_disease_severity_low_for_6(self) -> None:

184 rapid3 = Rapid3()

185

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

187 mock_rapid3.return_value = 6

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

189 rapid3.disease_severity(self.request)

190

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

192

193 def test_disease_severity_moderate_for_12(self) -> None:

194 rapid3 = Rapid3()

195

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

197 mock_rapid3.return_value = 12

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

199 rapid3.disease_severity(self.request)

200

201 mock_wxstring.assert_called_once_with(

202 self.request, "moderate_severity"

203 )

204

205 def test_disease_severity_high_for_12point1(self) -> None:

206 rapid3 = Rapid3()

207

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

209 mock_rapid3.return_value = 12.1

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

211 rapid3.disease_severity(self.request)

212

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