fixed bug related to specific breaks

This commit is contained in:
Luis Aleixo 2023-12-14 14:49:06 +01:00
parent 18456f4efa
commit 23c8959244
2 changed files with 11 additions and 10 deletions

View file

@ -227,15 +227,15 @@ class FormData:
else:
return self.exposed_coffee_break_times()
def generate_specific_break_times(self, population_breaks) -> models.BoundarySequence_t:
def generate_specific_break_times(self, breaks_dict: dict, target: str) -> models.BoundarySequence_t:
break_times = []
for n in population_breaks:
for n in breaks_dict[f'{target}_breaks']:
# Parse break times.
begin = time_string_to_minutes(n["start_time"])
end = time_string_to_minutes(n["finish_time"])
for time in [begin, end]:
# For a specific break, the infected and exposed presence is the same.
if not getattr(self, 'infected_start') < time < getattr(self, 'infected_finish'):
if not getattr(self, f'{target}_start') < time < getattr(self, f'{target}_finish'):
raise ValueError(f'All breaks should be within the simulation time. Got {time_minutes_to_string(time)}.')
break_times.append((begin, end))
@ -335,7 +335,7 @@ class FormData:
def infected_present_interval(self) -> models.Interval:
if self.specific_breaks != {}: # It means the breaks are specific and not predefined
breaks = self.generate_specific_break_times(self.specific_breaks['infected_breaks'])
breaks = self.generate_specific_break_times(breaks_dict=self.specific_breaks, target='exposed')
else:
breaks = self.infected_lunch_break_times() + self.infected_coffee_break_times()
return self.present_interval(
@ -351,7 +351,7 @@ class FormData:
def exposed_present_interval(self) -> models.Interval:
if self.specific_breaks != {}: # It means the breaks are specific and not predefined
breaks = self.generate_specific_break_times(self.specific_breaks['exposed_breaks'])
breaks = self.generate_specific_break_times(breaks_dict=self.specific_breaks, target='exposed')
else:
breaks = self.exposed_lunch_break_times() + self.exposed_coffee_break_times()
return self.present_interval(

View file

@ -39,15 +39,16 @@ def test_specific_population_break_data_structure(population_break_input, error,
@pytest.mark.parametrize(
["break_input", "error"],
[
[[{"start_time": "07:00", "finish_time": "11:00"}, ], "All breaks should be within the simulation time. Got 07:00."],
[[{"start_time": "17:00", "finish_time": "18:00"}, ], "All breaks should be within the simulation time. Got 18:00."],
[[{"start_time": "10:00", "finish_time": "11:00"}, {"start_time": "17:00", "finish_time": "20:00"}, ], "All breaks should be within the simulation time. Got 20:00."],
[[{"start_time": "08:00", "finish_time": "11:00"}, {"start_time": "14:00", "finish_time": "15:00"}, ], "All breaks should be within the simulation time. Got 08:00."],
[{'exposed_breaks': [{"start_time": "07:00", "finish_time": "11:00"}, ], 'infected_breaks': []}, "All breaks should be within the simulation time. Got 07:00."],
[{'exposed_breaks': [], 'infected_breaks': [{"start_time": "17:00", "finish_time": "18:00"}, ]}, "All breaks should be within the simulation time. Got 18:00."],
[{'exposed_breaks': [{"start_time": "10:00", "finish_time": "11:00"}, {"start_time": "17:00", "finish_time": "20:00"}, ], 'infected_breaks': []}, "All breaks should be within the simulation time. Got 20:00."],
[{'exposed_breaks': [], 'infected_breaks': [{"start_time": "08:00", "finish_time": "11:00"}, {"start_time": "14:00", "finish_time": "15:00"}, ]}, "All breaks should be within the simulation time. Got 08:00."],
]
)
def test_specific_break_time(break_input, error, baseline_form: model_generator.VirusFormData):
with pytest.raises(ValueError, match=error):
baseline_form.generate_specific_break_times(break_input)
baseline_form.generate_specific_break_times(breaks_dict=break_input, target='exposed')
baseline_form.generate_specific_break_times(breaks_dict=break_input, target='infected')
@pytest.mark.parametrize(