diff --git a/python/ribasim/ribasim/geometry/edge.py b/python/ribasim/ribasim/geometry/edge.py index 4fb268dfe..f0c42b6fd 100644 --- a/python/ribasim/ribasim/geometry/edge.py +++ b/python/ribasim/ribasim/geometry/edge.py @@ -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_]: diff --git a/python/ribasim/tests/test_model.py b/python/ribasim/tests/test_model.py index 734c8121f..db249f0a4 100644 --- a/python/ribasim/tests/test_model.py +++ b/python/ribasim/tests/test_model.py @@ -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