From f58919374990ba3a1424207c02a1ea44dd299f92 Mon Sep 17 00:00:00 2001 From: Aakash Singh Date: Wed, 26 Jul 2023 13:40:35 +0530 Subject: [PATCH] add test case for more than allowed beds Signed-off-by: Aakash Singh --- care/facility/tests/test_bed_create.py | 65 ++++++++++++-------------- 1 file changed, 30 insertions(+), 35 deletions(-) diff --git a/care/facility/tests/test_bed_create.py b/care/facility/tests/test_bed_create.py index 90b1447bba..cc178625fe 100644 --- a/care/facility/tests/test_bed_create.py +++ b/care/facility/tests/test_bed_create.py @@ -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.", @@ -38,9 +25,7 @@ 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(), @@ -48,19 +33,18 @@ def test_create(self): ) -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.", @@ -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)