Skip to content

Commit

Permalink
add test case for more than allowed beds
Browse files Browse the repository at this point in the history
Signed-off-by: Aakash Singh <[email protected]>
  • Loading branch information
sainak committed Jul 26, 2023
1 parent 1cee771 commit f589193
Showing 1 changed file with 30 additions and 35 deletions.
65 changes: 30 additions & 35 deletions care/facility/tests/test_bed_create.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,22 @@
from rest_framework import status
from rest_framework.test import APIRequestFactory, APITestCase
from rest_framework.test import APITransactionTestCase

from care.facility.api.viewsets.bed import BedViewSet
from care.facility.models import AssetLocation, Bed
from care.facility.tests.mixins import TestClassMixin
from care.utils.tests.test_base import TestBase


class SingleBedTest(TestBase, TestClassMixin, APITestCase):
@classmethod
def setUpClass(cls):
cls.factory = APIRequestFactory()
state = cls.create_state()
district = cls.create_district(state=state)
cls.user = cls.create_user(district=district, username="test user")
cls.facility = cls.create_facility(district=district, user=cls.user)
cls.asset_location: AssetLocation = AssetLocation.objects.create(
name="asset location", location_type=1, facility=cls.facility
class SingleBedTest(TestBase):
def setUp(self) -> None:
super().setUp()
self.asset_location: AssetLocation = AssetLocation.objects.create(
name="asset location", location_type=1, facility=self.facility
)

def tearDown(self) -> None:
Bed._default_manager.filter(facility=self.facility).delete()

@classmethod
def tearDownClass(cls):
cls.facility.delete()
cls.user.delete()
cls.asset_location.delete()
AssetLocation._default_manager.filter(id=self.asset_location.id).delete()

def test_create(self):
user = self.user
sample_data = {
"bed_type": "REGULAR",
"description": "Testing creation of beds.",
Expand All @@ -38,29 +25,26 @@ def test_create(self):
"name": "Test Bed",
"number_of_beds": 1,
}
response = self.new_request(
("/api/v1/bed/", sample_data, "json"), {"post": "create"}, BedViewSet, user
)
response = self.client.post("/api/v1/bed/", sample_data, format="json")
self.assertIs(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(
Bed.objects.filter(facility=self.facility).count(),
sample_data["number_of_beds"],
)


class MultipleBedTest(TestBase, TestClassMixin, APITestCase):
def setUp(self):
self.factory = APIRequestFactory()
state = self.create_state()
district = self.create_district(state=state)
self.user = self.create_user(district=district, username="test user")
self.facility = self.create_facility(district=district, user=self.user)
self.asset_location = AssetLocation.objects.create(
class MultipleBedTest(APITransactionTestCase, TestBase):
def setUp(self) -> None:
super().setUp()
self.asset_location: AssetLocation = AssetLocation.objects.create(
name="asset location", location_type=1, facility=self.facility
)

def tearDown(self) -> None:
Bed._default_manager.filter(facility=self.facility).delete()
AssetLocation._default_manager.filter(id=self.asset_location.id).delete()

def test_create(self):
user = self.user
sample_data = {
"bed_type": "REGULAR",
"description": "Testing creation of beds.",
Expand All @@ -69,11 +53,22 @@ def test_create(self):
"name": "Test Bed",
"number_of_beds": 5,
}
response = self.new_request(
("/api/v1/bed/", sample_data, "json"), {"post": "create"}, BedViewSet, user
)
response = self.client.post("/api/v1/bed/", sample_data, format="json")
self.assertIs(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(
Bed.objects.filter(facility=self.facility).count(),
sample_data["number_of_beds"],
)

def test_create_with_more_than_allowed_value(self):
sample_data = {
"bed_type": "REGULAR",
"description": "Testing creation of beds.",
"facility": self.facility.external_id,
"location": self.asset_location.external_id,
"name": "Test 2 Bed",
"number_of_beds": 101,
}
response = self.client.post("/api/v1/bed/", sample_data, format="json")
self.assertIs(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(Bed.objects.filter(facility=self.facility).count(), 0)

0 comments on commit f589193

Please sign in to comment.