Coverage for tasks/tests/cpft_research_preferences_tests.py: 27%
37 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/cpft_research_preferences_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
29import unittest
31from camcops_server.tasks.cpft_research_preferences import (
32 CpftResearchPreferences,
33)
36class CpftResearchPreferencesTests(unittest.TestCase):
37 def setUp(self) -> None:
38 super().setUp()
40 self.request = mock.Mock()
42 def test_complete_when_all_answers_valid(self) -> None:
43 task = CpftResearchPreferences()
45 task.contact_preference = "Y"
46 task.contact_by_email = True
47 task.research_opt_out = False
49 self.assertTrue(task.is_complete())
51 def test_complete_when_red_and_email_null(self) -> None:
52 task = CpftResearchPreferences()
54 task.contact_preference = "R"
56 self.assertTrue(task.is_complete())
58 def test_incomplete_when_contact_preference_null(self) -> None:
59 task = CpftResearchPreferences()
60 task.contact_by_email = True
61 task.research_opt_out = False
63 self.assertFalse(task.is_complete())
65 def test_incomplete_when_yellow_and_email_null(self) -> None:
66 task = CpftResearchPreferences()
67 task.contact_preference = "Y"
68 task.research_opt_out = False
70 self.assertFalse(task.is_complete())
72 def test_incomplete_when_any_field_invalid(self) -> None:
73 all_fields = [
74 "contact_preference",
75 "contact_by_email",
76 "research_opt_out",
77 ]
79 for invalid_field in all_fields:
80 task = CpftResearchPreferences()
82 task.contact_preference = "G"
83 task.contact_by_email = True
84 task.research_opt_out = False
85 self.assertTrue(task.is_complete())
87 setattr(task, invalid_field, 10.5)
88 self.assertFalse(
89 task.is_complete(),
90 msg=f"Failed when setting {invalid_field} invalid",
91 )