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_patient_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 hl7 

30import pendulum 

31 

32from camcops_server.cc_modules.cc_simpleobjects import BarePatientInfo 

33from camcops_server.cc_modules.cc_patient import Patient 

34from camcops_server.cc_modules.cc_patientidnum import PatientIdNum 

35from camcops_server.cc_modules.cc_simpleobjects import IdNumReference 

36from camcops_server.cc_modules.cc_taskschedule import ( 

37 PatientTaskSchedule, 

38 TaskSchedule, 

39 TaskScheduleItem, 

40) 

41from camcops_server.cc_modules.cc_tsv import TsvPage 

42from camcops_server.cc_modules.cc_unittest import DemoDatabaseTestCase 

43from camcops_server.cc_modules.cc_xml import XmlElement 

44 

45 

46# ============================================================================= 

47# Unit tests 

48# ============================================================================= 

49 

50class PatientTests(DemoDatabaseTestCase): 

51 """ 

52 Unit tests. 

53 """ 

54 def test_patient(self) -> None: 

55 self.announce("test_patient") 

56 from camcops_server.cc_modules.cc_group import Group 

57 

58 req = self.req 

59 q = self.dbsession.query(Patient) 

60 p = q.first() # type: Patient 

61 assert p, "Missing Patient in demo database!" 

62 

63 for pidnum in p.get_idnum_objects(): 

64 self.assertIsInstance(pidnum, PatientIdNum) 

65 for idref in p.get_idnum_references(): 

66 self.assertIsInstance(idref, IdNumReference) 

67 for idnum in p.get_idnum_raw_values_only(): 

68 self.assertIsInstance(idnum, int) 

69 self.assertIsInstance(p.get_xml_root(req), XmlElement) 

70 self.assertIsInstance(p.get_tsv_page(req), TsvPage) 

71 self.assertIsInstance(p.get_bare_ptinfo(), BarePatientInfo) 

72 self.assertIsInstanceOrNone(p.group, Group) 

73 self.assertIsInstance(p.satisfies_upload_id_policy(), bool) 

74 self.assertIsInstance(p.satisfies_finalize_id_policy(), bool) 

75 self.assertIsInstance(p.get_surname(), str) 

76 self.assertIsInstance(p.get_forename(), str) 

77 self.assertIsInstance(p.get_surname_forename_upper(), str) 

78 for longform in [True, False]: 

79 self.assertIsInstance(p.get_dob_html(req, longform), str) 

80 age_str_int = p.get_age(req) 

81 assert isinstance(age_str_int, str) or isinstance(age_str_int, int) 

82 self.assertIsInstanceOrNone(p.get_dob(), pendulum.Date) 

83 self.assertIsInstanceOrNone(p.get_dob_str(), str) 

84 age_at_str_int = p.get_age_at(req.now) 

85 assert isinstance(age_at_str_int, str) or isinstance(age_at_str_int, int) # noqa 

86 self.assertIsInstance(p.is_female(), bool) 

87 self.assertIsInstance(p.is_male(), bool) 

88 self.assertIsInstance(p.get_sex(), str) 

89 self.assertIsInstance(p.get_sex_verbose(), str) 

90 self.assertIsInstance(p.get_address(), str) 

91 self.assertIsInstance(p.get_email(), str) 

92 self.assertIsInstance(p.get_hl7_pid_segment(req, self.recipdef), 

93 hl7.Segment) 

94 self.assertIsInstanceOrNone(p.get_idnum_object(which_idnum=1), 

95 PatientIdNum) 

96 self.assertIsInstanceOrNone(p.get_idnum_value(which_idnum=1), int) 

97 self.assertIsInstance(p.get_iddesc(req, which_idnum=1), str) 

98 self.assertIsInstance(p.get_idshortdesc(req, which_idnum=1), str) 

99 self.assertIsInstance(p.is_preserved(), bool) 

100 self.assertIsInstance(p.is_finalized(), bool) 

101 self.assertIsInstance(p.user_may_edit(req), bool) 

102 

103 def test_surname_forename_upper(self) -> None: 

104 patient = Patient() 

105 patient.forename = "Forename" 

106 patient.surname = "Surname" 

107 

108 self.assertEqual(patient.get_surname_forename_upper(), 

109 "SURNAME, FORENAME") 

110 

111 def test_surname_forename_upper_no_forename(self) -> None: 

