Coverage for tasks/tests/basdai_tests.py: 19%
64 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-30 13:48 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-30 13:48 +0000
1"""
2camcops_server/tasks/tests/basdai_tests.py
4===============================================================================
6 Copyright (C) 2012, University of Cambridge, Department of Psychiatry.
7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
9 This file is part of CamCOPS.
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.
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.
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/>.
24===============================================================================
26"""
28from unittest import mock, TestCase
30from camcops_server.tasks.basdai import Basdai
33# =============================================================================
34# Unit tests
35# =============================================================================
38class BasdaiTests(TestCase):
39 def setUp(self) -> None:
40 super().setUp()
42 self.request = mock.Mock()
44 def test_basdai_calculation(self) -> None:
45 basdai = Basdai()
47 basdai.q1 = 2
48 basdai.q2 = 10
49 basdai.q3 = 7
50 basdai.q4 = 1
52 basdai.q5 = 9
53 basdai.q6 = 3
55 # 2 + 10 + 7 + 1 = 20
56 # (9 + 3) / 2 = 6
57 # 20 + 6 = 26
58 # 26 / 5 = 5.2
60 self.assertEqual(basdai.basdai(), 5.2)
62 def test_basdai_none_when_field_none(self) -> None:
63 basdai = Basdai()
65 self.assertIsNone(basdai.basdai())
67 def test_basdai_complete_when_all_answers_valid(self) -> None:
68 basdai = Basdai()
70 basdai.q1 = 0
71 basdai.q2 = 0
72 basdai.q3 = 0
73 basdai.q4 = 0
75 basdai.q5 = 0
76 basdai.q6 = 0
78 self.assertTrue(basdai.is_complete())
80 def test_basdai_incomplete_when_a_field_none(self) -> None:
81 basdai = Basdai()
83 basdai.q1 = None
84 basdai.q2 = 0
85 basdai.q3 = 0
86 basdai.q4 = 0
88 basdai.q5 = 0
89 basdai.q6 = 0
91 self.assertFalse(basdai.is_complete())
93 def test_basdai_incomplete_when_a_field_invalid(self) -> None:
94 basdai = Basdai()
96 basdai.q1 = 11
97 basdai.q2 = 0
98 basdai.q3 = 0
99 basdai.q4 = 0
101 basdai.q5 = 0
102 basdai.q6 = 0
104 self.assertFalse(basdai.is_complete())
106 def test_activity_state_qmark_for_none(self) -> None:
107 basdai = Basdai()
109 with mock.patch.object(basdai, "basdai") as mock_basdai:
110 mock_basdai.return_value = None
111 self.assertEqual(basdai.activity_state(self.request), "?")
113 def test_activity_state_inactive_for_less_than_4(self) -> None:
114 basdai = Basdai()
116 with mock.patch.object(basdai, "basdai") as mock_basdai:
117 mock_basdai.return_value = 3.8
118 with mock.patch.object(basdai, "wxstring") as mock_wxstring:
119 basdai.activity_state(self.request)
121 mock_wxstring.assert_called_once_with(self.request, "inactive")
123 def test_activity_state_active_for_4(self) -> None:
124 basdai = Basdai()
126 with mock.patch.object(basdai, "basdai") as mock_basdai:
127 mock_basdai.return_value = 4
128 with mock.patch.object(basdai, "wxstring") as mock_wxstring:
129 basdai.activity_state(self.request)
131 mock_wxstring.assert_called_once_with(self.request, "active")