From d2fa7667bbfcdd66622033eb7fdbd5498520438b Mon Sep 17 00:00:00 2001 From: Suyash Singh Date: Fri, 7 Jul 2023 13:16:17 +0530 Subject: [PATCH] add home_facility to serializer and tests for patient notes --- care/facility/tests/test_patient_api.py | 167 ++++++++++++++++++++++++ care/users/api/serializers/user.py | 1 + care/utils/tests/test_base.py | 12 ++ 3 files changed, 180 insertions(+) diff --git a/care/facility/tests/test_patient_api.py b/care/facility/tests/test_patient_api.py index e69de29bb2..86d96ade62 100644 --- a/care/facility/tests/test_patient_api.py +++ b/care/facility/tests/test_patient_api.py @@ -0,0 +1,167 @@ +from enum import Enum + +from rest_framework import status +from rest_framework.test import APIRequestFactory, APITestCase +from rest_framework_simplejwt.tokens import RefreshToken + +from care.facility.tests.mixins import TestClassMixin +from care.utils.tests.test_base import TestBase + + +class ExpectedPatientNoteKeys(Enum): + NOTE = "note" + FACILITY = "facility" + CREATED_BY_OBJECT = "created_by_object" + CREATED_DATE = "created_date" + + +class ExpectedFacilityKeys(Enum): + ID = "id" + NAME = "name" + LOCAL_BODY = "local_body" + DISTRICT = "district" + STATE = "state" + WARD_OBJECT = "ward_object" + LOCAL_BODY_OBJECT = "local_body_object" + DISTRICT_OBJECT = "district_object" + STATE_OBJECT = "state_object" + FACILITY_TYPE = "facility_type" + READ_COVER_IMAGE_URL = "read_cover_image_url" + FEATURES = "features" + PATIENT_COUNT = "patient_count" + BED_COUNT = "bed_count" + + +class ExpectedWardObjectKeys(Enum): + ID = "id" + NAME = "name" + NUMBER = "number" + LOCAL_BODY = "local_body" + + +class ExpectedLocalBodyObjectKeys(Enum): + ID = "id" + NAME = "name" + BODY_TYPE = "body_type" + LOCALBODY_CODE = "localbody_code" + DISTRICT = "district" + + +class ExpectedDistrictObjectKeys(Enum): + ID = "id" + NAME = "name" + STATE = "state" + + +class ExpectedStateObjectKeys(Enum): + ID = "id" + NAME = "name" + + +class ExpectedFacilityTypeKeys(Enum): + ID = "id" + NAME = "name" + + +class ExpectedCreatedByObjectKeys(Enum): + ID = "id" + FIRST_NAME = "first_name" + USERNAME = "username" + EMAIL = "email" + LAST_NAME = "last_name" + USER_TYPE = "user_type" + LAST_LOGIN = "last_login" + HOME_FACILITY = "home_facility" + + +class PatientNotesTestCase(TestBase, TestClassMixin, APITestCase): + asset_id = None + + def setUp(self): + self.factory = APIRequestFactory() + state = self.create_state() + district = self.create_district(state=state) + + # Create users and facility + self.user = self.create_user(district=district, username="test user") + facility = self.create_facility(district=district, user=self.user) + + self.patient = self.create_patient(district=district.id) + + self.patient_note = self.create_patient_note( + patient=self.patient, facility=facility + ) + + refresh_token = RefreshToken.for_user(self.user) + self.client.credentials( + HTTP_AUTHORIZATION=f"Bearer {refresh_token.access_token}" + ) + + def test_patient_notes(self): + patientId = self.patient.external_id + response = self.client.get(f"/api/v1/patient/{patientId}/notes/") + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertIsInstance(response.json()["results"], list) + + # Ensure only necessary data is being sent and no extra data + + data = response.json()["results"][0] + + self.assertCountEqual( + data.keys(), [item.value for item in ExpectedPatientNoteKeys] + ) + + facility_content = data["facility"] + + if facility_content is not None: + self.assertCountEqual( + facility_content.keys(), [item.value for item in ExpectedFacilityKeys] + ) + + ward_object_content = facility_content["ward_object"] + + if ward_object_content is not None: + self.assertCountEqual( + ward_object_content.keys(), + [item.value for item in ExpectedWardObjectKeys], + ) + + local_body_object_content = facility_content["local_body_object"] + + if local_body_object_content is not None: + self.assertCountEqual( + local_body_object_content.keys(), + [item.value for item in ExpectedLocalBodyObjectKeys], + ) + + district_object_content = facility_content["district_object"] + + if district_object_content is not None: + self.assertCountEqual( + district_object_content.keys(), + [item.value for item in ExpectedDistrictObjectKeys], + ) + + state_object_content = facility_content["state_object"] + + if state_object_content is not None: + self.assertCountEqual( + state_object_content.keys(), + [item.value for item in ExpectedStateObjectKeys], + ) + + facility_type_content = facility_content["facility_type"] + + if facility_type_content is not None: + self.assertCountEqual( + facility_type_content.keys(), + [item.value for item in ExpectedFacilityTypeKeys], + ) + + created_by_object_content = data["created_by_object"] + + if created_by_object_content is not None: + self.assertCountEqual( + created_by_object_content.keys(), + [item.value for item in ExpectedCreatedByObjectKeys], + ) diff --git a/care/users/api/serializers/user.py b/care/users/api/serializers/user.py index 4e637c003b..c1fb589171 100644 --- a/care/users/api/serializers/user.py +++ b/care/users/api/serializers/user.py @@ -344,6 +344,7 @@ class Meta: "last_name", "user_type", "last_login", + "home_facility", ) diff --git a/care/utils/tests/test_base.py b/care/utils/tests/test_base.py index 20d24ecf98..3ed212c1b2 100644 --- a/care/utils/tests/test_base.py +++ b/care/utils/tests/test_base.py @@ -19,6 +19,7 @@ Facility, LocalBody, PatientConsultation, + PatientNotes, PatientRegistration, User, ) @@ -436,3 +437,14 @@ def create_consultation( ) data.update(kwargs) return PatientConsultation.objects.create(**data) + + def create_patient_note( + self, patient=None, facility=None, note="Patient is doing find", **kwargs + ): + data = { + "patient": patient or self.patient, + "facility": facility or self.facility, + "note": note, + } + data.update(kwargs) + return PatientNotes.objects.create(**data)