Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1#!/usr/bin/env python 

2 

3""" 

4camcops_server/tasks/tests/basdai_tests.py 

5 

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

7 

8 Copyright (C) 2012-2020 Rudolf Cardinal (rudolf@pobox.com). 

9 

10 This file is part of CamCOPS. 

11 

12 CamCOPS is free software: you can redistribute it and/or modify 

13 it under the terms of the GNU General Public License as published by 

14 the Free Software Foundation, either version 3 of the License, or 

15 (at your option) any later version. 

16 

17 CamCOPS is distributed in the hope that it will be useful, 

18 but WITHOUT ANY WARRANTY; without even the implied warranty of 

19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

20 GNU General Public License for more details. 

21 

22 You should have received a copy of the GNU General Public License 

23 along with CamCOPS. If not, see <https://www.gnu.org/licenses/>. 

24 

25=============================================================================== 

26 

27""" 

28 

29from unittest import mock, TestCase 

30 

31from camcops_server.tasks.basdai import Basdai 

32 

33 

34# ============================================================================= 

35# Unit tests 

36# ============================================================================= 

37 

38class 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")