112 patient = Patient() 

113 patient.surname = "Surname" 

114 

115 self.assertEqual(patient.get_surname_forename_upper(), 

116 "SURNAME, (UNKNOWN)") 

117 

118 def test_surname_forename_upper_no_surname(self) -> None: 

119 patient = Patient() 

120 patient.forename = "Forename" 

121 

122 self.assertEqual(patient.get_surname_forename_upper(), 

123 "(UNKNOWN), FORENAME") 

124 

125 

126class LineageTests(DemoDatabaseTestCase): 

127 def create_tasks(self) -> None: 

128 # Actually not creating any tasks but we don't want the patients 

129 # created by default in the baseclass 

130 

131 # First record for patient 1 

132 self.set_era("2020-01-01") 

133 

134 self.patient_1 = Patient() 

135 self.patient_1.id = 1 

136 self._apply_standard_db_fields(self.patient_1) 

137 self.dbsession.add(self.patient_1) 

138 

139 # First ID number record for patient 1 

140 self.patient_idnum_1_1 = PatientIdNum() 

141 self.patient_idnum_1_1.id = 3 

142 self._apply_standard_db_fields(self.patient_idnum_1_1) 

143 self.patient_idnum_1_1.patient_id = 1 

144 self.patient_idnum_1_1.which_idnum = self.nhs_iddef.which_idnum 

145 self.patient_idnum_1_1.idnum_value = 555 

146 self.dbsession.add(self.patient_idnum_1_1) 

147 

148 # Second ID number record for patient 1 

149 self.patient_idnum_1_2 = PatientIdNum() 

150 self.patient_idnum_1_2.id = 3 

151 self._apply_standard_db_fields(self.patient_idnum_1_2) 

152 # This one is not current 

153 self.patient_idnum_1_2._current = False 

154 self.patient_idnum_1_2.patient_id = 1 

155 self.patient_idnum_1_2.which_idnum = self.nhs_iddef.which_idnum 

156 self.patient_idnum_1_2.idnum_value = 555 

157 self.dbsession.add(self.patient_idnum_1_2) 

158 

159 self.dbsession.commit() 

160 

161 def test_gen_patient_idnums_even_noncurrent(self) -> None: 

162 idnums = list(self.patient_1.gen_patient_idnums_even_noncurrent()) 

163 

164 self.assertEqual(len(idnums), 2) 

165 

166 

167class PatientDeleteTests(DemoDatabaseTestCase): 

168 def test_deletes_patient_task_schedule(self) -> None: 

169 schedule = TaskSchedule() 

170 schedule.group_id = self.group.id 

171 self.dbsession.add(schedule) 

172 self.dbsession.flush() 

173 

174 item = TaskScheduleItem() 

175 item.schedule_id = schedule.id 

176 item.task_table_name = "ace3" 

177 item.due_from = pendulum.Duration(days=30) 

178 item.due_by = pendulum.Duration(days=60) 

179 self.dbsession.add(item) 

180 self.dbsession.flush() 

181 

182 patient = self.create_patient() 

183 

184 pts = PatientTaskSchedule() 

185 pts.schedule_id = schedule.id 

186 pts.patient_pk = patient.pk 

187 self.dbsession.add(pts) 

188 self.dbsession.commit() 

189 

190 self.assertIsNotNone(self.dbsession.query(TaskSchedule).filter( 

191 TaskSchedule.id == schedule.id).one_or_none()) 

192 self.assertIsNotNone(self.dbsession.query(TaskScheduleItem).filter( 

193 TaskScheduleItem.id == item.id).one_or_none()) 

194 self.assertIsNotNone(self.dbsession.query(PatientTaskSchedule).filter( 

195 PatientTaskSchedule.id == pts.id).one_or_none()) 

196 

197 self.dbsession.delete(patient) 

198 self.dbsession.commit() 

199 

200 self.assertIsNotNone(self.dbsession.query(TaskSchedule).filter( 

201 TaskSchedule.id == schedule.id).one_or_none()) 

202 self.assertIsNotNone(self.dbsession.query(TaskScheduleItem).filter( 

203 TaskScheduleItem.id == item.id).one_or_none()) 

204 

205 self.assertIsNone(self.dbsession.query(PatientTaskSchedule).filter( 

206 PatientTaskSchedule.id == pts.id).one_or_none())