Skip to content

Commit 64a8246

Browse files
petercorkeclaude
andcommitted
test: connect() is defined once and the vertex kinds supply a single hook
Fails on main: UVertex/DVertex override connect() and there is no _attach. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent d22408d commit 64a8246

1 file changed

Lines changed: 150 additions & 0 deletions

File tree

‎tests/test_vertex_hook.py‎

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
"""
2+
``BaseVertex.connect()`` is the one public method; what differs between an
3+
undirected and a directed vertex is a single bookkeeping hook, ``_attach``,
4+
that ``connect()`` calls once the connection has been validated and the edge
5+
registered with the graph.
6+
7+
This pins the design: ``connect`` is defined once, the hook is what a vertex
8+
kind supplies, and nothing is attached when validation fails.
9+
"""
10+
11+
import unittest
12+
13+
from pgraph import DGraph, Edge, UGraph, DVertex, UVertex
14+
from pgraph.PGraph import BaseVertex
15+
16+
17+
class TestConnectIsDefinedOnce(unittest.TestCase):
18+
def test_subclasses_inherit_connect(self):
19+
self.assertIs(UVertex.connect, BaseVertex.connect)
20+
self.assertIs(DVertex.connect, BaseVertex.connect)
21+
22+
def test_subclasses_supply_the_hook(self):
23+
self.assertIsNot(UVertex._attach, BaseVertex._attach)
24+
self.assertIsNot(DVertex._attach, BaseVertex._attach)
25+
self.assertIsNot(UVertex._attach, DVertex._attach)
26+
27+
def test_base_hook_is_not_implemented(self):
28+
a, b = BaseVertex(name="a"), BaseVertex(name="b")
29+
with self.assertRaises(NotImplementedError):
30+
a._attach(Edge(a, b), b)
31+
32+
33+
class TestBareBaseVertex(unittest.TestCase):
34+
"""BaseVertex stays constructible (see policy.rst) but can never join a graph"""
35+
36+
def test_instantiable(self):
37+
v = BaseVertex([1, 2], name="v")
38+
self.assertEqual(v.name, "v")
39+
40+
def test_cannot_connect_or_be_added(self):
41+
a, b = BaseVertex(name="a"), BaseVertex(name="b")
42+
with self.assertRaises(ValueError):
43+
a.connect(b)
44+
with self.assertRaises(TypeError):
45+
UGraph().add_vertex(a)
46+
with self.assertRaises(TypeError):
47+
DGraph().add_vertex(a)
48+
49+
50+
class _Hook:
51+
Graph: type
52+
Vertex: type
53+
54+
def setUp(self):
55+
self.calls = []
56+
outer = self
57+
58+
class Recording(self.Vertex):
59+
def _attach(self, edge, dest):
60+
# the edge is already registered with the graph at this point
61+
outer.calls.append(
62+
(self, edge, dest, edge in self._graph._edgelist)
63+
)
64+
super()._attach(edge, dest)
65+
66+
self.g = self.Graph(vertex_class=Recording)
67+
self.a = self.g.add_vertex([0, 0], name="a")
68+
self.b = self.g.add_vertex([1, 0], name="b")
69+
70+
def test_attach_called_once_with_the_edge(self):
71+
e = self.a.connect(self.b, cost=3)
72+
self.assertEqual(len(self.calls), 1)
73+
v, edge, dest, registered = self.calls[0]
74+
self.assertIs(v, self.a)
75+
self.assertIs(edge, e)
76+
self.assertIs(dest, self.b)
77+
self.assertTrue(registered)
78+
79+
def test_returns_the_edge(self):
80+
e = self.a.connect(self.b, cost=3)
81+
self.assertIsInstance(e, Edge)
82+
self.assertEqual(e.cost, 3)
83+
self.assertIn(e, self.g.edges())
84+
85+
def test_nothing_attached_when_validation_fails(self):
86+
other = self.Graph().add_vertex([5, 5], name="x")
87+
with self.assertRaises(ValueError):
88+
self.a.connect(other) # different graph
89+
with self.assertRaises(ValueError):
90+
self.a.connect(self.b, edge=Edge(self.b, self.b)) # wrong endpoints
91+
ungraphed = self.Vertex([9, 9])
92+
with self.assertRaises(ValueError):
93+
self.a.connect(ungraphed)
94+
self.assertEqual(self.calls, [])
95+
self.assertEqual(self.g.ne, 0)
96+
self.assertEqual((self.a.edges(), self.b.edges()), ([], []))
97+
98+
def test_add_edge_and_edge_connect_go_through_the_hook(self):
99+
self.g.add_edge(self.a, self.b)
100+
Edge(cost=1).connect(self.a, self.b)
101+
self.assertEqual(len(self.calls), 2)
102+
103+
104+
class TestUndirectedHook(_Hook, unittest.TestCase):
105+
Graph, Vertex = UGraph, UVertex
106+
107+
def test_edge_is_held_by_both_ends(self):
108+
e = self.a.connect(self.b)
109+
self.assertEqual(self.a.edges(), [e])
110+
self.assertEqual(self.b.edges(), [e])
111+
112+
def test_self_loop_is_held_twice_by_its_vertex(self):
113+
e = self.a.connect(self.a)
114+
self.assertEqual(self.a.edges(), [e, e])
115+
self.assertEqual(self.a.degree, 2)
116+
117+
118+
class TestDirectedHook(_Hook, unittest.TestCase):
119+
Graph, Vertex = DGraph, DVertex
120+
121+
def test_edge_is_held_by_the_source_only(self):
122+
e = self.a.connect(self.b)
123+
self.assertEqual(self.a.edges(), [e])
124+
self.assertEqual(self.b.edges(), [])
125+
126+
def test_self_loop_is_held_once(self):
127+
e = self.a.connect(self.a)
128+
self.assertEqual(self.a.edges(), [e])
129+
self.assertEqual(self.a.degree, 1)
130+
131+
132+
class TestHookIsTheExtensionPoint(unittest.TestCase):
133+
def test_custom_bookkeeping(self):
134+
# a user kind that records the edge on the source only, though it is
135+
# built on the undirected vertex: only the hook has to change
136+
class OneSided(UVertex):
137+
def _attach(self, edge, dest):
138+
self._edgelist.append(edge)
139+
140+
g = UGraph(vertex_class=OneSided)
141+
a = g.add_vertex([0, 0], name="a")
142+
b = g.add_vertex([1, 0], name="b")
143+
e = a.connect(b)
144+
self.assertEqual(a.edges(), [e])
145+
self.assertEqual(b.edges(), [])
146+
self.assertEqual(g.ne, 1)
147+
148+
149+
if __name__ == "__main__":
150+
unittest.main()

0 commit comments

Comments
 (0)