Skip to content

Commit 15994d2

Browse files
petercorkeclaude
andcommitted
test: plot()/highlight_edge() arguments and unplottable graphs
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 173f548 commit 15994d2

1 file changed

Lines changed: 112 additions & 0 deletions

File tree

‎tests/test_plot_args.py‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
"""
2+
plot() / highlight_*() argument handling and the errors they give for graphs
3+
that cannot be drawn. These were found by mypy: ``text=None`` and
4+
``text=True`` crashed although the docstring documents ``None`` as "default
5+
formatting", and unplottable graphs gave a bare TypeError/IndexError.
6+
"""
7+
8+
import unittest
9+
10+
import matplotlib
11+
12+
matplotlib.use("Agg")
13+
import matplotlib.pyplot as plt
14+
15+
from pgraph import DGraph, UGraph
16+
17+
18+
class TestPlotArgs(unittest.TestCase):
19+
def setUp(self):
20+
self.g = UGraph()
21+
a = self.g.add_vertex([0, 0], name="a")
22+
b = self.g.add_vertex([1, 1], name="b")
23+
self.edge = self.g.add_edge(a, b)
24+
25+
def tearDown(self):
26+
plt.close("all")
27+
28+
def texts(self):
29+
return [t.get_text().strip() for t in plt.gca().texts]
30+
31+
def test_text_default_labels_vertices(self):
32+
self.g.plot(block=None)
33+
self.assertEqual(sorted(self.texts()), ["a", "b"])
34+
35+
def test_text_none_means_default_formatting(self):
36+
self.g.plot(text=None, block=None)
37+
self.assertEqual(sorted(self.texts()), ["a", "b"])
38+
39+
def test_text_true_means_default_formatting(self):
40+
self.g.plot(text=True, block=None)
41+
self.assertEqual(sorted(self.texts()), ["a", "b"])
42+
43+
def test_text_false_means_no_labels(self):
44+
self.g.plot(text=False, block=None)
45+
self.assertEqual(self.texts(), [])
46+
47+
def test_text_dict_is_the_format(self):
48+
self.g.plot(text={"color": "r"}, block=None)
49+
self.assertEqual({t.get_color() for t in plt.gca().texts}, {"r"})
50+
51+
def test_vopt_eopt_none(self):
52+
self.g.plot(vopt=None, eopt=None, block=None)
53+
54+
def test_options_are_not_shared_between_calls(self):
55+
# a mutable default argument must not accumulate state
56+
self.g.plot(vopt={"markersize": 3}, block=None)
57+
plt.close("all")
58+
self.g.plot(block=None)
59+
sizes = {line.get_markersize() for line in plt.gca().lines}
60+
self.assertNotIn(3, sizes)
61+
62+
def test_3d_text_none(self):
63+
g = UGraph()
64+
a = g.add_vertex([0, 0, 0], name="a")
65+
b = g.add_vertex([1, 1, 1], name="b")
66+
g.add_edge(a, b)
67+
g.plot(text=None, block=None)
68+
self.assertEqual(sorted(self.texts()), ["a", "b"])
69+
70+
def test_3d_vertices_plotted_at_their_z(self):
71+
# without component colouring the vertex marker dropped z (plotted at 0)
72+
g = UGraph()
73+
a = g.add_vertex([0, 0, 5], name="a")
74+
b = g.add_vertex([1, 1, 7], name="b")
75+
g.add_edge(a, b)
76+
g.plot(colorcomponents=False, block=None)
77+
zs = set()
78+
for line in plt.gca().lines:
79+
x, y, z = line.get_data_3d()
80+
if len(x) == 1: # a vertex marker, not an edge
81+
zs.add(float(z[0]))
82+
self.assertEqual(zs, {5.0, 7.0})
83+
84+
def test_plot_graph_without_coordinates(self):
85+
g = UGraph()
86+
g.add_edge(g.add_vertex(), g.add_vertex())
87+
with self.assertRaises(ValueError):
88+
g.plot(block=None)
89+
90+
def test_plot_graph_with_one_coordinate(self):
91+
g = UGraph()
92+
g.add_vertex([0])
93+
with self.assertRaises(ValueError):
94+
g.plot(block=None)
95+
96+
def test_plot_empty_graph(self):
97+
with self.assertRaises(ValueError):
98+
DGraph().plot(block=None)
99+
100+
def test_highlight_edge(self):
101+
self.g.plot(block=None)
102+
self.g.highlight_edge(self.edge)
103+
104+
def test_highlight_removed_edge(self):
105+
self.g.plot(block=None)
106+
self.g.remove_edge(self.edge)
107+
with self.assertRaises(ValueError):
108+
self.g.highlight_edge(self.edge)
109+
110+
111+
if __name__ == "__main__":
112+
unittest.main()

0 commit comments

Comments
 (0)