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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
# coding=iso-8859-1
# TODO:
# -test graph generation APIs (from adjacency, etc..)
# -test del_node, del_edge methods
# -test Common.set method
from __future__ import division, print_function
import os
try:
from hashlib import sha256
except ImportError:
import sha
sha256 = sha.new
import subprocess
import sys
import pydotplus
import unittest
PY3 = not sys.version_info < (3, 0, 0)
if PY3:
NULL_SEP = b''
xrange = range
else:
NULL_SEP = ''
bytes = str
DOT_BINARY_PATH = pydotplus.find_graphviz()['dot']
TEST_DIR = './'
REGRESSION_TESTS_DIR = os.path.join(TEST_DIR, 'graphs')
MY_REGRESSION_TESTS_DIR = os.path.join(TEST_DIR, 'my_tests')
class TestGraphAPI(unittest.TestCase):
def setUp(self):
self._reset_graphs()
def _reset_graphs(self):
self.graph_directed = pydotplus.Graph(
'testgraph', graph_type='digraph'
)
def test_keep_graph_type(self):
g = pydotplus.Dot(graph_name='Test', graph_type='graph')
self.assertEqual(g.get_type(), 'graph')
g = pydotplus.Dot(graph_name='Test', graph_type='digraph')
self.assertEqual(g.get_type(), 'digraph')
def test_add_style(self):
node = pydotplus.Node('mynode')
node.add_style('abc')
self.assertEqual(node.get_style(), 'abc')
node.add_style('def')
self.assertEqual(node.get_style(), 'abc,def')
node.add_style('ghi')
self.assertEqual(node.get_style(), 'abc,def,ghi')
def test_create_simple_graph_with_node(self):
g = pydotplus.Dot()
g.set_type('digraph')
node = pydotplus.Node('legend')
node.set("shape", 'box')
g.add_node(node)
node.set('label', 'mine')
self.assertEqual(
g.to_string(),
'digraph G {\nlegend [label=mine, shape=box];\n}\n'
)
def test_attribute_with_implicit_value(self):
d = 'digraph {\na -> b[label="hi", decorate];\n}'
g = pydotplus.graph_from_dot_data(d)
attrs = g.get_edges()[0].get_attributes()
self.assertEqual('decorate' in attrs, True)
def test_subgraphs(self):
g = pydotplus.Graph()
s = pydotplus.Subgraph("foo")
self.assertEqual(g.get_subgraphs(), [])
self.assertEqual(g.get_subgraph_list(), [])
g.add_subgraph(s)
self.assertEqual(g.get_subgraphs()[0].get_name(), s.get_name())
self.assertEqual(g.get_subgraph_list()[0].get_name(), s.get_name())
def test_graph_pickling(self):
import pickle
g = pydotplus.Graph()
s = pydotplus.Subgraph("foo")
g.add_subgraph(s)
g.add_edge(pydotplus.Edge('A', 'B'))
g.add_edge(pydotplus.Edge('A', 'C'))
g.add_edge(pydotplus.Edge(('D', 'E')))
g.add_node(pydotplus.Node('node!'))
self.assertEqual(type(pickle.dumps(g)), bytes)
def test_unicode_ids(self):
node1 = '"aánñoöüé€"'
node2 = '"îôø®çßΩ"'
g = pydotplus.Dot()
g.set_charset('latin1')
g.add_node(pydotplus.Node(node1))
g.add_node(pydotplus.Node(node2))
g.add_edge(pydotplus.Edge(node1, node2))
self.assertEqual(g.get_node(node1)[0].get_name(), node1)
self.assertEqual(g.get_node(node2)[0].get_name(), node2)
self.assertEqual(g.get_edges()[0].get_source(), node1)
self.assertEqual(g.get_edges()[0].get_destination(), node2)
g2 = pydotplus.graph_from_dot_data(g.to_string())
self.assertEqual(g2.get_node(node1)[0].get_name(), node1)
self.assertEqual(g2.get_node(node2)[0].get_name(), node2)
self.assertEqual(g2.get_edges()[0].get_source(), node1)
self.assertEqual(g2.get_edges()[0].get_destination(), node2)
def test_graph_with_shapefiles(self):
shapefile_dir = os.path.join(TEST_DIR, 'from-past-to-future')
dot_file = os.path.join(shapefile_dir, 'from-past-to-future.dot')
pngs = [
os.path.join(shapefile_dir, fname)
for fname in os.listdir(shapefile_dir)
if fname.endswith('.png')
]
f = open(dot_file, 'rt')
graph_data = f.read()
f.close()
g = pydotplus.graph_from_dot_data(graph_data)
g.set_shape_files(pngs)
jpe_data = g.create(format='jpe')
hexdigest = sha256(jpe_data).hexdigest()
hexdigest_original = self._render_with_graphviz(dot_file)
self.assertEqual(hexdigest, hexdigest_original)
def test_multiple_graphs(self):
graph_data = 'graph A { a->b };\ngraph B {c->d}'
graphs = pydotplus.graph_from_dot_data(graph_data)
self.assertEqual(len(graphs), 2)
self.assertEqual([g.get_name() for g in graphs], ['A', 'B'])
def _render_with_graphviz(self, filename):
p = subprocess.Popen(
(DOT_BINARY_PATH, '-Tjpe'),
cwd=os.path.dirname(filename),
stdin=open(filename, 'rt'),
stderr=subprocess.PIPE, stdout=subprocess.PIPE
)
stdout = p.stdout
stdout_output = list()
while True:
data = stdout.read()
if not data:
break
stdout_output.append(data)
stdout.close()
if stdout_output:
stdout_output = NULL_SEP.join(stdout_output)
# pid, status = os.waitpid(p.pid, 0)
# this returns a status code we should check
p.wait()
return sha256(stdout_output).hexdigest()
def _render_with_pydot(self, filename):
# f = open(filename, 'rt')
# graph_data = f.read()
# f.close()
# g = pydotplus.parse_from_dot_data(graph_data)
g = pydotplus.graph_from_dot_file(filename)
if not isinstance(g, list):
g = [g]
jpe_data = NULL_SEP.join([_g.create(format='jpe') for _g in g])
return sha256(jpe_data).hexdigest()
def test_my_regression_tests(self):
self._render_and_compare_dot_files(MY_REGRESSION_TESTS_DIR)
def test_graphviz_regression_tests(self):
self._render_and_compare_dot_files(REGRESSION_TESTS_DIR)
def _render_and_compare_dot_files(self, directory):
dot_files = [
fname for fname in os.listdir(directory)
if fname.endswith('.dot')
] # and fname.startswith('')]
for dot in dot_files:
os.sys.stdout.write('#')
os.sys.stdout.flush()
fname = os.path.join(directory, dot)
try:
parsed_data_hexdigest = self._render_with_pydot(fname)
original_data_hexdigest = self._render_with_graphviz(fname)
except Exception:
print('Failed rendering BAD(%s)' % dot)
raise
if parsed_data_hexdigest != original_data_hexdigest:
print('BAD(%s)' % dot)
self.assertEqual(parsed_data_hexdigest, original_data_hexdigest)
def test_numeric_node_id(self):
self._reset_graphs()
self.graph_directed.add_node(pydotplus.Node(1))
self.assertEqual(self.graph_directed.get_nodes()[0].get_name(), '1')
def test_quoted_node_id(self):
self._reset_graphs()
self.graph_directed.add_node(pydotplus.Node('"node"'))
self.assertEqual(
self.graph_directed.get_nodes()[0].get_name(), '"node"'
)
def test_quoted_node_id_to_string_no_attributes(self):
self._reset_graphs()
self.graph_directed.add_node(pydotplus.Node('"node"'))
self.assertEqual(
self.graph_directed.get_nodes()[0].to_string(), '"node";'
)
def test_keyword_node_id(self):
self._reset_graphs()
self.graph_directed.add_node(pydotplus.Node('node'))
self.assertEqual(self.graph_directed.get_nodes()[0].get_name(), 'node')
def test_keyword_node_id_to_string_no_attributes(self):
self._reset_graphs()
self.graph_directed.add_node(pydotplus.Node('node'))
self.assertEqual(self.graph_directed.get_nodes()[0].to_string(), '')
def test_keyword_node_id_to_string_with_attributes(self):
self._reset_graphs()
self.graph_directed.add_node(pydotplus.Node('node', shape='box'))
self.assertEqual(
self.graph_directed.get_nodes()[0].to_string(), 'node [shape=box];'
)
def test_names_of_a_thousand_nodes(self):
self._reset_graphs()
names = set(['node_%05d' % i for i in xrange(10 ** 4)])
for name in names:
self.graph_directed.add_node(pydotplus.Node(name, label=name))
self.assertEqual(
set([n.get_name() for n in self.graph_directed.get_nodes()]), names
)
def test_executable_not_found_exception(self):
paths = {'dot': 'invalid_executable_path'}
graph = pydotplus.Dot('graphname', graph_type='digraph')
graph.set_graphviz_executables(paths)
self.assertRaises(pydotplus.InvocationException, graph.create)
def test_graph_add_node_argument_type(self):
self._reset_graphs()
self.assertRaises(TypeError, self.graph_directed.add_node, 1)
self.assertRaises(TypeError, self.graph_directed.add_node, 'a')
def test_graph_add_edge_argument_type(self):
self._reset_graphs()
self.assertRaises(TypeError, self.graph_directed.add_edge, 1)
self.assertRaises(TypeError, self.graph_directed.add_edge, 'a')
def test_graph_add_subgraph_argument_type(self):
self._reset_graphs()
self.assertRaises(TypeError, self.graph_directed.add_subgraph, 1)
self.assertRaises(TypeError, self.graph_directed.add_subgraph, 'a')
def test_quoting(self):
import string
g = pydotplus.Dot()
g.add_node(pydotplus.Node("test", label=string.printable))
data = g.create(format='jpe')
self.assertEqual(len(data) > 0, True)
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TestGraphAPI)
unittest.TextTestRunner(verbosity=2).run(suite)
|