Skip to content

Commit

Permalink
Merge 5b09c9c into 0a9ea6c
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmiller committed Nov 20, 2018
2 parents 0a9ea6c + 5b09c9c commit 4a60038
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 45 deletions.
79 changes: 49 additions & 30 deletions intrahospital_api/services/appointments/service.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""
Appointments service for elCID RFH
"""
import datetime
from django.utils import timezone
from opal.models import Patient
Expand All @@ -11,25 +14,28 @@
from apps.tb.patient_lists import TbPatientList


def refresh_patient(patient):
if patient.episode_set.filter(
category_name=TbEpisode.display_name
).exists():
patient.appointment_set.all().delete()
load_patient(patient)


def load_patient(patient):
api = service_utils.get_api("appointments")
appointments = api.tb_appointments_for_hospital_number(
patient.demographics_set.first().hospital_number
def _get_or_create_appointment(patient, appointment_dict):
"""
INTERNAL METHOD, NEVER CALLED EXTERNALLY
"""
start = serialization.deserialize_datetime(
appointment_dict["start"]
)
return save_appointments(patient, appointments)
appointment = patient.appointment_set.filter(
start=start
).first()

if appointment:
return appointment, False
else:
return elcid_models.Appointment(patient=patient), True


def has_changed(appointment, appointment_dict):
def _has_changed(appointment, appointment_dict):
"""
Returns True if an appointment has changed
INTERNAL METHOD, NEVER CALLED EXTERNALLY
"""
for key in appointment_dict.keys():
model_value = getattr(appointment, key)
Expand All @@ -43,35 +49,48 @@ def has_changed(appointment, appointment_dict):
return False


def save_appointments(patient, appointment_dicts):
def _save_appointments(patient, appointment_dicts):
"""
INTERNAL METHOD, NEVER CALLED EXTERNALLY
"""
user = service_utils.get_user()
count = 0
for appointment_dict in appointment_dicts:
appointment, is_new = get_or_create_appointment(
appointment, is_new = _get_or_create_appointment(
patient, appointment_dict
)
if is_new or has_changed(appointment, appointment_dict):
if is_new or _has_changed(appointment, appointment_dict):
appointment.update_from_api_dict(appointment_dict, user)
count += 1
return count


def get_or_create_appointment(patient, appointment_dict):
start = serialization.deserialize_datetime(
appointment_dict["start"]
def _load_patient(patient):
"""
INTERNAL METHOD, NEVER CALLED EXTERNALLY
"""
api = service_utils.get_api("appointments")
appointments = api.tb_appointments_for_hospital_number(
patient.demographics_set.first().hospital_number
)
appointment = patient.appointment_set.filter(
start=start
).first()
return _save_appointments(patient, appointments)

if appointment:
return appointment, False
else:
return elcid_models.Appointment(patient=patient), True

def refresh_patient(patient):
"""
PUBLIC METHOD, CALLED EXTERNALLY
"""
if patient.episode_set.filter(
category_name=TbEpisode.display_name
).exists():
patient.appointment_set.all().delete()
_load_patient(patient)


def load_patients():
def _load_patients():
"""
INTERNAL METHOD, NEVER CALLED EXTERNALLY
Loads in all appointments for all patients with the category of tb
"""
patients = Patient.objects.filter(
Expand All @@ -80,7 +99,7 @@ def load_patients():
updated = 0

for patient in patients:
loaded = load_patient(patient)
loaded = _load_patient(patient)
if loaded:
updated += loaded

Expand All @@ -93,5 +112,5 @@ def load_patients():
batch_load = load_utils.batch_load(
service_name="appointments"
)(
load_patients
_load_patients
)
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def get_appointment(self, **kwargs):


class RefreshPatientTestCase(AbstractServiceTestCase):
@mock.patch(SERVICE_STR.format("load_patient"))
@mock.patch(SERVICE_STR.format("_load_patient"))
def test_refresh_patient(self, load_patient):
# create an appointment
self.get_appointment()
Expand All @@ -81,7 +81,7 @@ class LoadPatientTestCase(AbstractServiceTestCase):
def test_load_patient(self, get_api):
api = get_api.return_value
api.tb_appointments_for_hospital_number.return_value = self.get_api_response()
service.load_patient(self.patient)
service._load_patient(self.patient)
appointment = self.patient.appointment_set.get()
for i, v in RESPONSE.items():
self.assertEqual(
Expand All @@ -94,7 +94,7 @@ def test_has_changed_true(self):
appointment = self.get_appointment()
appointment_dict = self.get_api_response(state="DNA")[0]
self.assertTrue(
service.has_changed(
service._has_changed(
appointment, appointment_dict
)
)
Expand All @@ -103,7 +103,7 @@ def test_has_changed_false(self):
appointment = self.get_appointment()
appointment_dict = self.get_api_response()[0]
self.assertFalse(
service.has_changed(
service._has_changed(
appointment, appointment_dict
)
)
Expand All @@ -112,7 +112,7 @@ def test_has_changed_false(self):
class SaveAppointmentsTestCase(AbstractServiceTestCase):
def test_save_appointments_new(self):
appointment_dicts = self.get_api_response()
result = service.save_appointments(self.patient, appointment_dicts)
result = service._save_appointments(self.patient, appointment_dicts)
appointment = elcid_models.Appointment.objects.first()
self.assertEqual(appointment.patient, self.patient)
self.assertTrue(result)
Expand All @@ -122,7 +122,7 @@ def test_save_appointments_unchanged_old(self):
appointment.updated = None
appointment.save()
appointment_dicts = self.get_api_response()
result = service.save_appointments(self.patient, appointment_dicts)
result = service._save_appointments(self.patient, appointment_dicts)

reloaded_appointment = elcid_models.Appointment.objects.get(
id=appointment.id
Expand All @@ -135,7 +135,7 @@ def test_save_appointments_changed_old(self):
appointment.updated = None
appointment.save()
appointment_dicts = self.get_api_response(state="DNA")
result = service.save_appointments(self.patient, appointment_dicts)
result = service._save_appointments(self.patient, appointment_dicts)
reloaded_appointment = elcid_models.Appointment.objects.get(
id=appointment.id
)
Expand All @@ -149,7 +149,7 @@ def test_save_multiple_appointments(self):
end="19/09/2018 15:10:00"
)[0]
appointment_dicts = [appointment_dict_1, appointment_dict_2]
service.save_appointments(self.patient, appointment_dicts)
service._save_appointments(self.patient, appointment_dicts)
appointments = elcid_models.Appointment.objects.all()
self.assertEqual(appointments.count(), 2)
self.assertEqual(self.patient.appointment_set.count(), 2)
Expand All @@ -174,7 +174,7 @@ def test_save_multiple_appointments(self):
class GetOrCreateAppointmentsTestCase(AbstractServiceTestCase):
def test_old_appointment(self):
appointment_dict = self.get_api_response()[0]
appointment, created = service.get_or_create_appointment(
appointment, created = service._get_or_create_appointment(
self.patient, appointment_dict
)
self.assertEqual(appointment.patient, self.patient)
Expand All @@ -183,17 +183,17 @@ def test_old_appointment(self):
def test_new_appointment(self):
appointment_dict = self.get_api_response()[0]
self.get_appointment()
_, created = service.get_or_create_appointment(
_, created = service._get_or_create_appointment(
self.patient, appointment_dict
)
self.assertFalse(created)


@mock.patch('intrahospital_api.services.appointments.service.load_patient')
@mock.patch('intrahospital_api.services.appointments.service._load_patient')
class LoadPatientsTestCase(AbstractServiceTestCase):
def test_load_patient_tb(self, load_patient):
load_patient.return_value = True
result = service.load_patients()
result = service._load_patients()
load_patient.assert_called_once_with(self.patient)
self.assertEqual(
result, 1
Expand All @@ -204,7 +204,7 @@ def test_load_patient_not_tb(self, load_patient):
self.patient.episode_set.update(
category_name="Infection Srvice"
)
result = service.load_patients()
result = service._load_patients()
self.assertFalse(load_patient.called)
self.assertEqual(
result, 0
Expand All @@ -216,8 +216,7 @@ def test_load_patients_multiple(self, load_patient):

# 4 because another patient is created by the test setup
load_patient.side_effect = [1, 3, 2, 0]
result = service.load_patients()
result = service._load_patients()
self.assertEqual(
result, sum([1, 3, 2, 0])
)

0 comments on commit 4a60038

Please sign in to comment.