summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--networkx/algorithms/tree/tests/test_branchings.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/networkx/algorithms/tree/tests/test_branchings.py b/networkx/algorithms/tree/tests/test_branchings.py
index 34174463..b7bbd9ea 100644
--- a/networkx/algorithms/tree/tests/test_branchings.py
+++ b/networkx/algorithms/tree/tests/test_branchings.py
@@ -1,4 +1,5 @@
import math
+from operator import itemgetter
import pytest
@@ -199,6 +200,26 @@ def test_greedy_max1():
assert_equal_branchings(B, B_)
+def test_greedy_branching_kwarg_kind():
+ G = G1()
+ with pytest.raises(nx.NetworkXException, match="Unknown value for `kind`."):
+ B = branchings.greedy_branching(G, kind="lol")
+
+
+def test_greedy_branching_for_unsortable_nodes():
+ G = nx.DiGraph()
+ G.add_weighted_edges_from([((2, 3), 5, 1), (3, "a", 1), (2, 4, 5)])
+ edges = [(u, v, data.get("weight", 1)) for (u, v, data) in G.edges(data=True)]
+ with pytest.raises(TypeError):
+ edges.sort(key=itemgetter(2, 0, 1), reverse=True)
+ B = branchings.greedy_branching(G, kind="max").edges(data=True)
+ assert list(B) == [
+ ((2, 3), 5, {"weight": 1}),
+ (3, "a", {"weight": 1}),
+ (2, 4, {"weight": 5}),
+ ]
+
+
def test_greedy_max2():
# Different default weight.
#
@@ -427,6 +448,38 @@ def test_edge_attribute_preservation_multigraph():
assert B[0][1][0]["otherattr2"] == 3
+def test_Edmond_kind():
+ G = nx.MultiGraph()
+
+ edgelist = [
+ (0, 1, [("weight", 5), ("otherattr", 1), ("otherattr2", 3)]),
+ (0, 2, [("weight", 5), ("otherattr", 2), ("otherattr2", 2)]),
+ (1, 2, [("weight", 6), ("otherattr", 3), ("otherattr2", 1)]),
+ ]
+ G.add_edges_from(edgelist * 2) # Make sure we have duplicate edge paths
+ ed = branchings.Edmonds(G)
+ with pytest.raises(nx.NetworkXException, match="Unknown value for `kind`."):
+ ed.find_optimum(kind="lol", preserve_attrs=True)
+
+
+def test_MultiDiGraph_EdgeKey():
+ # test if more than one edges has the same key
+ G = branchings.MultiDiGraph_EdgeKey()
+ G.add_edge(1, 2, "A")
+ with pytest.raises(Exception, match="Key 'A' is already in use."):
+ G.add_edge(3, 4, "A")
+ # test if invalid edge key was specified
+ with pytest.raises(KeyError, match="Invalid edge key 'B'"):
+ G.remove_edge_with_key("B")
+ # test remove_edge_with_key works
+ if G.remove_edge_with_key("A"):
+ assert list(G.edges(data=True)) == []
+ # test that remove_edges_from doesn't work
+ G.add_edge(1, 3, "A")
+ with pytest.raises(NotImplementedError):
+ G.remove_edges_from([(1, 3)])
+
+
def test_edge_attribute_discard():
# Test that edge attributes are discarded if we do not specify to keep them
G = nx.Graph()