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/cc_modules/tests/cc_task_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 

29import logging 

30 

31from cardinal_pythonlib.logs import BraceStyleAdapter 

32from pendulum import Date, DateTime as Pendulum 

33 

34from camcops_server.cc_modules.cc_task import Task 

35from camcops_server.cc_modules.cc_unittest import DemoDatabaseTestCase 

36from camcops_server.cc_modules.cc_validators import ( 

37 validate_task_tablename, 

38) 

39 

40log = BraceStyleAdapter(logging.getLogger(__name__)) 

41 

42 

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

44# Unit testing 

45# ============================================================================= 

46 

47class TaskTests(DemoDatabaseTestCase): 

48 """ 

49 Unit tests. 

50 """ 

51 def test_query_phq9(self) -> None: 

52 self.announce("test_query_phq9") 

53 from camcops_server.tasks import Phq9 

54 phq9_query = self.dbsession.query(Phq9) 

55 results = phq9_query.all() 

56 log.info("{}", results) 

57 

58 def test_all_tasks(self) -> None: 

59 self.announce("test_all_tasks") 

60 from datetime import date 

61 import hl7 

62 from sqlalchemy.sql.schema import Column 

63 from camcops_server.cc_modules.cc_ctvinfo import CtvInfo # noqa: F811 

64 from camcops_server.cc_modules.cc_patient import Patient # noqa: F811 

65 from camcops_server.cc_modules.cc_simpleobjects import IdNumReference 

66 from camcops_server.cc_modules.cc_snomed import SnomedExpression # noqa: E501,F811 

67 from camcops_server.cc_modules.cc_string import APPSTRING_TASKNAME 

68 from camcops_server.cc_modules.cc_summaryelement import SummaryElement 

69 from camcops_server.cc_modules.cc_trackerhelpers import TrackerInfo # noqa: E501,F811 

70 from camcops_server.cc_modules.cc_tsv import TsvPage # noqa: F811 

71 from camcops_server.cc_modules.cc_xml import XmlElement 

72 

73 subclasses = Task.all_subclasses_by_tablename() 

74 tables = [cls.tablename for cls in subclasses] 

75 log.info("Actual task table names: {!r} (n={})", tables, len(tables)) 

76 req = self.req 

77 recipdef = self.recipdef 

78 for cls in subclasses: 

79 log.info("Testing {}", cls) 

80 assert cls.extrastring_taskname != APPSTRING_TASKNAME 

81 q = self.dbsession.query(cls) 

82 t = q.first() # type: Task 

83 

84 self.assertIsNotNone(t, "Missing task!") 

85 

86 # Name validity 

87 validate_task_tablename(t.tablename) 

88 

89 # Core functions 

90 self.assertIsInstance(t.is_complete(), bool) 

91 self.assertIsInstance(t.get_task_html(req), str) 

92 for trackerinfo in t.get_trackers(req): 

93 self.assertIsInstance(trackerinfo, TrackerInfo) 

94 ctvlist = t.get_clinical_text(req) 

95 if ctvlist is not None: 

96 for ctvinfo in ctvlist: 

97 self.assertIsInstance(ctvinfo, CtvInfo) 

98 for est in t.get_all_summary_tables(req): 

99 self.assertIsInstance(est.get_tsv_page(), TsvPage) 

100 self.assertIsInstance(est.get_xml_element(), XmlElement) 

101 

102 self.assertIsInstance(t.has_patient, bool) 

103 self.assertIsInstance(t.is_anonymous, bool) 

104 self.assertIsInstance(t.has_clinician, bool) 

105 self.assertIsInstance(t.has_respondent, bool) 

106 self.assertIsInstance(t.tablename, str) 

107 for fn in t.get_fieldnames(): 

108 self.assertIsInstance(fn, str) 

109 self.assertIsInstance(t.field_contents_valid(), bool) 

110 for msg in t.field_contents_invalid_because(): 

111 self.assertIsInstance(msg, str) 

112 for fn in t.get_blob_fields(): 

113 self.assertIsInstance(fn, str) 

114 

115 self.assertIsInstance(t.pk, int) # all our examples do have PKs # noqa 

116 self.assertIsInstance(t.is_preserved(), bool) 

117 self.assertIsInstance(t.was_forcibly_preserved(), bool) 

118 self.assertIsInstanceOrNone(t.get_creation_datetime(), Pendulum) 

119 self.assertIsInstanceOrNone( 

120 t.get_creation_datetime_utc(), Pendulum) 

