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

1#!/usr/bin/env python 

2 

3""" 

4camcops_server/tasks/tests/basdai_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, TestCase 

31 

32from camcops_server.tasks.basdai import Basdai 

33 

34 

35# ============================================================================= 

36# Unit tests 

37# ============================================================================= 

38 

39 

40class BasdaiTests(TestCase): 

41 def setUp(self) -> None: 

42 super().setUp() 

43 

44 self.request = mock.Mock() 

45 

46 def test_basdai_calculation(self) -> None: 

47 basdai = Basdai() 

48 

49 basdai.q1 = 2 

50 basdai.q2 = 10 

51 basdai.q3 = 7 

52 basdai.q4 = 1 

53 

54 basdai.q5 = 9 

55 basdai.q6 = 3 

56 

57 # 2 + 10 + 7 + 1 = 20 

58 # (9 + 3) / 2 = 6 

59 # 20 + 6 = 26 

60 # 26 / 5 = 5.2 

61 

62 self.assertEqual(basdai.basdai(), 5.2) 

63 

64 def test_basdai_none_when_field_none(self) -> None: 

65 basdai = Basdai() 

66 

67 self.assertIsNone(basdai.basdai()) 

68 

69 def test_basdai_complete_when_all_answers_valid(self) -> None: 

70 basdai = Basdai() 

71 

72 basdai.q1 = 0 

73 basdai.q2 = 0 

74 basdai.q3 = 0 

75 basdai.q4 = 0 

76 

77 basdai.q5 = 0 

78 basdai.q6 = 0 

79 

80 self.assertTrue(basdai.is_complete()) 

81 

82 def test_basdai_incomplete_when_a_field_none(self) -> None: 

83 basdai = Basdai() 

84 

85 basdai.q1 = None 

86 basdai.q2 = 0 

87 basdai.q3 = 0 

88 basdai.q4 = 0 

89 

90 basdai.q5 = 0 

91 basdai.q6 = 0 

92 

93 self.assertFalse(basdai.is_complete()) 

94 

95 def test_basdai_incomplete_when_a_field_invalid(self) -> None: 

96 basdai = Basdai() 

97 

98 basdai.q1 = 11 

99 basdai.q2 = 0 

100 basdai.q3 = 0 

101 basdai.q4 = 0 

102 

103 basdai.q5 = 0 

104 basdai.q6 = 0 

105 

106 self.assertFalse(basdai.is_complete()) 

107 

108 def test_activity_state_qmark_for_none(self) -> None: 

109 basdai = Basdai() 

110 

111 with mock.patch.object(basdai, "basdai") as mock_basdai: 

112 mock_basdai.return_value = None 

113 self.assertEqual(basdai.activity_state(self.request), "?") 

114 

115 def test_activity_state_inactive_for_less_than_4(self) -> None: 

116 basdai = Basdai() 

117 

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) 

122 

123 mock_wxstring.assert_called_once_with(self.request, "inactive") 

124 

125 def test_activity_state_active_for_4(self) -> None: 

126 basdai = Basdai() 

127 

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) 

132 

133 mock_wxstring.assert_called_once_with(self.request, "active")