|
| 1 | +""" |
| 2 | +A graph can be told which vertex and edge classes it builds, so user |
| 3 | +subclasses never have to be constructed by hand:: |
| 4 | +
|
| 5 | + g = UGraph(vertex_class=MyVertex, edge_class=MyEdge) |
| 6 | + g.add_vertex([0, 0]) # a MyVertex |
| 7 | + g.add_edge(v1, v2) # a MyEdge |
| 8 | +""" |
| 9 | + |
| 10 | +import unittest |
| 11 | + |
| 12 | +import numpy as np |
| 13 | + |
| 14 | +from pgraph import DGraph, DVertex, Edge, UGraph, UVertex |
| 15 | + |
| 16 | + |
| 17 | +class MyEdge(Edge): |
| 18 | + def __init__(self, *args, **kwargs): |
| 19 | + super().__init__(*args, **kwargs) |
| 20 | + self.traversals = 0 |
| 21 | + |
| 22 | + |
| 23 | +class _Classes: |
| 24 | + Graph: type |
| 25 | + Vertex: type # UVertex or DVertex |
| 26 | + OtherVertex: type # the wrong kind for this graph |
| 27 | + |
| 28 | + def setUp(self): |
| 29 | + outer = self |
| 30 | + |
| 31 | + class MyVertex(self.Vertex): |
| 32 | + def __init__(self, *args, **kwargs): |
| 33 | + super().__init__(*args, **kwargs) |
| 34 | + self.visited = False |
| 35 | + |
| 36 | + self.MyVertex = MyVertex |
| 37 | + self.g = self.Graph(vertex_class=MyVertex, edge_class=MyEdge) |
| 38 | + |
| 39 | + # ------------------------------------------------------------------ # |
| 40 | + |
| 41 | + def test_defaults(self): |
| 42 | + g = self.Graph() |
| 43 | + self.assertIs(g.vertex_class, self.Vertex) |
| 44 | + self.assertIs(g.edge_class, Edge) |
| 45 | + |
| 46 | + v = g.add_vertex([0, 0]) |
| 47 | + self.assertIs(type(v), self.Vertex) |
| 48 | + self.assertIs(type(g.add_edge(v, g.add_vertex([1, 1]))), Edge) |
| 49 | + |
| 50 | + def test_properties(self): |
| 51 | + self.assertIs(self.g.vertex_class, self.MyVertex) |
| 52 | + self.assertIs(self.g.edge_class, MyEdge) |
| 53 | + |
| 54 | + def test_add_vertex_builds_vertex_class(self): |
| 55 | + v = self.g.add_vertex([1, 2], name="p") |
| 56 | + self.assertIs(type(v), self.MyVertex) |
| 57 | + self.assertFalse(v.visited) # subclass __init__ ran |
| 58 | + self.assertEqual(v.name, "p") |
| 59 | + np.testing.assert_array_equal(v.coord, [1, 2]) |
| 60 | + |
| 61 | + w = self.g.add_vertex() |
| 62 | + self.assertIs(type(w), self.MyVertex) |
| 63 | + self.assertEqual(w.name, "#1") |
| 64 | + |
| 65 | + def test_add_edge_builds_edge_class(self): |
| 66 | + a = self.g.add_vertex([0, 0]) |
| 67 | + b = self.g.add_vertex([3, 4]) |
| 68 | + |
| 69 | + e = self.g.add_edge(a, b) |
| 70 | + self.assertIs(type(e), MyEdge) |
| 71 | + self.assertEqual(e.cost, 5.0) # still computed from the metric |
| 72 | + self.assertEqual(e.traversals, 0) |
| 73 | + |
| 74 | + e2 = a.connect(b, cost=9, data="road") |
| 75 | + self.assertIs(type(e2), MyEdge) |
| 76 | + self.assertEqual((e2.cost, e2.data), (9, "road")) |
| 77 | + |
| 78 | + self.assertTrue(all(type(x) is MyEdge for x in self.g.edges())) |
| 79 | + |
| 80 | + def test_add_vertex_accepts_instances_of_vertex_class(self): |
| 81 | + v = self.MyVertex([5, 5], name="mine") |
| 82 | + self.assertIs(self.g.add_vertex(v), v) |
| 83 | + |
| 84 | + def test_add_vertex_rejects_plain_vertex_of_right_kind(self): |
| 85 | + # the graph was configured to hold MyVertex, not just any UVertex/DVertex |
| 86 | + with self.assertRaises(TypeError): |
| 87 | + self.g.add_vertex(self.Vertex([5, 5])) |
| 88 | + self.assertEqual(self.g.n, 0) |
| 89 | + |
| 90 | + def test_add_vertex_rejects_wrong_kind(self): |
| 91 | + with self.assertRaises(TypeError): |
| 92 | + self.g.add_vertex(self.OtherVertex([5, 5])) |
| 93 | + |
| 94 | + def test_connect_rejects_edge_of_wrong_class(self): |
| 95 | + a = self.g.add_vertex([0, 0]) |
| 96 | + b = self.g.add_vertex([1, 1]) |
| 97 | + with self.assertRaises(TypeError): |
| 98 | + a.connect(b, edge=Edge(cost=1)) |
| 99 | + self.assertEqual(self.g.ne, 0) |
| 100 | + |
| 101 | + mine = MyEdge(cost=1) |
| 102 | + self.assertIs(a.connect(b, edge=mine), mine) |
| 103 | + |
| 104 | + def test_default_graph_still_accepts_subclasses(self): |
| 105 | + g = self.Graph() |
| 106 | + self.assertIsInstance(g.add_vertex(self.MyVertex([0, 0])), self.MyVertex) |
| 107 | + a, b = g[0], g.add_vertex([1, 1]) |
| 108 | + self.assertIsInstance(g.add_edge(MyEdge(a, b)), MyEdge) |
| 109 | + |
| 110 | + def test_bad_vertex_class(self): |
| 111 | + for bad in (self.OtherVertex, int, "UVertex", self.Vertex([0, 0]), Edge): |
| 112 | + with self.subTest(bad=bad): |
| 113 | + with self.assertRaises(TypeError): |
| 114 | + self.Graph(vertex_class=bad) |
| 115 | + |
| 116 | + def test_bad_edge_class(self): |
| 117 | + for bad in (UVertex, int, "Edge", Edge(cost=1)): |
| 118 | + with self.subTest(bad=bad): |
| 119 | + with self.assertRaises(TypeError): |
| 120 | + self.Graph(edge_class=bad) |
| 121 | + |
| 122 | + def test_copy_keeps_classes(self): |
| 123 | + a = self.g.add_vertex([0, 0]) |
| 124 | + b = self.g.add_vertex([1, 1]) |
| 125 | + self.g.add_edge(a, b) |
| 126 | + |
| 127 | + h = self.g.copy() |
| 128 | + self.assertIs(h.vertex_class, self.MyVertex) |
| 129 | + self.assertIs(h.edge_class, MyEdge) |
| 130 | + self.assertIs(type(h[0]), self.MyVertex) |
| 131 | + c = h.add_vertex([2, 2]) |
| 132 | + self.assertIs(type(c), self.MyVertex) |
| 133 | + self.assertIs(type(h.add_edge(h[0], c)), MyEdge) |
| 134 | + |
| 135 | + def test_constructors_forward_classes(self): |
| 136 | + g = self.Graph.Dict( |
| 137 | + {"b": "a", "c": "a"}, vertex_class=self.MyVertex, edge_class=MyEdge |
| 138 | + ) |
| 139 | + self.assertEqual(g.n, 3) |
| 140 | + self.assertTrue(all(type(v) is self.MyVertex for v in g)) |
| 141 | + self.assertTrue(all(type(e) is MyEdge for e in g.edges())) |
| 142 | + |
| 143 | + A = np.array([[0, 1], [1, 0]]) |
| 144 | + g = self.Graph.Adjacency( |
| 145 | + A, vertex_class=self.MyVertex, edge_class=MyEdge |
| 146 | + ) |
| 147 | + self.assertTrue(all(type(v) is self.MyVertex for v in g)) |
| 148 | + self.assertTrue(all(type(e) is MyEdge for e in g.edges())) |
| 149 | + |
| 150 | + def test_planning_unaffected(self): |
| 151 | + g = self.g |
| 152 | + v = [g.add_vertex([i, 0], name=n) for i, n in enumerate("abc")] |
| 153 | + g.add_edge(v[0], v[1]) |
| 154 | + g.add_edge(v[1], v[2]) |
| 155 | + path, cost, _ = g.path_UCS(v[0], v[2]) |
| 156 | + self.assertEqual([p.name for p in path], ["a", "b", "c"]) |
| 157 | + self.assertAlmostEqual(cost, 2.0) |
| 158 | + |
| 159 | + |
| 160 | +class TestUGraphClasses(_Classes, unittest.TestCase): |
| 161 | + Graph = UGraph |
| 162 | + Vertex = UVertex |
| 163 | + OtherVertex = DVertex |
| 164 | + |
| 165 | + |
| 166 | +class TestDGraphClasses(_Classes, unittest.TestCase): |
| 167 | + Graph = DGraph |
| 168 | + Vertex = DVertex |
| 169 | + OtherVertex = UVertex |
| 170 | + |
| 171 | + |
| 172 | +if __name__ == "__main__": |
| 173 | + unittest.main() |
0 commit comments