Skip to content

update unique_bed_name_per_location to ignore deleted beds #2432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

from django.db import migrations, models
import django.db.models.functions.text


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
Loading