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

added tests for beds endpoints #2104

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
146 changes: 136 additions & 10 deletions care/facility/tests/test_bed_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from rest_framework.test import APITestCase

from care.facility.models import Bed
from care.users.models import User
from care.utils.tests.test_utils import TestUtils


Expand All @@ -13,26 +14,151 @@ def setUpTestData(cls) -> None:
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.facility2 = cls.create_facility(
cls.super_user, cls.district, cls.local_body
)
cls.asset_location = cls.create_asset_location(cls.facility)
cls.asset_location2 = cls.create_asset_location(cls.facility2)
cls.user = cls.create_user("staff", cls.district, home_facility=cls.facility)
cls.patient = cls.create_patient(
cls.district, cls.facility, local_body=cls.local_body
)

def setUp(self) -> None:
super().setUp()
self.bed1 = Bed.objects.create(
name="bed1", location=self.asset_location, facility=self.facility
cls.bed1 = Bed.objects.create(
name="bed1", location=cls.asset_location, facility=cls.facility
)
self.bed2 = Bed.objects.create(
name="bed2", location=self.asset_location, facility=self.facility
cls.bed2 = Bed.objects.create(
name="bed2", location=cls.asset_location, facility=cls.facility, bed_type=1
)
self.bed3 = Bed.objects.create(
name="bed3", location=self.asset_location, facility=self.facility
cls.bed3 = Bed.objects.create(
name="bed3", location=cls.asset_location2, facility=cls.facility2
Comment on lines -23 to +33
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moving this to setUpTestData, will create the objects only once, this could cause issues if you expect the objects to not change

)

def test_list_beds(self):
# includes 3 queries for auth and 1 for pagination count
with self.assertNumQueries(5):
response = self.client.get("/api/v1/bed/")
self.assertEqual(response.status_code, status.HTTP_200_OK)

# test list beds accessible to user
response = self.client.get("/api/v1/bed/")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["count"], 2)

self.client.force_login(self.super_user)

# test list beds accessible to superuser (all beds)
response = self.client.get("/api/v1/bed/")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["count"], 3)

response = self.client.get("/api/v1/bed/", {"bed_type": "ISOLATION"})
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["results"][0]["name"], self.bed2.name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's also check the count here

self.assertEqual(response.data["count"], 1)

response = self.client.get(
"/api/v1/bed/",
{"facility": self.facility2.external_id},
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["results"][0]["name"], self.bed3.name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's also check the count here

self.assertEqual(response.data["count"], 1)

response = self.client.get(
"/api/v1/bed/",
{"location": self.asset_location2.external_id},
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["count"], 1)
self.assertEqual(response.data["results"][0]["name"], self.bed3.name)

self.client.logout()

def test_create_beds(self):
sample_data = {
"name": "Sample Beds",
"bed_type": 2,
"location": self.asset_location.external_id,
"facility": self.facility.external_id,
"number_of_beds": 10,
"description": "This is a sample bed description",
}
response = self.client.post("/api/v1/bed/", sample_data, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Bed.objects.filter(bed_type=2).count(), 10)

# without location
sample_data.update({"location": None})
response = self.client.post("/api/v1/bed/", sample_data, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data["location"][0], "This field may not be null.")

# without facility
sample_data.update({"facility": None})
response = self.client.post("/api/v1/bed/", sample_data, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data["facility"][0], "This field may not be null.")

# Test - if beds > 100
sample_data.update({"number_of_beds": 200})
response = self.client.post("/api/v1/bed/", sample_data, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
response.data["number_of_beds"][0],
"Cannot create more than 100 beds at once.",
)

def test_retrieve_bed(self):
response = self.client.get(f"/api/v1/bed/{self.bed1.external_id}/")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["name"], "bed1")

def test_update_bed(self):
updated_data = {
"name": "Updated Bed Name",
"bed_type": 3,
"location": self.asset_location.external_id,
"facility": self.facility.external_id,
}
response = self.client.put(
f"/api/v1/bed/{self.bed2.external_id}/", updated_data, format="json"
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.bed2.refresh_from_db()
self.assertEqual(self.bed2.name, updated_data["name"])
self.assertEqual(self.bed2.bed_type, updated_data["bed_type"])

def test_patch_update_bed(self):
self.client.force_login(self.super_user)

# we always need to send location and facility since serializer is written that way
partial_data = {
"description": "Updated Bed Description",
"location": self.asset_location.external_id,
"facility": self.facility.external_id,
}
response = self.client.patch(
f"/api/v1/bed/{self.bed3.external_id}/", partial_data, format="json"
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.bed3.refresh_from_db()
self.assertEqual(self.bed3.description, partial_data["description"])
self.client.logout()

def test_delete_bed_without_permission(self):
response = self.client.delete(f"/api/v1/bed/{self.bed1.external_id}/")
self.assertFalse(self.user.user_type == User.TYPE_VALUE_MAP["DistrictLabAdmin"])
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assertTrue(Bed.objects.filter(id=self.bed1.id).exists())

def test_delete_bed(self):
user2 = self.create_user(
"Sample User",
self.district,
home_facility=self.facility,
user_type=User.TYPE_VALUE_MAP["DistrictLabAdmin"],
)
self.client.force_login(user2)
self.assertTrue(user2.user_type == User.TYPE_VALUE_MAP["DistrictLabAdmin"])
response = self.client.delete(f"/api/v1/bed/{self.bed1.external_id}/")
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertFalse(Bed.objects.filter(id=self.bed1.id).exists())