summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOkite chimaobi Samuel <okitesamuel@gmail.com>2022-10-31 20:23:46 +0100
committerJarrod Millman <jarrod.millman@gmail.com>2022-11-01 10:28:42 -0700
commit2baa77640281f01a66e238235ed16f08b08597a7 (patch)
treeb2b2a1427e2301d9798f1b1ba676a13946b11b19
parentf9bd619f8069f1dcc0a82861cc1fd1bfa6559d0b (diff)
downloadnetworkx-2baa77640281f01a66e238235ed16f08b08597a7.tar.gz
Improve test coverage in algorithms shortest paths unweighted.py (#6121)
* improved test coverage in algorithms shortest_paths unweighted.py * improved test coverage in algorithms shortest_paths unweighted.py * improved test coverage in algorithms shortest_paths unweighted.py * improved code for bidirectional shortest path test * improved code for bidirectional shortest path test * improved code for bidirectional shortest path test * improved comments in tests * improved comments in tests
-rw-r--r--networkx/algorithms/shortest_paths/tests/test_unweighted.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/networkx/algorithms/shortest_paths/tests/test_unweighted.py b/networkx/algorithms/shortest_paths/tests/test_unweighted.py
index 96708f06..e2d99951 100644
--- a/networkx/algorithms/shortest_paths/tests/test_unweighted.py
+++ b/networkx/algorithms/shortest_paths/tests/test_unweighted.py
@@ -1,3 +1,5 @@
+import pytest
+
import networkx as nx
@@ -32,6 +34,24 @@ class TestUnweightedPath:
4, 4, 1, 12, nx.bidirectional_shortest_path(self.grid, 1, 12)
)
assert nx.bidirectional_shortest_path(self.directed_cycle, 0, 3) == [0, 1, 2, 3]
+ # test source = target
+ assert nx.bidirectional_shortest_path(self.cycle, 3, 3) == [3]
+
+ @pytest.mark.parametrize(
+ ("src", "tgt"),
+ (
+ (8, 3), # source not in graph
+ (3, 8), # target not in graph
+ (8, 10), # neither source nor target in graph
+ (8, 8), # src == tgt, neither in graph - tests order of input checks
+ ),
+ )
+ def test_bidirectional_shortest_path_src_tgt_not_in_graph(self, src, tgt):
+ with pytest.raises(
+ nx.NodeNotFound,
+ match=f"Either source {src} or target {tgt} is not in G",
+ ):
+ nx.bidirectional_shortest_path(self.cycle, src, tgt)
def test_shortest_path_length(self):
assert nx.shortest_path_length(self.cycle, 0, 3) == 3
@@ -64,6 +84,10 @@ class TestUnweightedPath:
assert p[3] == [3, 2, 1, 0]
p = nx.single_target_shortest_path(self.cycle, 0, cutoff=0)
assert p == {0: [0]}
+ # test missing targets
+ target = 8
+ with pytest.raises(nx.NodeNotFound, match=f"Target {target} not in G"):
+ nx.single_target_shortest_path(self.cycle, target)
def test_single_target_shortest_path_length(self):
pl = nx.single_target_shortest_path_length
@@ -71,6 +95,10 @@ class TestUnweightedPath:
assert dict(pl(self.cycle, 0)) == lengths
lengths = {0: 0, 1: 6, 2: 5, 3: 4, 4: 3, 5: 2, 6: 1}
assert dict(pl(self.directed_cycle, 0)) == lengths
+ # test missing targets
+ target = 8
+ with pytest.raises(nx.NodeNotFound, match=f"Target {target} is not in G"):
+ nx.single_target_shortest_path_length(self.cycle, target)
def test_all_pairs_shortest_path(self):
p = dict(nx.all_pairs_shortest_path(self.cycle))
@@ -114,3 +142,8 @@ class TestUnweightedPath:
p, s = nx.predecessor(G, 0, 3, cutoff=2, return_seen=True)
assert p == []
assert s == -1
+
+ def test_predecessor_missing_source(self):
+ source = 8
+ with pytest.raises(nx.NodeNotFound, match=f"Source {source} not in G"):
+ nx.predecessor(self.cycle, source)