Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: locationtype should not be written for CrossSections or ObservationCrossSections #683

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion hydrolib/core/dflowfm/crosssection/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,9 @@ def validate_that_location_specification_is_correct(cls, values: Dict) -> Dict:
return validate_location_specification(
values,
config=LocationValidationConfiguration(
validate_node=False, validate_num_coordinates=False
validate_node=False,
validate_num_coordinates=False,
validate_location_type=False,
),
fields=LocationValidationFieldNames(x_coordinates="x", y_coordinates="y"),
)
Expand Down
9 changes: 7 additions & 2 deletions hydrolib/core/dflowfm/ini/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,9 @@ class LocationValidationConfiguration(BaseModel):
validate_num_coordinates: bool = True
"""bool, optional: Whether or not the number of coordinates should be validated or not. This option is only relevant when `validate_coordinates` is True. Defaults to True."""

validate_location_type: bool = True
"""bool, optional: Whether or not the location type should be validated. Defaults to True."""

minimum_num_coordinates: int = 0
"""int, optional: The minimum required number of coordinates. This option is only relevant when `validate_coordinates` is True. Defaults to 0."""

Expand Down Expand Up @@ -563,14 +566,16 @@ def is_valid_coordinates_with_num_coordinates_specification() -> bool:

if config.validate_node:
if is_valid_node_specification():
validate_location_type(LocationType.oned)
if config.validate_location_type:
validate_location_type(LocationType.oned)
return values

error_parts.append(fields.node_id)

if config.validate_branch:
if is_valid_branch_specification():
validate_location_type(LocationType.oned)
if config.validate_location_type:
validate_location_type(LocationType.oned)
return values

error_parts.append(f"{fields.branch_id} and {fields.chainage}")
Expand Down
4 changes: 3 additions & 1 deletion hydrolib/core/dflowfm/obscrosssection/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ def validate_that_location_specification_is_correct(cls, values: Dict) -> Dict:
return validate_location_specification(
values,
config=LocationValidationConfiguration(
validate_node=False, minimum_num_coordinates=2
validate_node=False,
minimum_num_coordinates=2,
validate_location_type=False,
),
)

Expand Down
1 change: 0 additions & 1 deletion tests/data/reference/crosssection/crsloc.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ branchId = branch1 # Branch on which the cross section is located.
chainage = 123.00 # Chainage on the branch (m).
shift = 0.00 # Vertical shift of the cross section definition [m]. Defined positive upwards.
definitionId = randomDefinition # Id of cross section definition.
locationtype = 1d

39 changes: 39 additions & 0 deletions tests/dflowfm/ini/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def test_default(self):
assert config.validate_coordinates == True
assert config.validate_branch == True
assert config.validate_num_coordinates == True
assert config.validate_location_type == True
assert config.minimum_num_coordinates == 0


Expand Down Expand Up @@ -210,6 +211,44 @@ def test_correct_1d_fields_locationtype_is_added(
)
assert validated_values == expected_values

@pytest.mark.parametrize(
"values",
[
pytest.param({"nodeid": "some_nodeid"}, id="nodeId"),
pytest.param(
{"branchid": "some_branchid", "chainage": 123},
id="branch specification",
),
],
)
def test_validate_location_type_false_does_not_add_location_type(
self, values: dict
):
config = LocationValidationConfiguration(validate_location_type=False)
validated_values = validate_location_specification(values, config)

assert validated_values == values

@pytest.mark.parametrize(
"values",
[
pytest.param({"nodeid": "some_nodeid"}, id="nodeId"),
pytest.param(
{"branchid": "some_branchid", "chainage": 123},
id="branch specification",
),
],
)
def test_validate_location_type_false_does_not_validate_location_type(
self, values: dict
):
config = LocationValidationConfiguration(validate_location_type=False)
values["locationtype"] = "This is an invalid location type..."

validated_values = validate_location_specification(values, config)

assert validated_values == values


class TestGetKeyRenamingRootValidator:
class DummyModel(BaseModel):
Expand Down
19 changes: 19 additions & 0 deletions tests/dflowfm/test_crosssection.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,22 @@ def test_create_and_save_crosslocmodel_correctly_saves_file_with_correct_enum_va
crossloc_model.save(filepath=output_file)

assert_files_equal(output_file, reference_file, skip_lines=[0])

def test_locationtype_is_not_written_for_crosssection(self, tmp_path: Path):
model = CrossLocModel()
model.crosssection.append(
CrossSection(
id="testCrossSection",
branchid="branch1",
chainage=1,
definitionid="testDefinition",
)
)

crs_file = tmp_path / "crsloc.ini"
model.save(filepath=crs_file)

with open(crs_file, "r") as file:
content = file.read()

assert "locationtype" not in content
21 changes: 21 additions & 0 deletions tests/dflowfm/test_obscrosssection.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pathlib import Path
from typing import List

import pytest
Expand Down Expand Up @@ -154,6 +155,26 @@ def test_create(self):
assert isinstance(model.observationcrosssection, List)
assert len(model.observationcrosssection) == 0

def test_locationtype_is_not_written_for_observationcrosssection(
self, tmp_path: Path
):
model = ObservationCrossSectionModel()
model.observationcrosssection.append(
ObservationCrossSection(
name="testName",
branchId="testbranch",
chainage=123,
)
)

obs_crs_file = tmp_path / "obs_crs.ini"
model.save(filepath=obs_crs_file)

with open(obs_crs_file, "r") as file:
content = file.read()

assert "locationtype" not in content


def _create_observation_cross_section_values() -> dict:
values = dict(
Expand Down