Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions tests/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import signal
import unittest
import unittest.mock
from contextlib import contextmanager

import numpy as np
Expand Down Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading