Coverage for tasks/tests/basdai_tests.py: 20%
65 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/basdai_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, TestCase
32from camcops_server.tasks.basdai import Basdai
35# =============================================================================
36# Unit tests
37# =============================================================================
40class BasdaiTests(TestCase):
41 def setUp(self) -> None:
42 super().setUp()
44 self.request = mock.Mock()
46 def test_basdai_calculation(self) -> None:
47 basdai = Basdai()
49 basdai.q1 = 2
50 basdai.q2 = 10
51 basdai.q3 = 7
52 basdai.q4 = 1
54 basdai.q5 = 9
55 basdai.q6 = 3
57 # 2 + 10 + 7 + 1 = 20
58 # (9 + 3) / 2 = 6
59 # 20 + 6 = 26
60 # 26 / 5 = 5.2
62 self.assertEqual(basdai.basdai(), 5.2)
64 def test_basdai_none_when_field_none(self) -> None:
65 basdai = Basdai()
67 self.assertIsNone(basdai.basdai())
69 def test_basdai_complete_when_all_answers_valid(self) -> None:
70 basdai = Basdai()
72 basdai.q1 = 0
73 basdai.q2 = 0
74 basdai.q3 = 0
75 basdai.q4 = 0
77 basdai.q5 = 0
78 basdai.q6 = 0
80 self.assertTrue(basdai.is_complete())
82 def test_basdai_incomplete_when_a_field_none(self) -> None:
83 basdai = Basdai()
85 basdai.q1 = None
86 basdai.q2 = 0
87 basdai.q3 = 0
88 basdai.q4 = 0
90 basdai.q5 = 0
91 basdai.q6 = 0
93 self.assertFalse(basdai.is_complete())
95 def test_basdai_incomplete_when_a_field_invalid(self) -> None:
96 basdai = Basdai()
98 basdai.q1 = 11
99 basdai.q2 = 0
100 basdai.q3 = 0
101 basdai.q4 = 0
103 basdai.q5 = 0
104 basdai.q6 = 0
106 self.assertFalse(basdai.is_complete())
108 def test_activity_state_qmark_for_none(self) -> None:
109 basdai = Basdai()
111 with mock.patch.object(basdai, "basdai") as mock_basdai:
112 mock_basdai.return_value = None
113 self.assertEqual(basdai.activity_state(self.request), "?")
115 def test_activity_state_inactive_for_less_than_4(self) -> None:
116 basdai = Basdai()
118 with mock.patch.object(basdai, "basdai") as mock_basdai:
119 mock_basdai.return_value = 3.8
120 with mock.patch.object(basdai, "wxstring") as mock_wxstring:
121 basdai.activity_state(self.request)
123 mock_wxstring.assert_called_once_with(self.request, "inactive")
125 def test_activity_state_active_for_4(self) -> None:
126 basdai = Basdai()
128 with mock.patch.object(basdai, "basdai") as mock_basdai:
129 mock_basdai.return_value = 4
130 with mock.patch.object(basdai, "wxstring") as mock_wxstring:
131 basdai.activity_state(self.request)
133 mock_wxstring.assert_called_once_with(self.request, "active")