Skip to content

Commit

Permalink
add tests for the list and retrieve endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
GeekGawd committed Jul 11, 2023
1 parent c1405de commit 2cdf17c
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
92 changes: 92 additions & 0 deletions care/facility/tests/test_bed_api.py
Original file line number Diff line number Diff line change
@@ -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]
)
23 changes: 23 additions & 0 deletions care/utils/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
COVID_CATEGORY_CHOICES,
DISEASE_CHOICES_MAP,
SYMPTOM_CHOICES,
AssetLocation,
Bed,
Disease,
DiseaseStatusEnum,
Facility,
Expand Down Expand Up @@ -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)

0 comments on commit 2cdf17c

Please sign in to comment.