Coverage for tasks/tests/cpft_research_preferences_tests.py: 29%

38 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-08 23:14 +0000

1#!/usr/bin/env python 

2 

3""" 

4camcops_server/tasks/tests/cpft_research_preferences_tests.py 

5 

6=============================================================================== 

7 

8 Copyright (C) 2012, University of Cambridge, Department of Psychiatry. 

9 Created by Rudolf Cardinal (rnc1001@cam.ac.uk). 

10 

11 This file is part of CamCOPS. 

12 

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. 

17 

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. 

22 

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/>. 

25 

26=============================================================================== 

27 

28""" 

29 

30from unittest import mock 

31import unittest 

32 

33from camcops_server.tasks.cpft_research_preferences import ( 

34 CpftResearchPreferences, 

35) 

36 

37 

38class CpftResearchPreferencesTests(unittest.TestCase): 

39 def setUp(self) -> None: 

40 super().setUp() 

41 

42 self.request = mock.Mock() 

43 

44 def test_complete_when_all_answers_valid(self) -> None: 

45 task = CpftResearchPreferences() 

46 

47 task.contact_preference = "Y" 

48 task.contact_by_email = True 

49 task.research_opt_out = False 

50 

51 self.assertTrue(task.is_complete()) 

52 

53 def test_complete_when_red_and_email_null(self) -> None: 

54 task = CpftResearchPreferences() 

55 

56 task.contact_preference = "R" 

57 

58 self.assertTrue(task.is_complete()) 

59 

60 def test_incomplete_when_contact_preference_null(self) -> None: 

61 task = CpftResearchPreferences() 

62 task.contact_by_email = True 

63 task.research_opt_out = False 

64 

65 self.assertFalse(task.is_complete()) 

66 

67 def test_incomplete_when_yellow_and_email_null(self) -> None: 

68 task = CpftResearchPreferences() 

69 task.contact_preference = "Y" 

70 task.research_opt_out = False 

71 

72 self.assertFalse(task.is_complete()) 

73 

74 def test_incomplete_when_any_field_invalid(self) -> None: 

75 all_fields = [ 

76 "contact_preference", 

77 "contact_by_email", 

78 "research_opt_out", 

79 ] 

80 

81 for invalid_field in all_fields: 

82 task = CpftResearchPreferences() 

83 

84 task.contact_preference = "G" 

85 task.contact_by_email = True 

86 task.research_opt_out = False 

87 self.assertTrue(task.is_complete()) 

88 

89 setattr(task, invalid_field, 10.5) 

90 self.assertFalse( 

91 task.is_complete(), 

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

93 )