diff --git a/tests/test_components.py b/tests/test_components.py index e37e10b9..e5b6138e 100644 --- a/tests/test_components.py +++ b/tests/test_components.py @@ -17,6 +17,7 @@ import signal import unittest +import unittest.mock from contextlib import contextmanager import numpy as np @@ -197,6 +198,41 @@ def test_samecomponent(self): self.assertFalse(g.samecomponent(g[1], g[2])) +class TestNotRecomputed(unittest.TestCase): + """The traversal runs once, and again only after the graph has changed""" + + def traversals(self, g, ask): + calls = [] + original = type(g[0]).adjacent + + def spy(vertex, *args, **kwargs): + calls.append(vertex) + return original(vertex, *args, **kwargs) + + with unittest.mock.patch.object(type(g[0]), "adjacent", spy): + ask() + return len(calls) + + def check(self, cls): + g = build(cls, 4, [(0, 1), (2, 3)]) + ask = lambda: (g.nc, g.component(g[0]), g.samecomponent(g[0], g[1]), g[2].label) + + self.assertGreater(self.traversals(g, ask), 0) # first time: does the work + self.assertEqual(self.traversals(g, ask), 0) # nothing changed: none + self.assertEqual(self.traversals(g, ask), 0) + + g.add_edge(g[1], g[2]) # a change: once more, and the answer is new + self.assertGreater(self.traversals(g, ask), 0) + self.assertEqual(g.nc, 1) + self.assertEqual(self.traversals(g, ask), 0) + + def test_undirected(self): + self.check(UGraph) + + def test_directed(self): + self.check(DGraph) + + class TestUndirectedComponents(_Components, unittest.TestCase): cls = UGraph diff --git a/tests/test_export.py b/tests/test_export.py index 52df2ec2..8afec4c5 100644 --- a/tests/test_export.py +++ b/tests/test_export.py @@ -408,6 +408,8 @@ def test_dotfile_prints_to_console(self): g.dotfile() self.assertEqual(out.getvalue(), g.export(format="dot", costs=False)) self.assertNotIn("label=", out.getvalue()) + # writing to the console must leave it usable (0.6.5 closed it) + self.assertFalse(out.closed) def test_dotfile_to_path_and_stream(self): g, *_ = line_graph(UGraph) diff --git a/tests/test_graph.py b/tests/test_graph.py index f8d6204d..c0249c25 100644 --- a/tests/test_graph.py +++ b/tests/test_graph.py @@ -298,6 +298,24 @@ def test_edge_vertices_deprecated(self): verts = e.vertices() self.assertEqual(verts, e.endpoints) + def test_planners_validate_start_and_goal(self): + # 0.6.5 checked the type of the start twice and never the goal + g = UGraph() + a = g.add_vertex(coord=[0, 0], name="a") + b = g.add_vertex(coord=[1, 0], name="b") + g.add_edge(a, b) + + for planner in (g.path_BFS, g.path_UCS, g.path_Astar): + with self.subTest(planner=planner.__name__): + with self.assertRaises(TypeError): + planner(a, 42) + with self.assertRaises(TypeError): + planner(42, b) + with self.assertRaises(KeyError): + planner(a, "no such vertex") + # by name and by reference both work + self.assertEqual(planner("a", "b")[0], [a, b]) + def test_edge3(self): g = UGraph()