Coverage for tasks/tests/basdai_tests.py : 20%

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
3"""
4camcops_server/tasks/tests/basdai_tests.py
6===============================================================================
8 Copyright (C) 2012-2020 Rudolf Cardinal (rudolf@pobox.com).
10 This file is part of CamCOPS.
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.
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.
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/>.
25===============================================================================
27"""
29from unittest import mock, TestCase
31from camcops_server.tasks.basdai import Basdai
34# =============================================================================
35# Unit tests
36# =============================================================================
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")