Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a sync lab tests that iterates through patients and runs update … #792

Merged
merged 1 commit into from
Feb 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions intrahospital_api/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,43 @@ def async_load_patient(patient_id, patient_load_id):
raise


def sync_all_patients():
"""
A utility to go through all patients and
sync them where possible.

This is expected to be called from the shell
"""
patients = Patient.objects.all().prefetch_related("demographics_set")
count = patients.count()
for number, patient in enumerate(patients):
logger.info("Synching {} ({}/{})".format(
patient.id, number+1, count
))
try:
sync_patient(patient)
except Exception:
log_errors("Unable to sync {}".format(patient.id))


def sync_patient(patient):
hospital_number = patient.demographics_set.all()[0].hospital_number
results = api.results_for_hospital_number(
hospital_number
)
logger.info(
"fetched results for patient {}".format(patient.id)
)
update_lab_tests.update_tests(patient, results)
logger.info(
"tests synced for {}".format(patient.id)
)
update_demographics.update_patient_demographics(patient)
logger.info(
"demographics synced for {}".format(patient.id)
)


@transaction.atomic
def _load_patient(patient, patient_load):
logger.info(
Expand Down
57 changes: 57 additions & 0 deletions intrahospital_api/test/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,3 +619,60 @@ def test_any_loads_running_false(self):
started=timezone.now()
)
self.assertFalse(loader.any_loads_running())


class SynchAllPatientsTestCase(ApiTestCase):
@mock.patch('intrahospital_api.loader.sync_patient')
@mock.patch.object(loader.logger, 'info')
def test_sync_all_patients(self, info, sync_patient):
p, _ = self.new_patient_and_episode_please()
loader.sync_all_patients()

info.assert_called_once_with("Synching {} (1/1)".format(
p.id
))
sync_patient.assert_called_once_with(p)

@mock.patch('intrahospital_api.loader.sync_patient')
@mock.patch('intrahospital_api.loader.log_errors')
@mock.patch.object(loader.logger, 'info')
def test_sync_all_patients_with_error(self, info, log_errors, sync_patient):
sync_patient.side_effect = ValueError('Boom')
patient, _ = self.new_patient_and_episode_please()
loader.sync_all_patients()
log_errors.assert_called_once_with(
"Unable to sync {}".format(patient.id)
)


class SynchPatientTestCase(ApiTestCase):
@mock.patch.object(loader.logger, 'info')
@mock.patch.object(loader.api, 'results_for_hospital_number')
@mock.patch('intrahospital_api.loader.update_lab_tests.update_tests')
@mock.patch(
'intrahospital_api.loader.update_demographics.update_patient_demographics'
)
def test_synch_patient(
self, update_demographics, update_tests, results, info
):
patient, _ = self.new_patient_and_episode_please()
patient.demographics_set.update(
hospital_number="111"
)
results.return_value = "some_results"
loader.sync_patient(patient)
results.assert_called_once_with('111')
update_tests.assert_called_once_with(patient, "some_results")
update_demographics.assert_called_once_with(patient)
self.assertEqual(
info.call_args_list[0][0][0],
"fetched results for patient {}".format(patient.id)
)
self.assertEqual(
info.call_args_list[1][0][0],
"tests synced for {}".format(patient.id)
)
self.assertEqual(
info.call_args_list[2][0][0],
"demographics synced for {}".format(patient.id)
)