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

Add home_facility to UserBaseMinimumSerializer #1441

Merged
merged 3 commits into from
Jul 9, 2023
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
167 changes: 167 additions & 0 deletions care/facility/tests/test_patient_api.py
Original file line number Diff line number Diff line change
@@ -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],
)
1 change: 1 addition & 0 deletions care/users/api/serializers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ class Meta:
"last_name",
"user_type",
"last_login",
"home_facility",
)


Expand Down
12 changes: 12 additions & 0 deletions care/utils/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Facility,
LocalBody,
PatientConsultation,
PatientNotes,
PatientRegistration,
User,
)
Expand Down Expand Up @@ -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)