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/perinatalpoem_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 typing import Generator 

30 

31import pendulum 

32 

33from camcops_server.cc_modules.cc_unittest import DemoDatabaseTestCase 

34 

35from camcops_server.tasks.perinatalpoem import ( 

36 PerinatalPoem, 

37 PerinatalPoemReport, 

38) 

39 

40 

41# ============================================================================= 

42# Unit tests 

43# ============================================================================= 

44 

45class PerinatalPoemReportTestCase(DemoDatabaseTestCase): 

46 def __init__(self, *args, **kwargs) -> None: 

47 super().__init__(*args, **kwargs) 

48 self.id_sequence = self.get_id() 

49 

50 def setUp(self) -> None: 

51 super().setUp() 

52 

53 self.report = PerinatalPoemReport() 

54 

55 # Really only needed for tests 

56 self.report.start_datetime = None 

57 self.report.end_datetime = None 

58 

59 @staticmethod 

60 def get_id() -> Generator[int, None, None]: 

61 i = 1 

62 

63 while True: 

64 yield i 

65 i += 1 

66 

67 def create_task(self, **kwargs) -> None: 

68 task = PerinatalPoem() 

69 self.apply_standard_task_fields(task) 

70 task.id = next(self.id_sequence) 

71 

72 era = kwargs.pop('era', None) 

73 if era is not None: 

74 task.when_created = pendulum.parse(era) 

75 

76 for name, value in kwargs.items(): 

77 setattr(task, name, value) 

78 

79 self.dbsession.add(task) 

80 

81 

82class PerinatalPoemReportTests(PerinatalPoemReportTestCase): 

83 """ 

84 Most of the base class tested in APEQCPFT Perinatal so just some basic 

85 sanity checking here 

86 """ 

87 

88 def create_tasks(self): 

89 self.create_task(general_comments="comment 1") 

90 self.create_task(general_comments="comment 2") 

91 self.create_task(general_comments="comment 3") 

92 

93 self.dbsession.commit() 

94 

95 def test_qa_rows_counts(self) -> None: 

96 tables = self.report._get_html_tables(self.req) 

97 

98 rows = tables[0].rows 

99 

100 self.assertEqual(len(rows), 1) 

101 self.assertEqual(len(rows[0]), 4) 

102 

103 def test_qb_rows_counts(self) -> None: 

104 tables = self.report._get_html_tables(self.req) 

105 

106 rows = tables[1].rows 

107 

108 self.assertEqual(len(rows), 1) 

109 self.assertEqual(len(rows[0]), 4) 

110 

111 def test_q1_rows_counts(self) -> None: 

112 tables = self.report._get_html_tables(self.req) 

113 

114 rows = tables[2].rows 

115 

116 self.assertEqual(len(rows), 2) 

117 self.assertEqual(len(rows[0]), 7) 

118 

119 def test_q2_rows_counts(self) -> None: 

120 tables = self.report._get_html_tables(self.req) 

121 

122 rows = tables[3].rows 

123 

124 self.assertEqual(len(rows), 12) 

125 self.assertEqual(len(rows[0]), 6) 

126 

127 def test_q3_rows_counts(self) -> None: 

128 tables = self.report._get_html_tables(self.req) 

129 

130 rows = tables[4].rows 

131 

132 self.assertEqual(len(rows), 6) 

133 self.assertEqual(len(rows[0]), 6) 

134 

135 def test_participation_rows_counts(self) -> None: 

136 tables = self.report._get_html_tables(self.req) 

137 

138 rows = tables[5].rows 

139 

140 self.assertEqual(len(rows), 1) 

141 self.assertEqual(len(rows[0]), 4) 

142 

143 def test_comments(self) -> None: 

144 expected_comments = [ 

145 "comment 1", "comment 2", "comment 3", 

146 ] 

147 comments = self.report._get_comments(self.req) 

148 self.assertEqual(comments, expected_comments) 

149 

150 

151class PerinatalPoemReportDateRangeTests(PerinatalPoemReportTestCase): 

152 def create_tasks(self) -> None: 

153 self.create_task(general_comments="comments 1", 

154 era="2018-10-01T00:00:00.000000+00:00") 

155 self.create_task(general_comments="comments 2", 

156 era="2018-10-02T00:00:00.000000+00:00") 

157 self.create_task(general_comments="comments 3", 

158 era="2018-10-03T00:00:00.000000+00:00") 

159 self.create_task(general_comments="comments 4", 

160 era="2018-10-04T00:00:00.000000+00:00") 

161 self.create_task(general_comments="comments 5", 

162 era="2018-10-05T00:00:00.000000+00:00") 

163 self.dbsession.commit() 

164 

165 def test_comments_filtered_by_date(self) -> None: 

166 self.report.start_datetime = "2018-10-02T00:00:00.000000+00:00" 

167 self.report.end_datetime = "2018-10-05T00:00:00.000000+00:00" 

168 

169 comments = self.report._get_comments(self.req) 

170 self.assertEqual(len(comments), 3) 

171 

172 self.assertEqual(comments[0], "comments 2") 

173 self.assertEqual(comments[1], "comments 3") 

174 self.assertEqual(comments[2], "comments 4")