Skip to content

Commit

Permalink
Duplicate edges (#1719)
Browse files Browse the repository at this point in the history
Fixes #1255
It gives an error if a duplicated edge is added. Unit test is added as
well

---------

Co-authored-by: Martijn Visser <[email protected]>
  • Loading branch information
Jingru923 and visr authored Aug 13, 2024
1 parent 2cb681a commit 1f6e238
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions python/ribasim/ribasim/geometry/edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ def add(
self.df = GeoDataFrame[EdgeSchema](
pd.concat([self.df, table_to_append], ignore_index=True)
)
if self.df.duplicated(subset=["from_node_id", "to_node_id"]).any():
raise ValueError(
f"Edges have to be unique, but edge ({from_node.node_id}, {to_node.node_id}) already exists."
)
self.df.index.name = "fid"

def _get_where_edge_type(self, edge_type: str) -> NDArray[np.bool_]:
Expand Down
13 changes: 13 additions & 0 deletions python/ribasim/tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ def test_edge_table(basic):
assert df.crs == CRS.from_epsg(28992)


def test_duplicate_edge(trivial):
model = trivial
with pytest.raises(
ValueError,
match=re.escape("Edges have to be unique, but edge (6, 0) already exists."),
):
model.edge.add(
model.basin[6],
model.tabulated_rating_curve[0],
name="duplicate",
)


def test_indexing(basic):
model = basic

Expand Down

0 comments on commit 1f6e238

Please sign in to comment.