121 self.assertIsInstanceOrNone( 

122 t.get_seconds_from_creation_to_first_finish(), float) 

123 

124 self.assertIsInstance(t.get_adding_user_id(), int) 

125 self.assertIsInstance(t.get_adding_user_username(), str) 

126 self.assertIsInstance(t.get_removing_user_username(), str) 

127 self.assertIsInstance(t.get_preserving_user_username(), str) 

128 self.assertIsInstance(t.get_manually_erasing_user_username(), str) 

129 

130 # Summaries 

131 for se in t.standard_task_summary_fields(): 

132 self.assertIsInstance(se, SummaryElement) 

133 

134 # SNOMED-CT 

135 if req.snomed_supported: 

136 for snomed_code in t.get_snomed_codes(req): 

137 self.assertIsInstance(snomed_code, SnomedExpression) 

138 

139 # Clinician 

140 self.assertIsInstance(t.get_clinician_name(), str) 

141 

142 # Respondent 

143 self.assertIsInstance(t.is_respondent_complete(), bool) 

144 

145 # Patient 

146 self.assertIsInstanceOrNone(t.patient, Patient) 

147 self.assertIsInstance(t.is_female(), bool) 

148 self.assertIsInstance(t.is_male(), bool) 

149 self.assertIsInstanceOrNone(t.get_patient_server_pk(), int) 

150 self.assertIsInstance(t.get_patient_forename(), str) 

151 self.assertIsInstance(t.get_patient_surname(), str) 

152 dob = t.get_patient_dob() 

153 assert ( 

154 dob is None or 

155 isinstance(dob, date) or 

156 isinstance(dob, Date) 

157 ) 

158 self.assertIsInstanceOrNone(t.get_patient_dob_first11chars(), str) 

159 self.assertIsInstance(t.get_patient_sex(), str) 

160 self.assertIsInstance(t.get_patient_address(), str) 

161 for idnum in t.get_patient_idnum_objects(): 

162 self.assertIsInstance(idnum.get_idnum_reference(), 

163 IdNumReference) 

164 self.assertIsInstance(idnum.is_superficially_valid(), bool) 

165 self.assertIsInstance(idnum.description(req), str) 

166 self.assertIsInstance(idnum.short_description(req), str) 

167 self.assertIsInstance(idnum.get_filename_component(req), str) 

168 

169 # HL7 

170 pidseg = t.get_patient_hl7_pid_segment(req, recipdef) 

171 assert isinstance(pidseg, str) or isinstance(pidseg, hl7.Segment) 

172 for dataseg in t.get_hl7_data_segments(req, recipdef): 

173 self.assertIsInstance(dataseg, hl7.Segment) 

174 for dataseg in t.get_hl7_extra_data_segments(recipdef): 

175 self.assertIsInstance(dataseg, hl7.Segment) 

176 

177 # Other properties 

178 self.assertIsInstance(t.is_erased(), bool) 

179 self.assertIsInstance(t.is_live_on_tablet(), bool) 

180 for attrname, col in t.gen_text_filter_columns(): 

181 self.assertIsInstance(attrname, str) 

182 self.assertIsInstance(col, Column) 

183 

184 # Views 

185 for page in t.get_tsv_pages(req): 

186 self.assertIsInstance(page.get_tsv(), str) 

187 self.assertIsInstance(t.get_xml(req), str) 

188 self.assertIsInstance(t.get_html(req), str) 

189 self.assertIsInstance(t.get_pdf(req), bytes) 

190 self.assertIsInstance(t.get_pdf_html(req), str) 

191 self.assertIsInstance(t.suggested_pdf_filename(req), str) 

192 self.assertIsInstance( 

193 t.get_rio_metadata(req, 

194 which_idnum=1, 

195 uploading_user_id=self.user.id, 

196 document_type="some_doc_type"), 

197 str 

198 ) 

199 

200 # Special operations 

201 t.apply_special_note(req, "Debug: Special note! (1)", 

202 from_console=True) 

203 t.apply_special_note(req, "Debug: Special note! (2)", 

204 from_console=False) 

205 self.assertIsInstance(t.special_notes, list) 

206 t.cancel_from_export_log(req, from_console=True) 

207 t.cancel_from_export_log(req, from_console=False) 

208 

209 # Destructive special operations 

210 self.assertFalse(t.is_erased()) 

211 t.manually_erase(req) 

212 self.assertTrue(t.is_erased()) 

213 t.delete_entirely(req)