Skip to content

Commit

Permalink
bug: update process on input for fmcomponent and add tests. (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
MRVermeulenDeltares committed Apr 17, 2024
1 parent 41ac1f1 commit e297865
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
12 changes: 11 additions & 1 deletion hydrolib/core/dimr/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Component(BaseModel, ABC):
name: str
workingDir: Path
inputFile: Path
process: Optional[int]
process: Optional[str]
setting: Optional[List[KeyValuePair]] = []
parameter: Optional[List[KeyValuePair]] = []
mpiCommunicator: Optional[str]
Expand Down Expand Up @@ -87,6 +87,16 @@ class FMComponent(Component):
"""Component to include the D-Flow FM program in a DIMR control flow."""

library: Literal["dflowfm"] = "dflowfm"

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, data):
if not data or data == 0:
return None
return ' '.join(str(i) for i in range(data))

@classmethod
def get_model(cls):
Expand Down
47 changes: 47 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,50 @@ def test_model_diskonlyfilemodel_field_is_constructed_correctly(
assert relevant_field == input
else:
assert relevant_field.filepath == input

@pytest.mark.parametrize(
"input_process, expected_process_format",
[
pytest.param(1, "0"),
pytest.param(2, "0 1"),
pytest.param(3, "0 1 2"),
pytest.param(4, "0 1 2 3"),
pytest.param(5, "0 1 2 3 4"),
],
)
def test_dimr_with_fmcomponent_saving_process(tmp_path, input_process : int, expected_process_format : str):
component = RRComponent(name="test", workingDir='.', inputfile='test.mdu', process=input_process, mpiCommunicator="DFM_COMM_DFMWORLD")
dimr = DIMR(component=component)
save_location : Path = tmp_path/'dimr_config.xml'
dimr.save(filepath=save_location)

line_to_check = f"<process>{expected_process_format}</process>"

with open(save_location, 'r') as file:
assert any(line.strip() == line_to_check for line in file), f"File {save_location} does not contain the line: {line_to_check}"

def test_dimr_with_fmcomponent_saving_process_when_process_zero(tmp_path):
component = FMComponent(name="test", workingDir='.', inputfile='test.mdu', process=0, mpiCommunicator="DFM_COMM_DFMWORLD")
dimr = DIMR(component=component)
save_location : Path = tmp_path/'dimr_config.xml'
dimr.save(filepath=save_location)

process = "<process>"

with open(save_location, 'r') as file:
assert process not in file, f"File {save_location} does contain the line: {process}"

@pytest.mark.parametrize(
"input_process, expected_process_format",
[
pytest.param(0, None),
pytest.param(1, "0"),
pytest.param(2, "0 1"),
pytest.param(3, "0 1 2"),
pytest.param(4, "0 1 2 3"),
pytest.param(5, "0 1 2 3 4"),
],
)
def test_fmcomponent_process_after_init(input_process : int, expected_process_format : str):
component = FMComponent(name="test", workingDir='.', inputfile='test.mdu', process=input_process, mpiCommunicator="DFM_COMM_DFMWORLD")
assert component.process == expected_process_format

0 comments on commit e297865

Please sign in to comment.