-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathtest_graphviz.py
149 lines (132 loc) · 4.81 KB
/
test_graphviz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import unittest
import rustworkx
from rustworkx.visualization import graphviz_draw, have_dot, is_format_supported
try:
import PIL
HAVE_PILLOW = True
except Exception:
HAVE_PILLOW = False
SAVE_IMAGES = os.getenv("RETWORKX_TEST_PRESERVE_IMAGES", None)
def _save_image(image, path):
if SAVE_IMAGES:
image.save(path)
@unittest.skipUnless(
HAVE_PILLOW and have_dot(), "pillow and graphviz are required for running these tests"
)
class TestGraphvizDraw(unittest.TestCase):
def test_draw_no_args(self):
graph = rustworkx.generators.star_graph(24)
image = graphviz_draw(graph)
self.assertIsInstance(image, PIL.Image.Image)
_save_image(image, "test_graphviz_draw.png")
def test_draw_node_attr_fn(self):
graph = rustworkx.PyGraph()
graph.add_node(
{
"color": "black",
"fillcolor": "green",
"label": "a",
"style": "filled",
}
)
graph.add_node(
{
"color": "black",
"fillcolor": "red",
"label": "a",
"style": "filled",
}
)
graph.add_edge(0, 1, dict(label="1", name="1"))
image = graphviz_draw(graph, lambda node: node)
self.assertIsInstance(image, PIL.Image.Image)
_save_image(image, "test_graphviz_draw_node_attr.png")
def test_draw_edge_attr_fn(self):
graph = rustworkx.PyGraph()
graph.add_node(
{
"color": "black",
"fillcolor": "green",
"label": "a",
"style": "filled",
}
)
graph.add_node(
{
"color": "black",
"fillcolor": "red",
"label": "a",
"style": "filled",
}
)
graph.add_edge(0, 1, dict(label="1", name="1"))
image = graphviz_draw(graph, lambda node: node, lambda edge: edge)
self.assertIsInstance(image, PIL.Image.Image)
_save_image(image, "test_graphviz_draw_edge_attr.png")
def test_draw_graph_attr(self):
graph = rustworkx.PyGraph()
graph.add_node(
{
"color": "black",
"fillcolor": "green",
"label": "a",
"style": "filled",
}
)
graph.add_node(
{
"color": "black",
"fillcolor": "red",
"label": "a",
"style": "filled",
}
)
graph.add_edge(0, 1, dict(label="1", name="1"))
graph_attr = {"bgcolor": "red"}
image = graphviz_draw(graph, lambda node: node, lambda edge: edge, graph_attr)
self.assertIsInstance(image, PIL.Image.Image)
_save_image(image, "test_graphviz_draw_graph_attr.png")
@unittest.skipUnless(
is_format_supported("jpg"), "Installed graphviz does not support jpg image format."
)
def test_image_type(self):
graph = rustworkx.directed_gnp_random_graph(50, 0.8)
image = graphviz_draw(graph, image_type="jpg")
self.assertIsInstance(image, PIL.Image.Image)
_save_image(image, "test_graphviz_draw_image_type.jpg")
def test_image_type_invalid_type(self):
graph = rustworkx.directed_gnp_random_graph(50, 0.8)
with self.assertRaises(ValueError):
graphviz_draw(graph, image_type="raw")
def test_method(self):
graph = rustworkx.directed_gnp_random_graph(50, 0.8)
image = graphviz_draw(graph, method="sfdp")
self.assertIsInstance(image, PIL.Image.Image)
_save_image(image, "test_graphviz_method.png")
def test_method_invalid_method(self):
graph = rustworkx.directed_gnp_random_graph(50, 0.8)
with self.assertRaises(ValueError):
graphviz_draw(graph, method="special")
def test_filename(self):
graph = rustworkx.generators.grid_graph(20, 20)
graphviz_draw(
graph,
filename="test_graphviz_filename.svg",
image_type="svg",
method="neato",
)
self.assertTrue(os.path.isfile("test_graphviz_filename.svg"))
if not SAVE_IMAGES:
self.addCleanup(os.remove, "test_graphviz_filename.svg")