diff --git a/python/ribasim/ribasim/config.py b/python/ribasim/ribasim/config.py index 30408fe05..d36416862 100644 --- a/python/ribasim/ribasim/config.py +++ b/python/ribasim/ribasim/config.py @@ -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 @@ -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 diff --git a/python/ribasim/ribasim/geometry/node.py b/python/ribasim/ribasim/geometry/node.py index d77a40f99..990dd394c 100644 --- a/python/ribasim/ribasim/geometry/node.py +++ b/python/ribasim/ribasim/geometry/node.py @@ -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( diff --git a/python/ribasim/ribasim/model.py b/python/ribasim/ribasim/model.py index 2b4f195ef..68f90e812 100644 --- a/python/ribasim/ribasim/model.py +++ b/python/ribasim/ribasim/model.py @@ -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.""" diff --git a/python/ribasim/tests/test_model.py b/python/ribasim/tests/test_model.py index 3e2ed89d6..c64ba479a 100644 --- a/python/ribasim/tests/test_model.py +++ b/python/ribasim/tests/test_model.py @@ -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 @@ -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.")