Skip to content

Commit e030b83

Browse files
committed
add graph cleanup
1 parent 6398d48 commit e030b83

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

pathfinding/core/graph.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,7 @@ def calc_cost(self, node_a, node_b, _weighted=False):
6262

6363
def node(self, node_id):
6464
return self.nodes[node_id]
65+
66+
def cleanup(self):
67+
for node in self.nodes.values():
68+
node.cleanup()

test/test_graph.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from unittest.mock import patch
2+
13
from pathfinding.core.graph import Graph
24
from pathfinding.finder.dijkstra import DijkstraFinder
35

@@ -33,3 +35,19 @@ def test_connected_end():
3335
finder = DijkstraFinder()
3436
path, _ = finder.find_path(graph.node(0), graph.node(2), graph)
3537
assert [n.node_id for n in path] == [0, 1, 2]
38+
39+
40+
def test_graph_cleanup():
41+
graph = Graph(edges=[[0, 1, 1], [1, 0, 1]], bi_directional=False)
42+
assert not graph.dirty
43+
44+
finder = DijkstraFinder()
45+
path, _ = finder.find_path(graph.node(0), graph.node(1), graph)
46+
assert [n.node_id for n in path] == [0, 1]
47+
48+
assert graph.dirty
49+
50+
with patch.object(graph, "cleanup", wraps=graph.cleanup) as mock_cleanup:
51+
path, _ = finder.find_path(graph.node(1), graph.node(0), graph)
52+
assert [n.node_id for n in path] == [1, 0]
53+
mock_cleanup.assert_called_once()

0 commit comments

Comments
 (0)