Coverage for cc_modules/tests/cc_patient_tests.py: 22%

1"""

2 camcops_server/cc_modules/tests/cc_patient_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 import hl7

29 import pendulum

30

31 from camcops_server.cc_modules.cc_group import Group

32 from camcops_server.cc_modules.cc_simpleobjects import BarePatientInfo

33 from camcops_server.cc_modules.cc_patientidnum import PatientIdNum

34 from camcops_server.cc_modules.cc_simpleobjects import IdNumReference

35 from camcops_server.cc_modules.cc_taskschedule import (

36 PatientTaskSchedule,

37 TaskSchedule,

38 TaskScheduleItem,

39)

40 from camcops_server.cc_modules.cc_spreadsheet import SpreadsheetPage

41 from camcops_server.cc_modules.cc_testfactories import (

42 GroupFactory,

43 NHSPatientIdNumFactory,

44 PatientFactory,

45 PatientTaskScheduleFactory,

46 RioPatientIdNumFactory,

47 ServerCreatedPatientFactory,

48 TaskScheduleFactory,

49 TaskScheduleItemFactory,

50 UserFactory,

51 UserGroupMembershipFactory,

52)

53 from camcops_server.cc_modules.cc_unittest import (

54 BasicDatabaseTestCase,

55 DemoRequestTestCase,

56)

57 from camcops_server.cc_modules.cc_xml import XmlElement

58

59

60# =============================================================================

61# Unit tests

62# =============================================================================

63

64

65 class PatientTests(DemoRequestTestCase):

66 def test_patient(self) -> None:

67 req = self.req

68 req._debugging_user = UserFactory()

69

70 p = PatientFactory()

71 nhs_idnum = NHSPatientIdNumFactory(patient=p)

72 RioPatientIdNumFactory(patient=p)

73

74 idnum_objects = p.get_idnum_objects()

75 self.assertEqual(len(idnum_objects), 2)

76 for pidnum in idnum_objects:

77 self.assertIsInstance(pidnum, PatientIdNum)

78

79 idnum_references = p.get_idnum_references()

80 self.assertEqual(len(idnum_references), 2)

81 for idref in idnum_references:

82 self.assertIsInstance(idref, IdNumReference)

83

84 idnum_raw_values = p.get_idnum_raw_values_only()

85 self.assertEqual(len(idnum_raw_values), 2)

86 for idnum in idnum_raw_values:

87 self.assertIsInstance(idnum, int)

88

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

90 self.assertIsInstance(p.get_spreadsheet_page(req), SpreadsheetPage)

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

92 self.assertIsInstanceOrNone(p.group, Group)

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

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

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

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

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

98 for longform in (True, False):

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

100 age_str_int = p.get_age(req)

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

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

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

104 age_at_str_int = p.get_age_at(req.now)

105 assert isinstance(age_at_str_int, str) or isinstance(

106 age_at_str_int, int

107 )

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

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

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

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

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

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

114 self.assertIsInstance(

115 p.get_hl7_pid_segment(req, self.recipdef), hl7.Segment

116 )

117 self.assertIsInstanceOrNone(

118 p.get_idnum_object(which_idnum=nhs_idnum.which_idnum), PatientIdNum

119 )

120 self.assertIsInstanceOrNone(

121 p.get_idnum_value(which_idnum=nhs_idnum.which_idnum), int

122 )

123 self.assertIsInstance(

124 p.get_iddesc(req, which_idnum=nhs_idnum.which_idnum), str

125 )

126 self.assertIsInstance(

127 p.get_idshortdesc(req, which_idnum=nhs_idnum.which_idnum), str

128 )

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

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

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

132

133 def test_surname_forename_upper(self) -> None:

134 patient = PatientFactory(forename="Forename", surname="Surname")

135 self.assertEqual(

136 patient.get_surname_forename_upper(), "SURNAME, FORENAME"

137 )

138

139 def test_surname_forename_upper_no_forename(self) -> None:

140 patient = PatientFactory(forename=None, surname="Surname")

141 self.assertEqual(

142 patient.get_surname_forename_upper(), "SURNAME, (UNKNOWN)"

143 )

144

145 def test_surname_forename_upper_no_surname(self) -> None:

146 patient = PatientFactory(forename="Forename", surname=None)

147 self.assertEqual(

148 patient.get_surname_forename_upper(), "(UNKNOWN), FORENAME"

149 )

150

151

152 class LineageTests(DemoRequestTestCase):

153 def setUp(self) -> None:

154 super().setUp()

155

156 self.patient = PatientFactory()

157 self.current_patient_idnum = NHSPatientIdNumFactory(

158 patient=self.patient

159 )

160 self.assertTrue(self.current_patient_idnum._current)

161

162 self.not_current_patient_idnum = NHSPatientIdNumFactory(

163 patient=self.patient,

164 _current=False,

165 id=self.current_patient_idnum.id,

166 which_idnum=self.current_patient_idnum.which_idnum,

167 idnum_value=self.current_patient_idnum.idnum_value,

168 )

169 self.assertFalse(self.not_current_patient_idnum._current)

170

171 def test_gen_patient_idnums_even_noncurrent(self) -> None:

172 idnums = list(self.patient.gen_patient_idnums_even_noncurrent())

173

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

175

176

177 class PatientDeleteTests(DemoRequestTestCase):

178 def test_deletes_patient_task_schedule(self) -> None:

179 schedule = TaskScheduleFactory()

180

181 item = TaskScheduleItemFactory(

182 task_schedule=schedule,

183 task_table_name="ace3",

184 due_from=pendulum.Duration(days=30),

185 due_by=pendulum.Duration(days=60),

186 )

187

188 patient = ServerCreatedPatientFactory()

189

190 pts = PatientTaskScheduleFactory(

191 task_schedule=schedule,

192 patient=patient,

193 )

194

195 self.assertIsNotNone(

196 self.dbsession.query(TaskSchedule)

197 .filter(TaskSchedule.id == schedule.id)

198 .one_or_none()

199 )

200 self.assertIsNotNone(

201 self.dbsession.query(TaskScheduleItem)

202 .filter(TaskScheduleItem.id == item.id)

203 .one_or_none()

204 )

205 self.assertIsNotNone(

206 self.dbsession.query(PatientTaskSchedule)

207 .filter(PatientTaskSchedule.id == pts.id)

208 .one_or_none()

209 )

210

211 self.dbsession.delete(patient)

212 self.dbsession.commit()

213

214 self.assertIsNotNone(

215 self.dbsession.query(TaskSchedule)

216 .filter(TaskSchedule.id == schedule.id)

217 .one_or_none()

218 )

219 self.assertIsNotNone(

220 self.dbsession.query(TaskScheduleItem)

221 .filter(TaskScheduleItem.id == item.id)

222 .one_or_none()

223 )

224

225 self.assertIsNone(

226 self.dbsession.query(PatientTaskSchedule)

227 .filter(PatientTaskSchedule.id == pts.id)

228 .one_or_none()

229 )

230

231

232 class PatientPermissionTests(BasicDatabaseTestCase):

233 def setUp(self) -> None:

234 super().setUp()

235

236 self.user = UserFactory()

237 self.group = GroupFactory()

238

239 def test_group_administrator_may_edit_server_patient(self) -> None:

240 patient = ServerCreatedPatientFactory(_group=self.group)

241 ugm = UserGroupMembershipFactory(

242 user_id=self.user.id, group_id=self.group.id, groupadmin=True

243 )

244

245 self.req._debugging_user = ugm.user

246 self.assertTrue(patient.user_may_edit(self.req))

247

248 def test_group_administrator_may_edit_finalized_patient(self) -> None:

249 patient = PatientFactory(_group=self.group)

250 ugm = UserGroupMembershipFactory(

251 user_id=self.user.id, group_id=self.group.id, groupadmin=True

252 )

253

254 self.assertTrue(ugm.groupadmin)

255

256 self.req._debugging_user = ugm.user

257 self.assertTrue(patient.user_may_edit(self.req))

258

259 def test_group_member_with_permission_may_edit_server_created(

260 self,

261 ) -> None:

262 patient = ServerCreatedPatientFactory(_group=self.group)

263 ugm = UserGroupMembershipFactory(

264 user_id=self.user.id,

265 group_id=self.group.id,

266 may_manage_patients=True,

267 )

268

269 self.req._debugging_user = ugm.user

270 self.assertTrue(patient.user_may_edit(self.req))

271

272 def test_group_member_with_permission_may_not_edit_finalized(self) -> None:

273 patient = PatientFactory(_group=self.group)

274 ugm = UserGroupMembershipFactory(

275 user_id=self.user.id,

276 group_id=self.group.id,

277 may_manage_patients=True,

278 )

279

280 self.req._debugging_user = ugm.user

281 self.assertFalse(patient.user_may_edit(self.req))