From b85f75add6ddd5523f49ccade87c562c33e60534 Mon Sep 17 00:00:00 2001 From: Ashesh3 <3626859+Ashesh3@users.noreply.github.com> Date: Wed, 15 Nov 2023 18:37:34 +0530 Subject: [PATCH] add tests --- care/facility/tests/test_patient_api.py | 95 +++++++++++++++++++++++++ care/utils/tests/test_utils.py | 2 - 2 files changed, 95 insertions(+), 2 deletions(-) diff --git a/care/facility/tests/test_patient_api.py b/care/facility/tests/test_patient_api.py index 793308dcb4..493a43f24b 100644 --- a/care/facility/tests/test_patient_api.py +++ b/care/facility/tests/test_patient_api.py @@ -244,3 +244,98 @@ def test_filter_by_location(self): self.assertEqual( response.data["results"][0]["id"], str(self.patient.external_id) ) + + +class PatientTransferTestCase(TestUtils, APITestCase): + @classmethod + def setUpTestData(cls): + cls.state = cls.create_state() + cls.district = cls.create_district(cls.state) + cls.local_body = cls.create_local_body(cls.district) + cls.super_user = cls.create_super_user("su", cls.district) + cls.facility = cls.create_facility(cls.super_user, cls.district, cls.local_body) + cls.destination_facility = cls.create_facility( + cls.super_user, cls.district, cls.local_body, name="Facility 2" + ) + cls.location = cls.create_asset_location(cls.facility) + cls.user = cls.create_user( + "doctor1", cls.district, home_facility=cls.facility, user_type=15 + ) + cls.patient = cls.create_patient(cls.district, cls.facility) + cls.consultation = cls.create_consultation( + patient_no="IP5678", + patient=cls.patient, + facility=cls.facility, + created_by=cls.user, + suggestion="A", + admission_date=now(), + discharge_date=None, # Patient is currently admitted + discharge_reason=None, + ) + cls.bed = cls.create_bed(cls.facility, cls.location) + cls.consultation_bed = cls.create_consultation_bed(cls.consultation, cls.bed) + cls.consultation.current_bed = cls.consultation_bed + cls.consultation.save() + cls.patient.last_consultation = cls.consultation + cls.patient.save() + + def test_patient_transfer(self): + self.client.force_authenticate(user=self.super_user) + response = self.client.post( + f"/api/v1/patient/{self.patient.external_id}/transfer/", + { + "date_of_birth": "1992-04-01", + "facility": self.destination_facility.external_id, + }, + ) + self.assertEqual(response.status_code, status.HTTP_200_OK) + + # Refresh patient data + self.patient.refresh_from_db() + self.consultation.refresh_from_db() + + # Assert the patient's facility has been updated + self.assertEqual(self.patient.facility, self.destination_facility) + + # Assert the consultation discharge reason and date are set correctly + self.assertEqual(self.consultation.discharge_reason, "REF") + self.assertIsNotNone(self.consultation.discharge_date) + + def test_transfer_with_active_consultation_same_facility(self): + # Set the patient's facility to allow transfers + self.patient.allow_transfer = True + self.patient.save() + + # Ensure transfer fails if the patient has an active consultation + self.client.force_authenticate(user=self.super_user) + response = self.client.post( + f"/api/v1/patient/{self.patient.external_id}/transfer/", + { + "date_of_birth": "1992-04-01", + "facility": self.facility.external_id, + }, + ) + self.assertEqual(response.status_code, status.HTTP_406_NOT_ACCEPTABLE) + self.assertEqual( + response.data["Patient"], + "Patient transfer cannot be completed because the patient has an active consultation in the same facility", + ) + + def test_transfer_disallowed_by_facility(self): + # Set the patient's facility to disallow transfers + self.patient.allow_transfer = False + self.patient.save() + + self.client.force_authenticate(user=self.super_user) + response = self.client.post( + f"/api/v1/patient/{self.patient.external_id}/transfer/", + { + "date_of_birth": "1992-04-01", + "facility": self.destination_facility.external_id, + }, + ) + self.assertEqual(response.status_code, status.HTTP_406_NOT_ACCEPTABLE) + self.assertEqual( + response.data["Patient"], + "Patient transfer cannot be completed because the source facility does not permit it", + ) diff --git a/care/utils/tests/test_utils.py b/care/utils/tests/test_utils.py index cd95e02299..1f6ceff22e 100644 --- a/care/utils/tests/test_utils.py +++ b/care/utils/tests/test_utils.py @@ -78,8 +78,6 @@ class TestUtils: Base class for tests, handles most of the test setup and tools for setting up data """ - maxDiff = None - def setUp(self) -> None: self.client.force_login(self.user)