From a05fc37291641395cce8bb91924f977fe4e07008 Mon Sep 17 00:00:00 2001 From: Marlon Vermeulen Date: Mon, 29 Apr 2024 10:55:20 +0200 Subject: [PATCH] bug: add support for reading process with semicolon, e.g. 0:4, assuming this is the same as 0 1 2 3 4 (#562) --- hydrolib/core/dimr/models.py | 46 +++++++++++++++++++++++++++++------- tests/test_model.py | 27 +++++++++++++++++++++ 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/hydrolib/core/dimr/models.py b/hydrolib/core/dimr/models.py index 077b4628f..40ab9b0b3 100644 --- a/hydrolib/core/dimr/models.py +++ b/hydrolib/core/dimr/models.py @@ -94,10 +94,9 @@ def validate_process(cls, value, values: dict) -> Union[None, int]: return None if isinstance(value, str): - split_values = value.split() - if cls.validate_process_as_str(split_values): - return len(split_values) - + if cls._validate_process_as_str(value): + return cls._get_process_from_str(value) + if isinstance(value, int): if value <= 0: raise ValueError( @@ -108,16 +107,47 @@ def validate_process(cls, value, values: dict) -> Union[None, int]: raise ValueError( f"In component '{values.get('name')}', the keyword process '{value}', is incorrect." ) + + @classmethod + def _get_process_from_str(cls, values: str) -> int: + if ':' in values: + semicolon_split_values = values.split(':') + semicolon_process = int(semicolon_split_values[-1]) + 1 + return semicolon_process + + return len(values.split()) @classmethod - def validate_process_as_str(cls, values: str) -> bool: - if len(values) < 1: + def _validate_process_as_str(cls, values: str) -> bool: + if ':' in values: + return cls._validate_process_as_semicolon_str(values) + + return cls._validate_process_as_list_str(values) + + @classmethod + def _validate_process_as_semicolon_str(cls, values: str) -> bool: + semicolon_split_values = values.split(':') + + if len(semicolon_split_values) != 2: + return False + + last_value : str = semicolon_split_values[-1] + if last_value.isdigit(): + return True + + return False + + @classmethod + def _validate_process_as_list_str(cls, values: str) -> bool: + split_values = values.split() + + if len(split_values) < 1: return False - if values[0] != "0": + if split_values[0] != "0": return False - for value in values: + for value in split_values: if not value.isdigit(): return False diff --git a/tests/test_model.py b/tests/test_model.py index 99636a8da..924eb9e91 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -608,6 +608,33 @@ def test_dimr_with_fmcomponent_given_correct_style_for_setting_process( dimr_config.save(filepath=temporary_save_location) assert_files_equal(temporary_dimr_config_file, temporary_save_location) + + @pytest.mark.parametrize( + "input_process, expected_process", + [ + pytest.param("0 1", 2), + pytest.param("0 1 2", 3), + pytest.param("0 1 2 3", 4), + pytest.param("0 1 2 3 4", 5), + pytest.param("0:1", 2), + pytest.param("0:2", 3), + pytest.param("0:3", 4), + pytest.param("0:4", 5), + ], + ) + def test_dimr_with_fmcomponent_process_component_set_correctly( + self, tmp_path, input_process, expected_process + ): + dimr_config_data = self.get_fm_dimr_config_data(input_process) + ( + temporary_dimr_config_file, + temporary_save_location, + ) = self.setup_temporary_files(tmp_path, dimr_config_data) + + dimr_config = DIMR(filepath=temporary_dimr_config_file) + dimr_config.save(filepath=temporary_save_location) + + assert dimr_config.component[0].process == expected_process def test_dimr_with_fmcomponent_given_old_incorrect_style_for_setting_process( self, tmp_path