From 79cd4b65be272fc1cb00572dbbba906e3f40e70f Mon Sep 17 00:00:00 2001 From: Stefano Grava Date: Thu, 13 Aug 2026 17:24:35 +0200 Subject: [PATCH 1/4] add base graph tests --- tests/test_graphs/test_base_graph.py | 85 ++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/tests/test_graphs/test_base_graph.py b/tests/test_graphs/test_base_graph.py index 98e08557d..e1d820830 100644 --- a/tests/test_graphs/test_base_graph.py +++ b/tests/test_graphs/test_base_graph.py @@ -69,6 +69,91 @@ def test_basegraph_init(n_nodes: int) -> None: assert len(graph.ud_edges(radius=10.0 * scale)) == max_n_edges +def test_empty_graph() -> None: + graph = BaseGraph() + assert len(graph.edges) == 0 + assert len(graph.sorted_edges) == 0 + + assert graph.has_coords is False + assert graph.coords == {} + + assert graph.has_node_weights is False + assert graph.node_weights == {} + + assert graph.has_edge_weights is False + assert graph.edge_weights == {} + + +def test_node_coords_update() -> None: + graph = BaseGraph() + + # nodes needs to be added before updating coordinates + with pytest.raises( + ValueError, match="Set of nodes in the given dictionary does not match the graph nodes." + ): + graph.coords = {0: (0.5, 0.5)} + + # add nodes without coordinates + graph.add_nodes_from([0, 1, 2]) + assert graph.has_coords is False + + # set new coordinates + new_coords = {0: (0.3, 0.4), 1: (0.5, 0.6), 2: (0.7, 0.8)} + graph.coords = new_coords + assert graph.coords == new_coords + + # update coordinates for existing nodes + graph.nodes[0]["pos"] = (0.9, 1.0) + assert graph.coords == {0: (0.9, 1.0), 1: (0.5, 0.6), 2: (0.7, 0.8)} + + +def test_node_weights_update() -> None: + graph = BaseGraph() + + # nodes needs to be added before updating weights + with pytest.raises( + ValueError, match="Set of nodes in the given dictionary does not match the graph nodes." + ): + graph.node_weights = {0: 0.5} + + # add nodes without weights + graph.add_nodes_from([0, 1, 2]) + assert graph.has_node_weights is False + + # set new weights + new_weights = {0: 0.3, 1: 0.4, 2: 0.5} + graph.node_weights = new_weights + assert graph.node_weights == new_weights + + # update weights for existing nodes + graph.nodes[0]["weight"] = 0.9 + assert graph.node_weights == {0: 0.9, 1: 0.4, 2: 0.5} + + +def test_edge_weights_update() -> None: + graph = BaseGraph() + + # nodes needs to be added before updating weights + with pytest.raises( + ValueError, + match="Set of edges in the given dictionary does not match the graph ordered edges.", + ): + graph.edge_weights = {(0, 1): 0.5} + + # add edges without weights + graph.add_edges_from([(0, 1), (1, 2), (2, 0)]) + assert graph.has_edge_weights is False + + # set new weights + new_weights = {(0, 1): 0.3, (1, 2): 0.4, (0, 2): 0.5} + graph.edge_weights = new_weights + assert graph.edge_weights == new_weights + + # update weights for existing edges + graph.edges[0, 1]["weight"] = 0.9 + assert graph.edge_weights == {(0, 1): 0.9, (1, 2): 0.4, (0, 2): 0.5} + + @pytest.mark.parametrize("n_nodes", [3, 8, 13]) def test_basegraph_interactions(n_nodes: int) -> None: From b4bb7f1c47ff41bb7b8509a896934cbfa85a8a80 Mon Sep 17 00:00:00 2001 From: Stefano Grava Date: Thu, 13 Aug 2026 18:23:10 +0200 Subject: [PATCH 2/4] Antoine's comment --- tests/test_graphs/test_base_graph.py | 65 +++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 6 deletions(-) diff --git a/tests/test_graphs/test_base_graph.py b/tests/test_graphs/test_base_graph.py index e1d820830..99387d235 100644 --- a/tests/test_graphs/test_base_graph.py +++ b/tests/test_graphs/test_base_graph.py @@ -84,15 +84,32 @@ def test_empty_graph() -> None: assert graph.edge_weights == {} -def test_node_coords_update() -> None: +def test_coords_update_requires_matching_nodes() -> None: graph = BaseGraph() - # nodes needs to be added before updating coordinates with pytest.raises( ValueError, match="Set of nodes in the given dictionary does not match the graph nodes." ): graph.coords = {0: (0.5, 0.5)} + graph.add_nodes_from([0, 1, 2]) + + # missing a node + with pytest.raises( + ValueError, match="Set of nodes in the given dictionary does not match the graph nodes." + ): + graph.coords = {0: (0.3, 0.4), 1: (0.5, 0.6)} + + # extra node not in the graph + with pytest.raises( + ValueError, match="Set of nodes in the given dictionary does not match the graph nodes." + ): + graph.coords = {0: (0.3, 0.4), 1: (0.5, 0.6), 2: (0.7, 0.8), 3: (0.9, 1.0)} + + +def test_node_coords_update() -> None: + graph = BaseGraph() + # add nodes without coordinates graph.add_nodes_from([0, 1, 2]) assert graph.has_coords is False @@ -107,15 +124,32 @@ def test_node_coords_update() -> None: assert graph.coords == {0: (0.9, 1.0), 1: (0.5, 0.6), 2: (0.7, 0.8)} -def test_node_weights_update() -> None: +def test_node_weights_update_requires_matching_nodes() -> None: graph = BaseGraph() - # nodes needs to be added before updating weights with pytest.raises( ValueError, match="Set of nodes in the given dictionary does not match the graph nodes." ): graph.node_weights = {0: 0.5} + graph.add_nodes_from([0, 1, 2]) + + # missing a node + with pytest.raises( + ValueError, match="Set of nodes in the given dictionary does not match the graph nodes." + ): + graph.node_weights = {0: 0.3, 1: 0.4} + + # extra node not in the graph + with pytest.raises( + ValueError, match="Set of nodes in the given dictionary does not match the graph nodes." + ): + graph.node_weights = {0: 0.3, 1: 0.4, 2: 0.5, 3: 0.6} + + +def test_node_weights_update() -> None: + graph = BaseGraph() + # add nodes without weights graph.add_nodes_from([0, 1, 2]) assert graph.has_node_weights is False @@ -130,16 +164,35 @@ def test_node_weights_update() -> None: assert graph.node_weights == {0: 0.9, 1: 0.4, 2: 0.5} -def test_edge_weights_update() -> None: +def test_edge_weights_update_requires_matching_edges() -> None: graph = BaseGraph() - # nodes needs to be added before updating weights with pytest.raises( ValueError, match="Set of edges in the given dictionary does not match the graph ordered edges.", ): graph.edge_weights = {(0, 1): 0.5} + graph.add_edges_from([(0, 1), (1, 2), (2, 0)]) + + # missing an edge + with pytest.raises( + ValueError, + match="Set of edges in the given dictionary does not match the graph ordered edges.", + ): + graph.edge_weights = {(0, 1): 0.3, (1, 2): 0.4} + + # extra edge not in the graph + with pytest.raises( + ValueError, + match="Set of edges in the given dictionary does not match the graph ordered edges.", + ): + graph.edge_weights = {(0, 1): 0.3, (1, 2): 0.4, (0, 2): 0.5, (1, 3): 0.6} + + +def test_edge_weights_update() -> None: + graph = BaseGraph() + # add edges without weights graph.add_edges_from([(0, 1), (1, 2), (2, 0)]) assert graph.has_edge_weights is False From c6879335eb188636c49aa903e9ba00ab7b0043b6 Mon Sep 17 00:00:00 2001 From: Stefano Grava Date: Fri, 14 Aug 2026 09:09:54 +0200 Subject: [PATCH 3/4] Antoine's comment for real this time --- tests/test_graphs/test_base_graph.py | 73 ++++++++++------------------ 1 file changed, 25 insertions(+), 48 deletions(-) diff --git a/tests/test_graphs/test_base_graph.py b/tests/test_graphs/test_base_graph.py index 99387d235..a8a759475 100644 --- a/tests/test_graphs/test_base_graph.py +++ b/tests/test_graphs/test_base_graph.py @@ -84,27 +84,20 @@ def test_empty_graph() -> None: assert graph.edge_weights == {} -def test_coords_update_requires_matching_nodes() -> None: - graph = BaseGraph() - - with pytest.raises( - ValueError, match="Set of nodes in the given dictionary does not match the graph nodes." - ): - graph.coords = {0: (0.5, 0.5)} - - graph.add_nodes_from([0, 1, 2]) - - # missing a node +def test_coords_update_missing_node() -> None: + graph = BaseGraph.from_nodes([0, 1, 2]) with pytest.raises( ValueError, match="Set of nodes in the given dictionary does not match the graph nodes." ): graph.coords = {0: (0.3, 0.4), 1: (0.5, 0.6)} - # extra node not in the graph + +def test_coords_update_extra_node() -> None: + graph = BaseGraph.from_nodes(["a", "b", "c"]) with pytest.raises( ValueError, match="Set of nodes in the given dictionary does not match the graph nodes." ): - graph.coords = {0: (0.3, 0.4), 1: (0.5, 0.6), 2: (0.7, 0.8), 3: (0.9, 1.0)} + graph.coords = {"a": (0.3, 0.4), "b": (0.5, 0.6), "c": (0.7, 0.8), "d": (0.9, 1.0)} def test_node_coords_update() -> None: @@ -124,34 +117,24 @@ def test_node_coords_update() -> None: assert graph.coords == {0: (0.9, 1.0), 1: (0.5, 0.6), 2: (0.7, 0.8)} -def test_node_weights_update_requires_matching_nodes() -> None: - graph = BaseGraph() - - with pytest.raises( - ValueError, match="Set of nodes in the given dictionary does not match the graph nodes." - ): - graph.node_weights = {0: 0.5} - - graph.add_nodes_from([0, 1, 2]) - - # missing a node +def test_node_weights_update_missing_node() -> None: + graph = BaseGraph.from_nodes([0, 1, 2]) with pytest.raises( ValueError, match="Set of nodes in the given dictionary does not match the graph nodes." ): graph.node_weights = {0: 0.3, 1: 0.4} - # extra node not in the graph + +def test_node_weights_update_extra_node() -> None: + graph = BaseGraph.from_nodes(["a", "b", "c"]) with pytest.raises( ValueError, match="Set of nodes in the given dictionary does not match the graph nodes." ): - graph.node_weights = {0: 0.3, 1: 0.4, 2: 0.5, 3: 0.6} + graph.node_weights = {"a": 0.3, "b": 0.4, "c": 0.5, "d": 0.6} def test_node_weights_update() -> None: - graph = BaseGraph() - - # add nodes without weights - graph.add_nodes_from([0, 1, 2]) + graph = BaseGraph.from_nodes([0, 1, 2]) assert graph.has_node_weights is False # set new weights @@ -164,37 +147,31 @@ def test_node_weights_update() -> None: assert graph.node_weights == {0: 0.9, 1: 0.4, 2: 0.5} -def test_edge_weights_update_requires_matching_edges() -> None: - graph = BaseGraph() - - with pytest.raises( - ValueError, - match="Set of edges in the given dictionary does not match the graph ordered edges.", - ): - graph.edge_weights = {(0, 1): 0.5} - - graph.add_edges_from([(0, 1), (1, 2), (2, 0)]) - - # missing an edge +def test_edge_weights_update_missing_edge() -> None: + graph = BaseGraph([(0, 1), (1, 2), (2, 0)]) with pytest.raises( ValueError, match="Set of edges in the given dictionary does not match the graph ordered edges.", ): graph.edge_weights = {(0, 1): 0.3, (1, 2): 0.4} - # extra edge not in the graph + +def test_edge_weights_update_extra_edge() -> None: + graph = BaseGraph([("a", "b"), ("b", "c"), ("c", "a")]) with pytest.raises( ValueError, match="Set of edges in the given dictionary does not match the graph ordered edges.", ): - graph.edge_weights = {(0, 1): 0.3, (1, 2): 0.4, (0, 2): 0.5, (1, 3): 0.6} + graph.edge_weights = { + ("a", "b"): 0.3, + ("b", "c"): 0.4, + ("c", "a"): 0.5, + ("b", "d"): 0.6, + } def test_edge_weights_update() -> None: - graph = BaseGraph() - - # add edges without weights - graph.add_edges_from([(0, 1), (1, 2), (2, 0)]) + graph = BaseGraph([(0, 1), (1, 2), (2, 0)]) assert graph.has_edge_weights is False # set new weights From fa3dcdaf2b7f3e0195bc78bfd21f130632048514 Mon Sep 17 00:00:00 2001 From: Stefano Grava Date: Fri, 14 Aug 2026 09:11:26 +0200 Subject: [PATCH 4/4] simplify test --- tests/test_graphs/test_base_graph.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/test_graphs/test_base_graph.py b/tests/test_graphs/test_base_graph.py index a8a759475..25db72735 100644 --- a/tests/test_graphs/test_base_graph.py +++ b/tests/test_graphs/test_base_graph.py @@ -101,10 +101,7 @@ def test_coords_update_extra_node() -> None: def test_node_coords_update() -> None: - graph = BaseGraph() - - # add nodes without coordinates - graph.add_nodes_from([0, 1, 2]) + graph = BaseGraph.from_nodes([0, 1, 2]) assert graph.has_coords is False # set new coordinates