Skip to content

Commit

Permalink
bug: add raise valueerror on not int for process (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
MRVermeulenDeltares committed Apr 17, 2024
1 parent 16b9b64 commit 5deeb66
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
6 changes: 4 additions & 2 deletions hydrolib/core/dimr/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ def __init__(self, **data):
super().__init__(**data)
process_input = data.get("process", None)
self.process = self._set_process_correctly(process_input)

def _set_process_correctly(self, process_input):
def _set_process_correctly(self, process_input : int):
if not process_input or process_input == 0:
return None
if not isinstance(process_input, int):
raise ValueError(f"Given process value {process_input}, is not of expected type {str(int)}")
return " ".join(str(i) for i in range(process_input))

@classmethod
Expand Down
32 changes: 32 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ def test_dimr_with_fmcomponent_saving_process_when_process_zero(tmp_path):
@pytest.mark.parametrize(
"input_process, expected_process_format",
[
pytest.param(None, None),
pytest.param(0, None),
pytest.param(1, "0"),
pytest.param(2, "0 1"),
Expand All @@ -598,3 +599,34 @@ def test_fmcomponent_process_after_init(
mpiCommunicator="DFM_COMM_DFMWORLD",
)
assert component.process == expected_process_format

def test_fmcomponent_without_process_after_init():
expected_process_format = None
component = FMComponent(
name="test",
workingDir=".",
inputfile="test.mdu",
mpiCommunicator="DFM_COMM_DFMWORLD",
)
assert component.process == expected_process_format

@pytest.mark.parametrize(
"input_process",
[
pytest.param("lalala"),
pytest.param("123"),
pytest.param(0.5),
],
)
def test_fmcomponent_process_after_init_with_incorrect_input_throws_valueerror(input_process: int):
with pytest.raises(ValueError) as error:
FMComponent(
name="test",
workingDir=".",
inputfile="test.mdu",
process=input_process,
mpiCommunicator="DFM_COMM_DFMWORLD",
)

expected_message = f"Given process value {input_process}, is not of expected type {str(int)}"
assert expected_message in str(error.value)

0 comments on commit 5deeb66

Please sign in to comment.