exploration.tests.test_UniqueExitsGraph

Authors: Peter Mawhorter Consulted: Date: 2022-3-11 Purpose: Tests for the UniqueExitsGraph class.

  1"""
  2Authors: Peter Mawhorter
  3Consulted:
  4Date: 2022-3-11
  5Purpose: Tests for the UniqueExitsGraph class.
  6"""
  7
  8import pytest
  9
 10import networkx as nx # type: ignore
 11
 12from .. import graphs
 13
 14
 15def test_create():
 16    "Tests graph creation."
 17    _ = graphs.UniqueExitsGraph()
 18    assert (True)
 19
 20
 21def test_notImplemented():
 22    "Tests that not implemented things aren't."
 23    g = graphs.UniqueExitsGraph()
 24    with pytest.raises(NotImplementedError):
 25        assert (g.new_edge_key(1, 2) is not None)
 26
 27    with pytest.raises(NotImplementedError):
 28        assert (g.reverse() is not None)
 29
 30
 31def test_addNode():
 32    "Tests adding a node."
 33    g = graphs.UniqueExitsGraph()
 34    g.add_node('A')
 35    assert (list(g) == ['A'])
 36    with pytest.raises(KeyError):
 37        assert (g['B'])
 38
 39
 40def test_edges():
 41    "Tests adding multiple edges between nodes."
 42    g = graphs.UniqueExitsGraph()
 43    g.add_node('A')
 44    g.add_node('B')
 45    g.add_node('C')
 46    g.add_edge('A', 'B', 0)
 47    g.add_edge('A', 'B', 1)
 48    g.add_edge('B', 'A', 'hi')
 49    assert (g['A']['B'] == {0: {}, 1: {}})
 50    assert (g['B']['A'] == {'hi': {}})
 51    with pytest.raises(KeyError):
 52        assert (g['A']['C'])
 53
 54
 55def test_destinations():
 56    "Tests getting destinations by edge name."
 57    g = graphs.UniqueExitsGraph()
 58    g.add_node('A')
 59    g.add_node('B')
 60    g.add_edge('A', 'B', 0)
 61    g.add_edge('A', 'B', 1)
 62    g.add_edge('B', 'A', 'hi')
 63    assert (g.destination('A', 0) == 'B')
 64    assert (g.destination('A', 1) == 'B')
 65    assert (g.destination('B', 'hi') == 'A')
 66    with pytest.raises(KeyError):
 67        assert (g.destination('A', 2))
 68    with pytest.raises(KeyError):
 69        assert (g.destination('B', 0))
 70    assert (g.getDestination('A', 0) == 'B')
 71    assert (g.getDestination('A', 1) == 'B')
 72    assert (g.getDestination('B', 'hi') == 'A')
 73    assert (g.getDestination('A', 2) is None)
 74    assert (g.getDestination('B', 0) is None)
 75    assert (g.destinationsFrom('A') == {0: 'B', 1: 'B'})
 76    assert (g.destinationsFrom('B') == {'hi': 'A'})
 77
 78
 79def test_clear():
 80    "Tests clearing a graph."
 81    g = graphs.UniqueExitsGraph()
 82    g.add_node('A')
 83    g.add_node('B')
 84    g.add_edge('A', 'B', 0)
 85    g.add_edge('A', 'B', 1)
 86    g.add_edge('B', 'A', 'hi')
 87
 88    assert (len(g) == 2)
 89    g.clear_edges()
 90    assert (len(g) == 2)
 91    assert (g['A'] == {})
 92    assert (g['B'] == {})
 93    assert (g.getDestination('A', 0) is None)
 94    assert (g.getDestination('B', 'hi') is None)
 95    assert (g.destinationsFrom('A') == {})
 96    assert (g.destinationsFrom('B') == {})
 97
 98    g.clear()
 99    assert (len(g) == 0)
