diff --git a/care/facility/tests/test_bed_api.py b/care/facility/tests/test_bed_api.py new file mode 100644 index 0000000000..545631a8a1 --- /dev/null +++ b/care/facility/tests/test_bed_api.py @@ -0,0 +1,92 @@ +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 ExpectedBedRetrieveKeys(Enum): + ID = "id" + BED_TYPE = "bed_type" + LOCATION_OBJECT = "location_object" + IS_OCCUPIED = "is_occupied" + CREATED_DATE = "created_date" + MODIFIED_DATE = "modified_date" + NAME = "name" + DESCRIPTION = "description" + META = "meta" + + +class ExpectedLocationObjectKeys(Enum): + ID = "id" + FACILITY = "facility" + CREATED_DATE = "created_date" + MODIFIED_DATE = "modified_date" + NAME = "name" + DESCRIPTION = "description" + LOCATION_TYPE = "location_type" + + +class ExpectedFacilityKeys(Enum): + ID = "id" + NAME = "name" + + +class ExpectedBedListKeys(Enum): + ID = "id" + BED_TYPE = "bed_type" + DESCRIPTION = "description" + NAME = "name" + IS_OCCUPIED = "is_occupied" + + +class BedTestCase(TestBase, TestClassMixin, APITestCase): + def setUp(self): + self.factory = APIRequestFactory() + self.bed = self.create_bed() + + refresh_token = RefreshToken.for_user(self.user) + self.client.credentials( + HTTP_AUTHORIZATION=f"Bearer {refresh_token.access_token}" + ) + + def test_retrieve_bed(self): + bedId = self.bed.external_id + response = self.client.get(f"/api/v1/bed/{bedId}/") + self.assertEqual(response.status_code, status.HTTP_200_OK) + data = response.json() + + self.assertCountEqual( + data.keys(), [item.value for item in ExpectedBedRetrieveKeys] + ) + + location_object_content = data["location_object"] + + if location_object_content is not None: + self.assertCountEqual( + location_object_content.keys(), + [item.value for item in ExpectedLocationObjectKeys], + ) + + facility_content = location_object_content["facility"] + + if facility_content is not None: + self.assertCountEqual( + facility_content.keys(), [item.value for item in ExpectedFacilityKeys] + ) + + def test_bed_list(self): + response = self.client.get("/api/v1/bed/") + self.assertEqual(response.status_code, status.HTTP_200_OK) + data = response.json() + + results = data["results"] + self.assertIsInstance(results, list) + + for bed in results: + self.assertCountEqual( + bed.keys(), [item.value for item in ExpectedBedListKeys] + ) diff --git a/care/utils/tests/test_base.py b/care/utils/tests/test_base.py index 3ed212c1b2..d425ee9b0d 100644 --- a/care/utils/tests/test_base.py +++ b/care/utils/tests/test_base.py @@ -14,6 +14,8 @@ COVID_CATEGORY_CHOICES, DISEASE_CHOICES_MAP, SYMPTOM_CHOICES, + AssetLocation, + Bed, Disease, DiseaseStatusEnum, Facility, @@ -448,3 +450,24 @@ def create_patient_note( } data.update(kwargs) return PatientNotes.objects.create(**data) + + def create_asset_location(self): + return AssetLocation.objects.create( + name="Location 1", + description="Location 1", + location_type=1, + facility=self.facility, + ) + + def create_bed(self, facility=None, room_type=1, room_number="1", **kwargs): + asset_location = self.create_asset_location() + data = { + "name": "Bed 1", + "description": "Bed 1", + "bed_type": room_type, + "facility": facility or self.facility, + "meta": {}, + "location": asset_location, + } + data.update(kwargs) + return Bed.objects.create(**data)