From a16e1f1fc9b954ec9d070d062af32cc57a6e88eb Mon Sep 17 00:00:00 2001 From: Luis Aleixo Date: Fri, 29 Jul 2022 09:50:15 +0200 Subject: [PATCH] Added validation for length of breaks <= length of activity --- cara/apps/calculator/model_generator.py | 32 ++++++++++------ .../apps/calculator/test_model_generator.py | 37 +++++++++++++++---- 2 files changed, 49 insertions(+), 20 deletions(-) diff --git a/cara/apps/calculator/model_generator.py b/cara/apps/calculator/model_generator.py index 43779f26..89366b04 100644 --- a/cara/apps/calculator/model_generator.py +++ b/cara/apps/calculator/model_generator.py @@ -205,19 +205,27 @@ class FormData: # 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." - ) + populations = ['exposed', 'infected'] if self.infected_dont_have_breaks_with_exposed else ['exposed'] + for population in populations: + + if (getattr(self, f'{population}_lunch_option') and ( + not validate_lunch(getattr(self, f'{population}_start'), getattr(self, f'{population}_finish'), getattr(self, f'{population}_lunch_start')) or + not validate_lunch(getattr(self, f'{population}_start'), getattr(self, f'{population}_finish'), getattr(self, f'{population}_lunch_finish')))): + raise ValueError( + f"{population} lunch break must be within presence times." + ) + + # Length of breaks < length of activity + lunch_mins, coffee_mins = 0, 0 + if getattr(self, f'{population}_lunch_option'): + lunch_mins = getattr(self, f'{population}_lunch_finish') - getattr(self, f'{population}_lunch_start') + if getattr(self, f'{population}_coffee_break_option') != 'coffee_break_0': + coffee_mins = COFFEE_OPTIONS_INT[getattr(self, f'{population}_coffee_break_option')] * getattr(self, f'{population}_coffee_duration') + if (lunch_mins + coffee_mins) >= (getattr(self, f'{population}_finish') - getattr(self, f'{population}_start')) : + raise ValueError( + f"Length of breaks >= Length of {population} presence." + ) validation_tuples = [('activity_type', ACTIVITY_TYPES), ('exposed_coffee_break_option', COFFEE_OPTIONS_INT), diff --git a/cara/tests/apps/calculator/test_model_generator.py b/cara/tests/apps/calculator/test_model_generator.py index fa4ab587..f925497e 100644 --- a/cara/tests/apps/calculator/test_model_generator.py +++ b/cara/tests/apps/calculator/test_model_generator.py @@ -257,53 +257,74 @@ def test_exposed_present_lunch_end_before_beginning(baseline_form: model_generat 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.'): + 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.'): + 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.'): + 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.'): + 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.'): + 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.'): + 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.'): + 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.'): + with pytest.raises(ValueError, match='infected lunch break must be within presence times.'): + baseline_form.validate() + + +def test_exposed_breaks_length(baseline_form: model_generator.FormData): + baseline_form.exposed_coffee_break_option = 'coffee_break_4' + baseline_form.exposed_coffee_duration = 30 + baseline_form.exposed_start = minutes_since_midnight(10 * 60) + baseline_form.exposed_finish = minutes_since_midnight(11 * 60) + baseline_form.exposed_lunch_option = False + with pytest.raises(ValueError, match='Length of breaks >= Length of exposed presence.'): + baseline_form.validate() + + +def test_infected_breaks_length(baseline_form: model_generator.FormData): + baseline_form.infected_start = minutes_since_midnight(9 * 60) + baseline_form.infected_finish = minutes_since_midnight(12 * 60) + baseline_form.infected_lunch_start = minutes_since_midnight(10 * 60) + baseline_form.infected_lunch_finish = minutes_since_midnight(11 * 60) + baseline_form.infected_coffee_break_option = 'coffee_break_4' + baseline_form.infected_coffee_duration = 30 + with pytest.raises(ValueError, match='Length of breaks >= Length of infected presence.'): baseline_form.validate()