100    with pytest.raises(KeyError):
101        assert (g['A'])
102    with pytest.raises(KeyError):
103        assert (g['B'])
104
105
106def test_remove():
107    "Tests removing edges by destination + name and by just name."
108    g = graphs.UniqueExitsGraph()
109    g.add_node('A')
110    g.add_node('B')
111    g.add_edge('A', 'B', 0)
112    g.add_edge('A', 'B', 1)
113    g.add_edge('A', 'B', 2)
114    g.add_edge('A', 'B', 3)
115    g.add_edge('B', 'A', 0)
116    g.add_edge('B', 'A', 1)
117    g.add_edge('B', 'A', 'hi')
118
119    with pytest.raises(TypeError):
120        g.remove_edge('A', 'B')
121
122    with pytest.raises(nx.NetworkXError):
123        g.remove_edge('A', 'B', 'hi')
124
125    with pytest.raises(nx.NetworkXError):
126        g.remove_edge('B', 'A', 2)
127
128    assert (g.getDestination('A', 3) == 'B')
129    g.remove_edge('A', 'B', 3)
130    assert (g.getDestination('A', 3) is None)
131
132    with pytest.raises(ValueError):
133        assert (g.remove_edges_from([('A', 'B')]) is None)
134
135    assert (g.getDestination('A', 2) == 'B')
136    assert (g.getDestination('B', 1) == 'A')
137    assert (g._byEdge['A'][2] == 'B')
138    g.remove_edges_from([('A', 'B', 2), ('B', 'A', 1, {})])
139    assert (g.getDestination('A', 2) is None)
140    assert (g.getDestination('B', 1) is None)
141
142    assert (g.getDestination('A', 0) == 'B')
143    g.removeEdgeByKey('A', 0)
144    assert (g.getDestination('A', 0) is None)
145
146    assert (g.getDestination('B', 0) == 'A')
147    g.removeEdgeByKey('B', 0)
148    assert (g.getDestination('B', 0) is None)
149
150    assert (g.getDestination('B', 'hi') == 'A')
151    g.removeEdgesByKey([('B', 'hi'), ('A', 1)]) # spurious target ignored
152    assert (g.getDestination('B', 'hi') is None)
def test_create():
16def test_create():
17    "Tests graph creation."
18    _ = graphs.UniqueExitsGraph()
19    assert (True)

Tests graph creation.

def test_notImplemented():
22def test_notImplemented():
23    "Tests that not implemented things aren't."
24    g = graphs.UniqueExitsGraph()
25    with pytest.raises(NotImplementedError):
26        assert (g.new_edge_key(1, 2) is not None)
27
28    with pytest.raises(NotImplementedError):
29        assert (g.reverse() is not None)

Tests that not implemented things aren't.

def test_addNode():
32def test_addNode():
33    "Tests adding a node."
34    g = graphs.UniqueExitsGraph()
35    g.add_node('A')
36    assert (list(g) == ['A'])
37    with pytest.raises(KeyError):
38        assert (g['B'])

Tests adding a node.

def test_edges():
41def test_edges():
42    "Tests adding multiple edges between nodes."
43    g = graphs.UniqueExitsGraph()
44    g.add_node('A')
45    g.add_node('B')
46    g.add_node('C')
47    g.add_edge('A', 'B', 0)
48    g.add_edge('A', 'B', 1)
49    g.add_edge('B', 'A', 'hi')
50    assert (g['A']['B'] == {0: {}, 1: {}})
51    assert (g['B']['A'] == {'hi': {}})
52    with pytest.raises(KeyError):
53        assert (g['A']['C'])

Tests adding multiple edges between nodes.

