Skip to content

Commit

Permalink
Remove old bed name field and fix bed name validator (#1894)
Browse files Browse the repository at this point in the history
* remove old bed name field

* fix bed create name validation

* cleanup
  • Loading branch information
sainak authored Feb 15, 2024
1 parent 3b91fd1 commit 63ff210
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
16 changes: 16 additions & 0 deletions care/facility/migrations/0414_remove_bed_old_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Generated by Django 4.2.10 on 2024-02-14 11:41

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("facility", "0413_eventtype_patientconsultationevent_and_more"),
]

operations = [
migrations.RemoveField(
model_name="bed",
name="old_name",
),
]
4 changes: 2 additions & 2 deletions care/facility/models/bed.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
However this is an addon feature and is not required for the regular patient flow,
Leaving scope to build rooms and wards to being even more organization.
"""

from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import JSONField
Expand All @@ -17,7 +18,6 @@

class Bed(BaseModel):
name = models.CharField(max_length=1024)
old_name = models.CharField(max_length=1024, null=True, blank=True)
description = models.TextField(default="", blank=True)
bed_type = models.IntegerField(
choices=BedTypeChoices, default=BedType.REGULAR.value
Expand Down Expand Up @@ -49,7 +49,7 @@ def __str__(self):

def validate(self) -> None:
if (
Bed.objects.filter(location=self.location, name=self.name)
Bed.objects.filter(location=self.location, name__iexact=self.name)
.exclude(pk=self.pk)
.exists()
):
Expand Down
30 changes: 30 additions & 0 deletions care/facility/tests/test_bed_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,36 @@ def test_create(self):
sample_data["number_of_beds"],
)

def test_create_with_same_name(self):
sample_data = {
"bed_type": "REGULAR",
"description": "Testing creation of beds.",
"facility": self.facility.external_id,
"location": self.asset_location.external_id,
"name": "Test Bed",
"number_of_beds": 1,
}
response = self.client.post("/api/v1/bed/", sample_data, format="json")
self.assertIs(response.status_code, status.HTTP_201_CREATED)

response = self.client.post("/api/v1/bed/", sample_data, format="json")
self.assertIs(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
response.json()["name"],
["Bed with same name already exists in location."],
)

# validate case insensitive
sample_data["name"] = "test bed"
response = self.client.post("/api/v1/bed/", sample_data, format="json")
self.assertIs(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
response.json()["name"],
["Bed with same name already exists in location."],
)

self.assertEqual(Bed.objects.filter(facility=self.facility).count(), 1)


class MultipleBedTest(TestUtils, APITestCase):
@classmethod
Expand Down

0 comments on commit 63ff210

Please sign in to comment.