Skip to content

Commit

Permalink
bug: add support for reading process with semicolon, e.g. 0:4, assumi…
Browse files Browse the repository at this point in the history
…ng this is the same as 0 1 2 3 4 (#562)
  • Loading branch information
MRVermeulenDeltares committed Apr 29, 2024
1 parent a45cb2f commit a05fc37
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
46 changes: 38 additions & 8 deletions hydrolib/core/dimr/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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

Expand Down
27 changes: 27 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a05fc37

Please sign in to comment.