From 0683faa66c1b962045e2bc683969054db4fbcec4 Mon Sep 17 00:00:00 2001 From: Aakash Singh Date: Thu, 19 Sep 2024 20:45:26 +0530 Subject: [PATCH] update unique_bed_name_per_location to ignore deleted beds (#2432) update unique_bed_name_per_location to ignore deleted beds (#2432) --------- Co-authored-by: vigneshhari --- ...d_unique_bed_name_per_location_and_more.py | 27 +++++++++++++++++++ care/facility/models/bed.py | 1 + care/facility/tests/test_bed_create.py | 21 +++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 care/facility/migrations/0459_remove_bed_unique_bed_name_per_location_and_more.py diff --git a/care/facility/migrations/0459_remove_bed_unique_bed_name_per_location_and_more.py b/care/facility/migrations/0459_remove_bed_unique_bed_name_per_location_and_more.py new file mode 100644 index 0000000000..9ed655e03b --- /dev/null +++ b/care/facility/migrations/0459_remove_bed_unique_bed_name_per_location_and_more.py @@ -0,0 +1,27 @@ +# Generated by Django 4.2.10 on 2024-09-19 14:44 + +import django.db.models.functions.text +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("facility", "0458_facilityflag_facilityflag_unique_facility_flag"), + ] + + operations = [ + migrations.RemoveConstraint( + model_name="bed", + name="unique_bed_name_per_location", + ), + migrations.AddConstraint( + model_name="bed", + constraint=models.UniqueConstraint( + django.db.models.functions.text.Lower("name"), + models.F("location"), + condition=models.Q(("deleted", False)), + name="unique_bed_name_per_location", + ), + ), + ] diff --git a/care/facility/models/bed.py b/care/facility/models/bed.py index 1f6ae30777..a06db2729c 100644 --- a/care/facility/models/bed.py +++ b/care/facility/models/bed.py @@ -37,6 +37,7 @@ class Meta: models.functions.Lower("name"), "location", name="unique_bed_name_per_location", + condition=models.Q(deleted=False), ) ] diff --git a/care/facility/tests/test_bed_create.py b/care/facility/tests/test_bed_create.py index 26a18a69be..5f0e160c5c 100644 --- a/care/facility/tests/test_bed_create.py +++ b/care/facility/tests/test_bed_create.py @@ -63,6 +63,27 @@ def test_create_with_same_name(self): self.assertEqual(Bed.objects.filter(facility=self.facility).count(), 1) + def test_create_with_name_previously_deleted(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) + + bed = Bed.objects.get(name="Test Bed") + bed.deleted = True + bed.save() + + 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(), 1) + class MultipleBedTest(TestUtils, APITestCase): @classmethod