class PatientImportTestCase(TestCase): fixtures = ['clinics.json', 'patients.json',] def setUp(self): self.importer = Importer() self.user = User.objects.get(username='******') def testInvalidAgeImport(self): # import an invalid patient record self.assertRaises(InvalidValueException, # exception self.importer.update_local_patient, # callable self.user, # args create_instance(PatientUpdate, { 'te_id': '01-1235', 'age': '3135', 'sex': 'Female', 'celphone': '082123' }) ) def testInvalidSexImport(self): # import an invalid patient record self.assertRaises(InvalidValueException, # exception self.importer.update_local_patient, # callable self.user, # args create_instance(PatientUpdate, { 'te_id': '01-1235', 'age': '31', 'sex': 'Feale', 'celphone': '082123' }) ) def testBasicImport(self): """basic patient record import""" patient = self.importer.update_local_patient(self.user, create_instance(PatientUpdate, { 'te_id': '03-12345', 'age': '25', 'sex': 'Male', 'celphone': '0821231234' })) # reload to make sure we have the database values patient = reload_record(patient) self.assertEquals(patient.te_id, '03-12345') self.assertEquals(patient.age, 25) self.assertEquals(patient.sex, 'm') self.assertEquals(patient.msisdns.latest('id').msisdn, '27821231234') self.assertEquals( patient.history.latest().get_history_type_display(), 'Created' ) def testAlterDetails(self): """duplicate 'te_id' import with altered details""" original_patient = Patient.objects.get(te_id='02-12345') original_history_count = original_patient.history.count() patient = self.importer.update_local_patient(self.user, create_instance(PatientUpdate, { 'te_id': '02-12345', 'age': '35', 'sex': 'Female', 'celphone':'0821234321' })) patient = reload_record(patient) self.assertEquals( patient.history.latest().get_history_type_display(), 'Changed' ) self.assertEquals(patient.age, 35) self.assertEquals(patient.sex, 'f') self.assertEquals(patient.msisdns.latest('id').msisdn, '27821234321') self.assertEquals(patient.history.count(), original_history_count + 1) # this is an update, should have a new history item def testDuplicateMsisdnImport(self): """duplicate 'msisdn' import""" # new patient, not in fixtures patientA = self.importer.update_local_patient(self.user, create_instance(PatientUpdate, { 'te_id': '03-12345', 'age': '30', 'sex': 'Male', 'celphone': '0821111111' })) # existing patient, in fixtures patientB = self.importer.update_local_patient(self.user, create_instance(PatientUpdate, { 'te_id': '01-12345', 'age': '30', 'sex': 'Male', 'celphone': '0821111111' })) patientA = reload_record(patientA) patientB = reload_record(patientB) self.assertEquals( patientA.history.latest().get_history_type_display(), 'Created' ) self.assertEquals( patientB.history.latest().get_history_type_display(), 'Changed' ) # both phone numbers should point to the same MSISDN record # in the database self.assertEqual( patientA.msisdns.latest('id').id, patientB.msisdns.latest('id').id ) def testCountryCodeMsisdn(self): """country code included in 'msisdn'""" patient = self.importer.update_local_patient(self.user, create_instance(PatientUpdate, { 'te_id': '03-12345', 'age': '55', 'sex': 'Male', 'celphone': '+27823211234' })) patient = reload_record(patient) self.assertEquals(patient.history.latest().get_history_type_display(), 'Created') self.assertEquals(patient.msisdns.latest('id').msisdn, '27823211234') def testMultipleMsisdn(self): """multiple 'msisdn' import (ons country code 'msisdn' without plus)""" patient = self.importer.update_local_patient(self.user, create_instance(PatientUpdate, { 'te_id': '03-12345', 'age': '18', 'sex': 'Female', 'celphone': '0821231111/27821232222' })) patient = reload_record(patient) self.assertEquals(patient.history.latest().get_history_type_display(), 'Created') msisdns = patient.msisdns.all() self.assertEqual(len(msisdns), 2) self.assertEqual(msisdns[0].msisdn, '27821232222') self.assertEqual(msisdns[1].msisdn, '27821231111')