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 3 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
2 changes: 2 additions & 0 deletions python/ribasim/ribasim/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ def _children(self):
}

def validate_model_node_field_ids(self):
"""Check whether the node IDs of the node_type fields are valid."""
Jingru923 marked this conversation as resolved.
Show resolved Hide resolved

raise NotImplementedError()

def validate_model_node_ids(self):
Expand Down
17 changes: 5 additions & 12 deletions python/ribasim/tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
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
from ribasim.model import Model
from ribasim.nodes import pump
from shapely import Point


Expand Down Expand Up @@ -65,24 +67,15 @@ def test_exclude_unset(basic):
assert d["solver"]["saveat"] == 86400.0


@pytest.mark.xfail(reason="Needs implementation")
# @pytest.mark.xfail(reason="Needs implementation")
Jingru923 marked this conversation as resolved.
Show resolved Hide resolved
def test_invalid_node_id(basic):
model = basic
Jingru923 marked this conversation as resolved.
Show resolved Hide resolved

# 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

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()
model.pump.add(Node(-1, Point(7.0, 7.0)), [pump.Static(flow_rate=[0.5 / 3600])])


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