From e9456e10e5fbfb9fcc14179658169f1eaa1e5c30 Mon Sep 17 00:00:00 2001 From: Phil Elson Date: Thu, 26 Aug 2021 12:20:09 +0200 Subject: [PATCH] Tidy up the month handling. --- cara/apps/calculator/model_generator.py | 22 ++++++++++++------- .../apps/calculator/test_model_generator.py | 6 +++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/cara/apps/calculator/model_generator.py b/cara/apps/calculator/model_generator.py index 69701a30..702f4930 100644 --- a/cara/apps/calculator/model_generator.py +++ b/cara/apps/calculator/model_generator.py @@ -105,9 +105,9 @@ class FormData: 'infected_lunch_start': '12:30', 'infected_people': _NO_DEFAULT, 'infected_start': '08:30', - 'location_latitude': 46.20833, - 'location_longitude': 6.14275, - 'location_name': 'Geneva', + 'location_latitude': _NO_DEFAULT, + 'location_longitude': _NO_DEFAULT, + 'location_name': _NO_DEFAULT, 'mask_type': 'Type I', 'mask_wearing_option': 'mask_off', 'mechanical_ventilation_type': 'not-applicable', @@ -205,7 +205,8 @@ class FormData: ('virus_type', VIRUS_TYPES), ('volume_type', VOLUME_TYPES), ('window_opening_regime', WINDOWS_OPENING_REGIMES), - ('window_type', WINDOWS_TYPES)] + ('window_type', WINDOWS_TYPES), + ('event_month', MONTH_NAMES)] for attr_name, valid_set in validation_tuples: if getattr(self, attr_name) not in valid_set: raise ValueError(f"{getattr(self, attr_name)} is not a valid value for {attr_name}") @@ -261,11 +262,8 @@ class FormData: else: window_interval = always_on - #month = self.event_month[:3] - datetime_object = datetime.datetime.strptime(self.event_month[:3], "%b") - month = datetime_object.month + month = MONTH_NAMES.index(self.event_month) + 1 - # set location wx_station = self.nearest_weather_station() temp_profile = cara.data.weather.mean_hourly_temperatures(wx_station[0], month) @@ -622,6 +620,9 @@ def baseline_raw_form_data(): 'infected_lunch_start': '12:30', 'infected_people': '1', 'infected_start': '09:00', + 'location_latitude': 46.20833, + 'location_longitude': 6.14275, + 'location_name': 'Geneva', 'mask_type': 'Type I', 'mask_wearing_option': 'mask_off', 'mechanical_ventilation_type': '', @@ -658,6 +659,11 @@ WINDOWS_TYPES = {'window_sliding', 'window_hinged', 'not-applicable'} COFFEE_OPTIONS_INT = {'coffee_break_0': 0, 'coffee_break_1': 1, 'coffee_break_2': 2, 'coffee_break_4': 4} +MONTH_NAMES = [ + 'January', 'February', 'March', 'April', 'May', 'June', 'July', + 'August', 'September', 'October', 'November', 'December', +] + def _hours2timestring(hours: float): # Convert times like 14.5 to strings, like "14:30" diff --git a/cara/tests/apps/calculator/test_model_generator.py b/cara/tests/apps/calculator/test_model_generator.py index 82ab2279..db141133 100644 --- a/cara/tests/apps/calculator/test_model_generator.py +++ b/cara/tests/apps/calculator/test_model_generator.py @@ -434,6 +434,12 @@ def test_key_validation_mech_ventilation_type_na(baseline_form_data): model_generator.FormData.from_dict(baseline_form_data) +def test_key_validation_event_month(baseline_form_data): + baseline_form_data['event_month'] = 'invalid month' + with pytest.raises(ValueError, match='invalid month is not a valid value for event_month'): + model_generator.FormData.from_dict(baseline_form_data) + + def test_default_types(): # Validate that FormData._DEFAULTS are complete and of the correct type. # Validate that we have the right types and matching attributes to the DEFAULTS.