Skip to content

Commit 190b21e

Browse files
petercorkeclaude
andcommitted
test: Edge.v1 and Edge.v2 are read-only; the library still binds them
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent f3e1e04 commit 190b21e

1 file changed

Lines changed: 121 additions & 0 deletions

File tree

‎tests/test_edge_endpoints.py‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
"""
2+
``Edge.v1`` and ``Edge.v2`` are read-only.
3+
4+
They used to be plain attributes, so a caller could write ``e.v1 = None`` on an
5+
edge that was in a graph and leave it wedged: ``remove_edge`` and
6+
``remove_vertex`` then raised and the edge could not be removed. Only the
7+
library binds an edge to its vertices -- the constructor, ``connect()`` and
8+
``remove_edge()`` -- so an edge in a graph always has both of its ends.
9+
"""
10+
11+
import copy
12+
import unittest
13+
14+
from pgraph import DGraph, Edge, UGraph
15+
16+
17+
class TestReadOnly(unittest.TestCase):
18+
def setUp(self):
19+
self.g = UGraph()
20+
self.a = self.g.add_vertex([0, 0], name="a")
21+
self.b = self.g.add_vertex([1, 0], name="b")
22+
self.c = self.g.add_vertex([2, 0], name="c")
23+
self.e = self.a.connect(self.b)
24+
25+
def test_cannot_assign(self):
26+
for name, value in (("v1", None), ("v1", self.c), ("v2", None), ("v2", self.c)):
27+
with self.subTest(name=name, value=value):
28+
with self.assertRaises(AttributeError) as cm:
29+
setattr(self.e, name, value)
30+
# the message says what to do instead
31+
self.assertIn("connect", str(cm.exception))
32+
33+
def test_cannot_delete(self):
34+
with self.assertRaises(AttributeError):
35+
del self.e.v1
36+
37+
def test_failed_assignment_changes_nothing(self):
38+
with self.assertRaises(AttributeError):
39+
self.e.v1 = self.c
40+
self.assertIs(self.e.v1, self.a)
41+
self.assertIs(self.e.v2, self.b)
42+
self.assertEqual(self.a.edges(), [self.e])
43+
self.assertEqual(self.c.edges(), [])
44+
45+
def test_the_graph_can_always_remove_its_edges(self):
46+
for name in ("v1", "v2"):
47+
try:
48+
setattr(self.e, name, None)
49+
except AttributeError:
50+
pass
51+
self.g.remove_edge(self.e) # not wedged
52+
self.assertEqual(self.g.ne, 0)
53+
self.g.remove_vertex(self.a)
54+
self.assertEqual(self.g.n, 2)
55+
56+
57+
class TestLibraryStillBindsThem(unittest.TestCase):
58+
def test_constructor(self):
59+
g = DGraph()
60+
a, b = g.add_vertex(name="a"), g.add_vertex(name="b")
61+
e = Edge(a, b, cost=3)
62+
self.assertIs(e.v1, a)
63+
self.assertIs(e.v2, b)
64+
65+
def test_standalone_edge_has_no_vertices(self):
66+
e = Edge(cost=1)
67+
self.assertIsNone(e.v1)
68+
self.assertIsNone(e.v2)
69+
70+
def test_connect_binds_a_bare_edge(self):
71+
g = UGraph()
72+
a, b = g.add_vertex(name="a"), g.add_vertex(name="b")
73+
e = Edge(cost=1)
74+
a.connect(b, edge=e)
75+
self.assertEqual((e.v1, e.v2), (a, b))
76+
77+
def test_edge_connect_binds(self):
78+
g = UGraph()
79+
a, b = g.add_vertex(name="a"), g.add_vertex(name="b")
80+
e = Edge(cost=1)
81+
e.connect(a, b)
82+
self.assertEqual((e.v1, e.v2), (a, b))
83+
84+
def test_removal_unlinks_and_the_edge_can_be_reused(self):
85+
g = UGraph()
86+
a, b, c = (g.add_vertex(name=n) for n in "abc")
87+
e = a.connect(b)
88+
g.remove_edge(e)
89+
self.assertIsNone(e.v1)
90+
self.assertIsNone(e.v2)
91+
with self.assertRaises(ValueError):
92+
g.add_edge(e) # unlinked, so add_edge(edge) refuses it
93+
b.connect(c, edge=e) # but connect() re-binds it
94+
self.assertEqual((e.v1, e.v2), (b, c))
95+
self.assertEqual(g.ne, 1)
96+
97+
def test_subclass_that_calls_super(self):
98+
class Observation(Edge):
99+
def __init__(self, v1, v2, uv):
100+
super().__init__(v1, v2, cost=0)
101+
self.uv = uv
102+
103+
g = UGraph()
104+
a, b = g.add_vertex(name="a"), g.add_vertex(name="b")
105+
o = Observation(a, b, (1, 2))
106+
a.connect(b, edge=o)
107+
self.assertEqual((o.v1, o.v2, o.uv), (a, b, (1, 2)))
108+
109+
def test_repr_and_copies_keep_the_endpoints(self):
110+
g = UGraph()
111+
a, b = g.add_vertex(name="a"), g.add_vertex(name="b")
112+
e = a.connect(b, cost=2)
113+
self.assertIn("a", str(e))
114+
for dup in (copy.copy(e), copy.deepcopy(e)):
115+
self.assertEqual(dup.cost, 2)
116+
self.assertIsNotNone(dup.v1)
117+
self.assertIsNotNone(dup.v2)
118+
119+
120+
if __name__ == "__main__":
121+
unittest.main()

0 commit comments

Comments
 (0)