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

1"""

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

28 from unittest import mock, TestCase

29

30 from camcops_server.tasks.basdai import Basdai

31

32

33# =============================================================================

34# Unit tests

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

36

37

38 class BasdaiTests(TestCase):

39 def setUp(self) -> None:

40 super().setUp()

41

42 self.request = mock.Mock()

43

44 def test_basdai_calculation(self) -> None:

45 basdai = Basdai()

46

47 basdai.q1 = 2

48 basdai.q2 = 10

49 basdai.q3 = 7

50 basdai.q4 = 1

51

52 basdai.q5 = 9

53 basdai.q6 = 3

54

55 # 2 + 10 + 7 + 1 = 20

56 # (9 + 3) / 2 = 6

57 # 20 + 6 = 26

58 # 26 / 5 = 5.2

59

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

61

62 def test_basdai_none_when_field_none(self) -> None:

63 basdai = Basdai()

64

65 self.assertIsNone(basdai.basdai())

66

67 def test_basdai_complete_when_all_answers_valid(self) -> None:

68 basdai = Basdai()

69

70 basdai.q1 = 0

71 basdai.q2 = 0

72 basdai.q3 = 0

73 basdai.q4 = 0

74

75 basdai.q5 = 0

76 basdai.q6 = 0

77

78 self.assertTrue(basdai.is_complete())

79

80 def test_basdai_incomplete_when_a_field_none(self) -> None:

81 basdai = Basdai()

82

83 basdai.q1 = None

84 basdai.q2 = 0

85 basdai.q3 = 0

86 basdai.q4 = 0

87

88 basdai.q5 = 0

89 basdai.q6 = 0

90

91 self.assertFalse(basdai.is_complete())

92

93 def test_basdai_incomplete_when_a_field_invalid(self) -> None:

94 basdai = Basdai()

95

96 basdai.q1 = 11

97 basdai.q2 = 0

98 basdai.q3 = 0

99 basdai.q4 = 0

100

101 basdai.q5 = 0

102 basdai.q6 = 0

103

104 self.assertFalse(basdai.is_complete())

105

106 def test_activity_state_qmark_for_none(self) -> None:

107 basdai = Basdai()

108

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

110 mock_basdai.return_value = None

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

112

113 def test_activity_state_inactive_for_less_than_4(self) -> None:

114 basdai = Basdai()

115

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)

120

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

122

123 def test_activity_state_active_for_4(self) -> None:

124 basdai = Basdai()

125

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)

130

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