Skip to content

Commit b965468

Browse files
authored
Merge pull request #53 from petercorke/test/pin-changelog-claims
tests: pin CHANGELOG claims that no test covered
2 parents 3898a89 + 4ad159a commit b965468

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

‎tests/test_components.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import signal
1919
import unittest
20+
import unittest.mock
2021
from contextlib import contextmanager
2122

2223
import numpy as np
@@ -197,6 +198,41 @@ def test_samecomponent(self):
197198
self.assertFalse(g.samecomponent(g[1], g[2]))
198199

199200

201+
class TestNotRecomputed(unittest.TestCase):
202+
"""The traversal runs once, and again only after the graph has changed"""
203+
204+
def traversals(self, g, ask):
205+
calls = []
206+
original = type(g[0]).adjacent
207+
208+
def spy(vertex, *args, **kwargs):
209+
calls.append(vertex)
210+
return original(vertex, *args, **kwargs)
211+
212+
with unittest.mock.patch.object(type(g[0]), "adjacent", spy):
213+
ask()
214+
return len(calls)
215+
216+
def check(self, cls):
217+
g = build(cls, 4, [(0, 1), (2, 3)])
218+
ask = lambda: (g.nc, g.component(g[0]), g.samecomponent(g[0], g[1]), g[2].label)
219+
220+
self.assertGreater(self.traversals(g, ask), 0) # first time: does the work
221+
self.assertEqual(self.traversals(g, ask), 0) # nothing changed: none
222+
self.assertEqual(self.traversals(g, ask), 0)
223+
224+
g.add_edge(g[1], g[2]) # a change: once more, and the answer is new
225+
self.assertGreater(self.traversals(g, ask), 0)
226+
self.assertEqual(g.nc, 1)
227+
self.assertEqual(self.traversals(g, ask), 0)
228+
229+
def test_undirected(self):
230+
self.check(UGraph)
231+
232+
def test_directed(self):
233+
self.check(DGraph)
234+
235+
200236
class TestUndirectedComponents(_Components, unittest.TestCase):
201237
cls = UGraph
202238

‎tests/test_export.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,8 @@ def test_dotfile_prints_to_console(self):
408408
g.dotfile()
409409
self.assertEqual(out.getvalue(), g.export(format="dot", costs=False))
410410
self.assertNotIn("label=", out.getvalue())
411+
# writing to the console must leave it usable (0.6.5 closed it)
412+
self.assertFalse(out.closed)
411413

412414
def test_dotfile_to_path_and_stream(self):
413415
g, *_ = line_graph(UGraph)

‎tests/test_graph.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,24 @@ def test_edge_vertices_deprecated(self):
298298
verts = e.vertices()
299299
self.assertEqual(verts, e.endpoints)
300300

301+
def test_planners_validate_start_and_goal(self):
302+
# 0.6.5 checked the type of the start twice and never the goal
303+
g = UGraph()
304+
a = g.add_vertex(coord=[0, 0], name="a")
305+
b = g.add_vertex(coord=[1, 0], name="b")
306+
g.add_edge(a, b)
307+
308+
for planner in (g.path_BFS, g.path_UCS, g.path_Astar):
309+
with self.subTest(planner=planner.__name__):
310+
with self.assertRaises(TypeError):
311+
planner(a, 42)
312+
with self.assertRaises(TypeError):
313+
planner(42, b)
314+
with self.assertRaises(KeyError):
315+
planner(a, "no such vertex")
316+
# by name and by reference both work
317+
self.assertEqual(planner("a", "b")[0], [a, b])
318+
301319
def test_edge3(self):
302320

303321
g = UGraph()

0 commit comments

Comments
 (0)