Skip to content

Commit e909aac

Browse files
add ClinicSlot clean function
Because clinic and clinic slot start times are both datetime, it is currently possible to have a clinic slot that starts on a different day to the clinic. This adds a validation function which checks the clinic slot date and the clinic start_date are the same
1 parent 30a6089 commit e909aac

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

manage_breast_screening/clinics/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from django.conf import settings
66
from django.contrib.postgres.fields import ArrayField
7+
from django.core.exceptions import ValidationError
78
from django.core.validators import MinLengthValidator
89
from django.db import models
910
from django.db.models import OuterRef, Subquery
@@ -186,6 +187,12 @@ def __str__(self):
186187
self.clinic.setting.name + " " + self.starts_at.strftime("%Y-%m-%d %H:%M")
187188
)
188189

190+
def clean(self):
191+
if self.starts_at.date() != self.clinic.starts_at.date():
192+
raise ValidationError(
193+
"Clinic slot must start on the same date as the clinic"
194+
)
195+
189196

190197
class ClinicStatus(models.Model):
191198
SCHEDULED = "SCHEDULED"

manage_breast_screening/clinics/tests/test_models.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

44
import pytest
55
import time_machine
6+
from django.core.exceptions import ValidationError
67
from pytest_django.asserts import assertQuerySetEqual
78

89
from manage_breast_screening.auth.models import Role
910
from manage_breast_screening.clinics import models
1011

1112
from .factories import (
1213
ClinicFactory,
14+
ClinicSlotFactory,
1315
ProviderFactory,
1416
UserAssignmentFactory,
1517
UserFactory,
@@ -87,6 +89,18 @@ def test_completed_ordering_by_ends_at():
8789
assertQuerySetEqual(completed, [clinic1, clinic2, clinic3])
8890

8991

92+
@pytest.mark.django_db
93+
def test_clean_clinic_slots():
94+
clinic = ClinicFactory.build(starts_at=datetime(2024, 12, 29, 9, tzinfo=tz.utc))
95+
clinic_slot = ClinicSlotFactory.build(
96+
starts_at=datetime(2024, 12, 26, 9, tzinfo=tz.utc),
97+
duration_in_minutes=30,
98+
clinic=clinic,
99+
)
100+
with pytest.raises(ValidationError):
101+
clinic_slot.clean()
102+
103+
90104
class TestUserAssignment:
91105
def test_str(self):
92106
user = UserFactory.build(first_name="John", last_name="Doe")

0 commit comments

Comments
 (0)