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

Re-enable validation in Ribasim python #1446

Merged
merged 4 commits into from
May 3, 2024
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: 2 additions & 2 deletions python/ribasim/ribasim/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pandas as pd
import pydantic
from geopandas import GeoDataFrame
from pydantic import ConfigDict, Field, model_validator
from pydantic import ConfigDict, Field, NonNegativeInt, model_validator
from shapely.geometry import Point

from ribasim.geometry import BasinAreaSchema, NodeTable
Expand Down Expand Up @@ -87,7 +87,7 @@ class Logging(ChildModel):


class Node(pydantic.BaseModel):
node_id: int
node_id: NonNegativeInt
geometry: Point
name: str = ""
subnetwork_id: int | None = None
Expand Down
2 changes: 1 addition & 1 deletion python/ribasim/ribasim/geometry/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


class NodeSchema(pa.SchemaModel):
node_id: Series[Int32]
node_id: Series[Int32] = pa.Field(ge=0)
name: Series[str] = pa.Field(default="")
node_type: Series[str] = pa.Field(default="")
subnetwork_id: Series[pd.Int32Dtype] = pa.Field(
Expand Down
17 changes: 0 additions & 17 deletions python/ribasim/ribasim/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,23 +229,6 @@ def _children(self):
if isinstance(getattr(self, k), ChildModel)
}

def validate_model_node_field_ids(self):
raise NotImplementedError()

def validate_model_node_ids(self):
raise NotImplementedError()

def validate_model(self):
"""Validate the model.

Checks:
- Whether the node IDs of the node_type fields are valid
- Whether the node IDs in the node field correspond to the node IDs on the node type fields
"""

self.validate_model_node_field_ids()
self.validate_model_node_ids()

@classmethod
def read(cls, filepath: str | PathLike[str]) -> "Model":
"""Read model from TOML file."""
Expand Down
19 changes: 4 additions & 15 deletions python/ribasim/tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import xugrid
from pydantic import ValidationError
from pyproj import CRS
from ribasim import Node
from ribasim.config import Solver
from ribasim.geometry.edge import NodeData
from ribasim.input_base import esc_id
Expand Down Expand Up @@ -65,24 +66,12 @@ def test_exclude_unset(basic):
assert d["solver"]["saveat"] == 86400.0


@pytest.mark.xfail(reason="Needs implementation")
def test_invalid_node_id(basic):
model = basic

# Add entry with invalid node ID
df = model.pump.static.df._append(
{"flow_rate": 1, "node_id": -1, "active": True},
ignore_index=True,
)
# Currently can't handle mixed NaN and None in a DataFrame
df = df.where(pd.notna(df), None)
model.pump.static.df = df

def test_invalid_node_id():
with pytest.raises(
ValueError,
match=re.escape("Node IDs must be non-negative integers, got [-1]."),
match=r".* Input should be greater than or equal to 0 .*",
):
model.validate_model_node_field_ids()
Node(-1, Point(7.0, 7.0))


@pytest.mark.xfail(reason="Should be reimplemented by the .add() API.")
Expand Down
Loading