|
| 1 | +""" |
| 2 | +``graph.add_edge()`` accepts either two vertices (or names) or an ``Edge`` |
| 3 | +that already carries its vertices, mirroring ``add_vertex()`` which accepts |
| 4 | +coordinates or a vertex. |
| 5 | +""" |
| 6 | + |
| 7 | +import unittest |
| 8 | + |
| 9 | +from pgraph import DGraph, Edge, UGraph, UVertex |
| 10 | + |
| 11 | + |
| 12 | +class MyEdge(Edge): |
| 13 | + pass |
| 14 | + |
| 15 | + |
| 16 | +class _AddEdge: |
| 17 | + Graph: type |
| 18 | + |
| 19 | + def setUp(self): |
| 20 | + self.g = self.Graph() |
| 21 | + self.a = self.g.add_vertex([0, 0], name="a") |
| 22 | + self.b = self.g.add_vertex([3, 4], name="b") |
| 23 | + self.c = self.g.add_vertex([6, 8], name="c") |
| 24 | + |
| 25 | + def test_two_vertices_unchanged(self): |
| 26 | + e = self.g.add_edge(self.a, self.b) |
| 27 | + self.assertIs(type(e), Edge) |
| 28 | + self.assertEqual((e.v1, e.v2), (self.a, self.b)) |
| 29 | + self.assertEqual(e.cost, 5.0) |
| 30 | + |
| 31 | + def test_two_names_unchanged(self): |
| 32 | + e = self.g.add_edge("a", "b", cost=9) |
| 33 | + self.assertEqual((e.v1, e.v2, e.cost), (self.a, self.b, 9)) |
| 34 | + |
| 35 | + def test_edge_subclass(self): |
| 36 | + e = MyEdge(self.a, self.b, cost=2, data="hello") |
| 37 | + r = self.g.add_edge(e) |
| 38 | + |
| 39 | + self.assertIs(r, e) |
| 40 | + self.assertIsInstance(r, MyEdge) |
| 41 | + self.assertEqual(self.g.ne, 1) |
| 42 | + self.assertIn(e, self.g.edges()) |
| 43 | + self.assertIs(self.a.edgeto(self.b), e) |
| 44 | + self.assertEqual(e.data, "hello") |
| 45 | + |
| 46 | + def test_edge_cost_computed_at_construction(self): |
| 47 | + r = self.g.add_edge(MyEdge(self.a, self.b)) |
| 48 | + self.assertEqual(r.cost, 5.0) |
| 49 | + |
| 50 | + def test_edge_must_carry_vertices(self): |
| 51 | + with self.assertRaises(ValueError) as cm: |
| 52 | + self.g.add_edge(MyEdge(cost=1)) |
| 53 | + self.assertIn("edge=", str(cm.exception)) # points at the alternative |
| 54 | + with self.assertRaises(ValueError): |
| 55 | + self.g.add_edge(MyEdge(self.a)) # only one end |
| 56 | + self.assertEqual(self.g.ne, 0) |
| 57 | + |
| 58 | + def test_edge_and_second_vertex_is_an_error(self): |
| 59 | + with self.assertRaises(TypeError): |
| 60 | + self.g.add_edge(MyEdge(self.a, self.b), self.c) |
| 61 | + self.assertEqual(self.g.ne, 0) |
| 62 | + |
| 63 | + def test_edge_with_cost_or_data_kwargs_is_an_error(self): |
| 64 | + # connect() would silently ignore them when given an edge |
| 65 | + e = MyEdge(self.a, self.b) |
| 66 | + with self.assertRaises(TypeError): |
| 67 | + self.g.add_edge(e, cost=3) |
| 68 | + with self.assertRaises(TypeError): |
| 69 | + self.g.add_edge(e, data="x") |
| 70 | + self.assertEqual(self.g.ne, 0) |
| 71 | + |
| 72 | + def test_wrong_type(self): |
| 73 | + with self.assertRaises(TypeError): |
| 74 | + self.g.add_edge(42, self.b) |
| 75 | + with self.assertRaises(TypeError): |
| 76 | + self.g.add_edge(self.a, 42) |
| 77 | + |
| 78 | + def test_vertices_of_another_graph(self): |
| 79 | + h = self.Graph() |
| 80 | + x = h.add_vertex([0, 0], name="x") |
| 81 | + y = h.add_vertex([1, 1], name="y") |
| 82 | + with self.assertRaises(ValueError): |
| 83 | + self.g.add_edge(MyEdge(x, y)) |
| 84 | + with self.assertRaises(ValueError): |
| 85 | + self.g.add_edge(x, y) |
| 86 | + self.assertEqual(self.g.ne, 0) |
| 87 | + self.assertEqual(h.ne, 0) |
| 88 | + |
| 89 | + def test_edge_between_ungraphed_vertices(self): |
| 90 | + v, w = UVertex([0, 0]), UVertex([1, 1]) |
| 91 | + with self.assertRaises(ValueError): |
| 92 | + self.g.add_edge(MyEdge(v, w)) |
| 93 | + |
| 94 | + |
| 95 | +class TestUGraphAddEdge(_AddEdge, unittest.TestCase): |
| 96 | + Graph = UGraph |
| 97 | + |
| 98 | + |
| 99 | +class TestDGraphAddEdge(_AddEdge, unittest.TestCase): |
| 100 | + Graph = DGraph |
| 101 | + |
| 102 | + def test_direction_follows_edge(self): |
| 103 | + self.g.add_edge(MyEdge(self.b, self.a)) |
| 104 | + self.assertEqual(self.b.neighbours(), [self.a]) |
| 105 | + self.assertEqual(self.a.neighbours(), []) |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + unittest.main() |
0 commit comments