def test_destinations():
56def test_destinations():
57    "Tests getting destinations by edge name."
58    g = graphs.UniqueExitsGraph()
59    g.add_node('A')
60    g.add_node('B')
61    g.add_edge('A', 'B', 0)
62    g.add_edge('A', 'B', 1)
63    g.add_edge('B', 'A', 'hi')
64    assert (g.destination('A', 0) == 'B')
65    assert (g.destination('A', 1) == 'B')
66    assert (g.destination('B', 'hi') == 'A')
67    with pytest.raises(KeyError):
68        assert (g.destination('A', 2))
69    with pytest.raises(KeyError):
70        assert (g.destination('B', 0))
71    assert (g.getDestination('A', 0) == 'B')
72    assert (g.getDestination('A', 1) == 'B')
73    assert (g.getDestination('B', 'hi') == 'A')
74    assert (g.getDestination('A', 2) is None)
75    assert (g.getDestination('B', 0) is None)
76    assert (g.destinationsFrom('A') == {0: 'B', 1: 'B'})
77    assert (g.destinationsFrom('B') == {'hi': 'A'})

Tests getting destinations by edge name.

def test_clear():
 80def test_clear():
 81    "Tests clearing a graph."
 82    g = graphs.UniqueExitsGraph()
 83    g.add_node('A')
 84    g.add_node('B')
 85    g.add_edge('A', 'B', 0)
 86    g.add_edge('A', 'B', 1)
 87    g.add_edge('B', 'A', 'hi')
 88
 89    assert (len(g) == 2)
 90    g.clear_edges()
 91    assert (len(g) == 2)
 92    assert (g['A'] == {})
 93    assert (g['B'] == {})
 94    assert (g.getDestination('A', 0) is None)
 95    assert (g.getDestination('B', 'hi') is None)
 96    assert (g.destinationsFrom('A') == {})
 97    assert (g.destinationsFrom('B') == {})
 98
 99    g.clear()
100    assert (len(g) == 0)
101    with pytest.raises(KeyError):
102        assert (g['A'])
103    with pytest.raises(KeyError):
104        assert (g['B'])

Tests clearing a graph.

def test_remove():
107def test_remove():
108    "Tests removing edges by destination + name and by just name."
109    g = graphs.UniqueExitsGraph()
110    g.add_node('A')
111    g.add_node('B')
112    g.add_edge('A', 'B', 0)
113    g.add_edge('A', 'B', 1)
114    g.add_edge('A', 'B', 2)
115    g.add_edge('A', 'B', 3)
116    g.add_edge('B', 'A', 0)
117    g.add_edge('B', 'A', 1)
118    g.add_edge('B', 'A', 'hi')
119
120    with pytest.raises(TypeError):
121        g.remove_edge('A', 'B')
122
123    with pytest.raises(nx.NetworkXError):
124        g.remove_edge('A', 'B', 'hi')
125
126    with pytest.raises(nx.NetworkXError):
127        g.remove_edge('B', 'A', 2)
128
129    assert (g.getDestination('A', 3) == 'B')
130    g.remove_edge('A', 'B', 3)
131    assert (g.getDestination('A', 3) is None)
132
133    with pytest.raises(ValueError):
134        assert (g.remove_edges_from([('A', 'B')]) is None)
135
136    assert (g.getDestination('A', 2) == 'B')
137    assert (g.getDestination('B', 1) == 'A')
138    assert (g._byEdge['A'][2] == 'B')
139    g.remove_edges_from([('A', 'B', 2), ('B', 'A', 1, {})])
140    assert (g.getDestination('A', 2) is None)
141    assert (g.getDestination('B', 1) is None)
142
143    assert (g.getDestination('A', 0) == 'B')
144    g.removeEdgeByKey('A', 0)
145    assert (g.getDestination('A', 0) is None)
146
147    assert (g.getDestination('B', 0) == 'A')
148    g.removeEdgeByKey('B', 0)
149    assert (g.getDestination('B', 0) is None)
150
151    assert (g.getDestination('B', 'hi') == 'A')
152    g.removeEdgesByKey([('B', 'hi'), ('A', 1)]) # spurious target ignored
153    assert (g.getDestination('B', 'hi') is None)

Tests removing edges by destination + name and by just name.