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

1""" 

2camcops_server/tasks/tests/cpft_research_preferences_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 

28from unittest import mock 

29import unittest 

30 

31from camcops_server.tasks.cpft_research_preferences import ( 

32 CpftResearchPreferences, 

33) 

34 

35 

36class CpftResearchPreferencesTests(unittest.TestCase): 

37 def setUp(self) -> None: 

38 super().setUp() 

39 

40 self.request = mock.Mock() 

41 

42 def test_complete_when_all_answers_valid(self) -> None: 

43 task = CpftResearchPreferences() 

44 

45 task.contact_preference = "Y" 

46 task.contact_by_email = True 

47 task.research_opt_out = False 

48 

49 self.assertTrue(task.is_complete()) 

50 

51 def test_complete_when_red_and_email_null(self) -> None: 

52 task = CpftResearchPreferences() 

53 

54 task.contact_preference = "R" 

55 

56 self.assertTrue(task.is_complete()) 

57 

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 

62 

63 self.assertFalse(task.is_complete()) 

64 

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 

69 

70 self.assertFalse(task.is_complete()) 

71 

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 ] 

78 

79 for invalid_field in all_fields: 

80 task = CpftResearchPreferences() 

81 

82 task.contact_preference = "G" 

83 task.contact_by_email = True 

84 task.research_opt_out = False 

85 self.assertTrue(task.is_complete()) 

86 

87 setattr(task, invalid_field, 10.5) 

88 self.assertFalse( 

89 task.is_complete(), 

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

91 )