Server-side validation in lunch break times within the activity times

This commit is contained in:
Luis Aleixo 2022-07-28 16:37:49 +02:00
parent 1f439cf22f
commit 3baaa02ff8
2 changed files with 69 additions and 0 deletions

View file

@ -202,6 +202,23 @@ class FormData:
raise ValueError(
f"{start_name} must be less than {end_name}. Got {start} and {end}.")
# Validate lunch time within the activity times.
def validate_lunch(start, finish, time):
return start < time < finish
if (self.exposed_lunch_option and (
not validate_lunch(self.exposed_start, self.exposed_finish, self.exposed_lunch_start) or
not validate_lunch(self.exposed_start, self.exposed_finish, self.exposed_lunch_finish))):
raise ValueError(
"Exposed lunch break must be within presence times."
)
if (self.infected_dont_have_breaks_with_exposed and self.infected_lunch_option and (
not validate_lunch(self.infected_start, self.infected_finish, self.infected_lunch_start) or
not validate_lunch(self.infected_start, self.infected_finish, self.infected_lunch_finish))):
raise ValueError(
"Infected lunch break must be within presence times."
)
validation_tuples = [('activity_type', ACTIVITY_TYPES),
('exposed_coffee_break_option', COFFEE_OPTIONS_INT),
('infected_coffee_break_option', COFFEE_OPTIONS_INT),

View file

@ -255,6 +255,58 @@ def test_exposed_present_lunch_end_before_beginning(baseline_form: model_generat
baseline_form.validate()
def test_exposed_presence_lunch_start_before_begining(baseline_form: model_generator.FormData):
baseline_form.exposed_lunch_start = minutes_since_midnight(8 * 60)
with pytest.raises(ValueError, match='Exposed lunch break must be within presence times.'):
baseline_form.validate()
def test_exposed_presence_lunch_start_after_finishing(baseline_form: model_generator.FormData):
baseline_form.exposed_lunch_start = minutes_since_midnight(19 * 60)
baseline_form.exposed_lunch_finish = minutes_since_midnight(20 * 60)
with pytest.raises(ValueError, match='Exposed lunch break must be within presence times.'):
baseline_form.validate()
def test_exposed_presence_lunch_finish_before_begining(baseline_form: model_generator.FormData):
baseline_form.exposed_lunch_start = minutes_since_midnight(7 * 60)
baseline_form.exposed_lunch_finish = minutes_since_midnight(8 * 60)
with pytest.raises(ValueError, match='Exposed lunch break must be within presence times.'):
baseline_form.validate()
def test_exposed_presence_lunch_finish_after_finishing(baseline_form: model_generator.FormData):
baseline_form.exposed_lunch_finish = minutes_since_midnight(19 * 60)
with pytest.raises(ValueError, match='Exposed lunch break must be within presence times.'):
baseline_form.validate()
def test_infected_presence_lunch_start_before_begining(baseline_form: model_generator.FormData):
baseline_form.infected_lunch_start = minutes_since_midnight(8 * 60)
with pytest.raises(ValueError, match='Infected lunch break must be within presence times.'):
baseline_form.validate()
def test_infected_presence_lunch_start_after_finishing(baseline_form: model_generator.FormData):
baseline_form.infected_lunch_start = minutes_since_midnight(19 * 60)
baseline_form.infected_lunch_finish = minutes_since_midnight(20 * 60)
with pytest.raises(ValueError, match='Infected lunch break must be within presence times.'):
baseline_form.validate()
def test_infected_presence_lunch_finish_before_begining(baseline_form: model_generator.FormData):
baseline_form.infected_lunch_start = minutes_since_midnight(7 * 60)
baseline_form.infected_lunch_finish = minutes_since_midnight(8 * 60)
with pytest.raises(ValueError, match='Infected lunch break must be within presence times.'):
baseline_form.validate()
def test_infected_presence_lunch_finish_after_finishing(baseline_form: model_generator.FormData):
baseline_form.infected_lunch_finish = minutes_since_midnight(19 * 60)
with pytest.raises(ValueError, match='Infected lunch break must be within presence times.'):
baseline_form.validate()
@pytest.fixture
def coffee_break_between_1045_and_1115(baseline_form: model_generator.FormData):
baseline_form.exposed_coffee_break_option = 'coffee_break_1'