Skip to content

Commit

Permalink
update unique_bed_name_per_location to ignore deleted beds (#2432)
Browse files Browse the repository at this point in the history
update unique_bed_name_per_location to ignore deleted beds (#2432)

---------

Co-authored-by: vigneshhari <vichuhari100@gmail.com>
  • Loading branch information
sainak and vigneshhari authored Sep 19, 2024
1 parent d36b5d9 commit 0683faa
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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",
),
),
]
1 change: 1 addition & 0 deletions care/facility/models/bed.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Meta:
models.functions.Lower("name"),
"location",
name="unique_bed_name_per_location",
condition=models.Q(deleted=False),
)
]

Expand Down
21 changes: 21 additions & 0 deletions care/facility/tests/test_bed_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0683faa

Please sign in to comment.