356 lines
9.8 KiB
Python
Raw Normal View History

# coding=iso-8859-1
# TODO:
# -test graph generation APIs (from adjacency, etc..)
# -test del_node, del_edge methods
# -test Common.set method
2013-07-26 03:06:12 -07:00
from __future__ import division, print_function
import os
try:
from hashlib import sha256
except ImportError:
import sha
sha256 = sha.new
import subprocess
2013-07-26 03:06:12 -07:00
import sys
2015-02-25 19:58:39 +01:00
import pydot_ng as pydot
import unittest2 as unittest
2013-07-26 03:06:12 -07:00
PY3 = not sys.version_info < (3, 0, 0)
if PY3:
NULL_SEP = b''
xrange = range
else:
NULL_SEP = ''
bytes = str
DOT_BINARY_PATH = pydot.find_graphviz()['dot']
2015-02-26 10:22:59 +01:00
TEST_DIR = os.path.dirname(__file__)
REGRESSION_TESTS_DIR = os.path.join(TEST_DIR, 'graphs')
MY_REGRESSION_TESTS_DIR = os.path.join(TEST_DIR, 'my_tests')
class TestGraphAPI(unittest.TestCase):
2013-07-26 00:15:57 -07:00
def setUp(self):
2013-07-26 00:15:57 -07:00
self._reset_graphs()
2013-07-26 00:15:57 -07:00
def _reset_graphs(self):
2013-07-26 00:15:57 -07:00
self.graph_directed = pydot.Graph('testgraph', graph_type='digraph')
2013-07-26 00:15:57 -07:00
def test_keep_graph_type(self):
2013-07-26 00:15:57 -07:00
g = pydot.Dot(graph_name='Test', graph_type='graph')
2013-07-26 00:15:57 -07:00
self.assertEqual(g.get_type(), 'graph')
2013-07-26 00:15:57 -07:00
g = pydot.Dot(graph_name='Test', graph_type='digraph')
2013-07-26 00:15:57 -07:00
self.assertEqual(g.get_type(), 'digraph')
2013-07-26 00:15:57 -07:00
def test_add_style(self):
2013-07-26 00:15:57 -07:00
node = pydot.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):
2013-07-26 00:15:57 -07:00
g = pydot.Dot()
g.set_type('digraph')
node = pydot.Node('legend')
node.set("shape", 'box')
g.add_node(node)
node.set('label', 'mine')
2013-07-26 00:15:57 -07:00
self.assertEqual(g.to_string(), 'digraph G {\nlegend [label=mine, shape=box];\n}\n')
2013-07-26 00:15:57 -07:00
def test_attribute_with_implicit_value(self):
d = 'digraph {\na -> b[label="hi", decorate];\n}'
g = pydot.graph_from_dot_data(d)
attrs = g.get_edges()[0].get_attributes()
2013-07-26 00:15:57 -07:00
self.assertEqual('decorate' in attrs, True)
def test_subgraphs(self):
g = pydot.Graph()
s = pydot.Subgraph("foo")
2013-07-26 00:15:57 -07:00
self.assertEqual(g.get_subgraphs(), [])
self.assertEqual(g.get_subgraph_list(), [])
2013-07-26 00:15:57 -07:00
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 = pydot.Graph()
s = pydot.Subgraph("foo")
g.add_subgraph(s)
g.add_edge(pydot.Edge('A', 'B'))
g.add_edge(pydot.Edge('A', 'C'))
g.add_edge(pydot.Edge(('D', 'E')))
g.add_node(pydot.Node('node!'))
self.assertEqual(type(pickle.dumps(g)), bytes)
def test_unicode_ids(self):
2013-07-26 00:15:57 -07:00
node1 = '"aánñoöüé€"'
node2 = '"îôø®çßΩ"'
2013-07-26 00:15:57 -07:00
g = pydot.Dot()
g.set_charset('latin1')
g.add_node(pydot.Node(node1))
g.add_node(pydot.Node(node2))
g.add_edge(pydot.Edge(node1, node2))
2013-07-26 00:15:57 -07:00
self.assertEqual(g.get_node(node1)[0].get_name(), node1)
self.assertEqual(g.get_node(node2)[0].get_name(), node2)
2013-07-26 00:15:57 -07:00
self.assertEqual(g.get_edges()[0].get_source(), node1)
self.assertEqual(g.get_edges()[0].get_destination(), node2)
2013-07-26 00:15:57 -07:00
g2 = pydot.graph_from_dot_data(g.to_string())
2013-07-26 00:15:57 -07:00
self.assertEqual(g2.get_node(node1)[0].get_name(), node1)
self.assertEqual(g2.get_node(node2)[0].get_name(), node2)
2013-07-26 00:15:57 -07:00
self.assertEqual(g2.get_edges()[0].get_source(), node1)
self.assertEqual(g2.get_edges()[0].get_destination(), node2)
2013-07-26 00:15:57 -07:00
@unittest.skip("failing checksum")
def test_graph_with_shapefiles(self):
2013-07-26 00:15:57 -07:00
shapefile_dir = os.path.join(TEST_DIR, 'from-past-to-future')
dot_file = os.path.join(shapefile_dir, 'from-past-to-future.dot')
2013-07-26 00:15:57 -07:00
pngs = [
os.path.join(shapefile_dir, fname)
for fname in os.listdir(shapefile_dir)
if fname.endswith('.png')
]
2013-07-26 00:15:57 -07:00
f = open(dot_file, 'rt')
graph_data = f.read()
f.close()
2013-07-26 00:15:57 -07:00
g = pydot.graph_from_dot_data(graph_data)
2013-07-26 00:15:57 -07:00
g.set_shape_files(pngs)
2013-07-26 00:15:57 -07:00
jpe_data = g.create(format='jpe')
2013-07-26 00:15:57 -07:00
hexdigest = sha256(jpe_data).hexdigest()
2013-07-26 00:15:57 -07:00
hexdigest_original = self._render_with_graphviz(dot_file)
2013-07-26 00:15:57 -07:00
self.assertEqual(hexdigest, hexdigest_original)
2013-07-26 00:15:57 -07:00
def test_multiple_graphs(self):
2013-07-26 00:15:57 -07:00
graph_data = 'graph A { a->b };\ngraph B {c->d}'
2013-07-26 00:15:57 -07:00
graphs = pydot.graph_from_dot_data(graph_data)
2013-07-26 00:15:57 -07:00
self.assertEqual(len(graphs), 2)
2013-07-26 00:15:57 -07:00
self.assertEqual([g.get_name() for g in graphs], ['A', 'B'])
2013-07-26 00:15:57 -07:00
def _render_with_graphviz(self, filename):
2013-07-26 00:15:57 -07:00
p = subprocess.Popen(
(DOT_BINARY_PATH, '-Tjpe'),
2013-07-26 03:06:12 -07:00
cwd=os.path.dirname(filename),
stdin=open(filename, 'rt'),
stderr=subprocess.PIPE, stdout=subprocess.PIPE
)
2013-07-26 00:15:57 -07:00
stdout = p.stdout
2013-07-26 00:15:57 -07:00
stdout_output = list()
while True:
data = stdout.read()
if not data:
break
stdout_output.append(data)
stdout.close()
2013-07-26 00:15:57 -07:00
if stdout_output:
2013-07-26 03:06:12 -07:00
stdout_output = NULL_SEP.join(stdout_output)
2013-07-26 00:15:57 -07:00
#pid, status = os.waitpid(p.pid, 0)
# this returns a status code we should check
p.wait()
2013-07-26 00:15:57 -07:00
return sha256(stdout_output).hexdigest()
2013-07-26 00:15:57 -07:00
def _render_with_pydot(self, filename):
2013-07-26 03:06:12 -07:00
#f = open(filename, 'rt')
#graph_data = f.read()
#f.close()
2013-07-26 00:15:57 -07:00
#g = pydot.parse_from_dot_data(graph_data)
g = pydot.graph_from_dot_file(filename)
2013-07-26 00:15:57 -07:00
if not isinstance(g, list):
g = [g]
2013-07-26 00:15:57 -07:00
jpe_data = NULL_SEP.join([_g.create(format='jpe') for _g in g])
2013-07-26 00:15:57 -07:00
return sha256(jpe_data).hexdigest()
2013-07-26 00:15:57 -07:00
def test_my_regression_tests(self):
self._render_and_compare_dot_files(MY_REGRESSION_TESTS_DIR)
2013-07-26 00:15:57 -07:00
@unittest.skip('failing checksum')
def test_graphviz_regression_tests(self):
self._render_and_compare_dot_files(REGRESSION_TESTS_DIR)
2013-07-26 00:15:57 -07:00
def _render_and_compare_dot_files(self, directory):
2013-07-26 00:15:57 -07:00
dot_files = [
fname for fname in os.listdir(directory)
if fname.endswith('.dot')
] # and fname.startswith('')]
2013-07-26 00:15:57 -07:00
for dot in dot_files:
2013-07-26 00:15:57 -07:00
#print 'Processing: %s' % dot
os.sys.stdout.write('#')
os.sys.stdout.flush()
2013-07-26 00:15:57 -07:00
fname = os.path.join(directory, dot)
2013-07-26 00:15:57 -07:00
try:
parsed_data_hexdigest = self._render_with_pydot(fname)
original_data_hexdigest = self._render_with_graphviz(fname)
2013-07-26 03:06:12 -07:00
except Exception:
print('Failed rendering BAD(%s)' % dot)
#print 'Error:', str(excp)
2013-07-26 03:06:12 -07:00
raise
2013-07-26 00:15:57 -07:00
if parsed_data_hexdigest != original_data_hexdigest:
2013-07-26 03:06:12 -07:00
print('BAD(%s)' % dot)
2013-07-26 00:15:57 -07:00
self.assertEqual(parsed_data_hexdigest, original_data_hexdigest)
2013-07-26 00:15:57 -07:00
def test_numeric_node_id(self):
2013-07-26 00:15:57 -07:00
self._reset_graphs()
2013-07-26 00:15:57 -07:00
self.graph_directed.add_node(pydot.Node(1))
2013-07-26 00:15:57 -07:00
self.assertEqual(self.graph_directed.get_nodes()[0].get_name(), '1')
2013-07-26 00:15:57 -07:00
def test_quoted_node_id(self):
2013-07-26 00:15:57 -07:00
self._reset_graphs()
2013-07-26 00:15:57 -07:00
self.graph_directed.add_node(pydot.Node('"node"'))
2013-07-26 00:15:57 -07:00
self.assertEqual(self.graph_directed.get_nodes()[0].get_name(), '"node"')
2013-07-26 00:15:57 -07:00
def test_quoted_node_id_to_string_no_attributes(self):
2013-07-26 00:15:57 -07:00
self._reset_graphs()
2013-07-26 00:15:57 -07:00
self.graph_directed.add_node(pydot.Node('"node"'))
2013-07-26 00:15:57 -07:00
self.assertEqual(self.graph_directed.get_nodes()[0].to_string(), '"node";')
2013-07-26 00:15:57 -07:00
def test_keyword_node_id(self):
2013-07-26 00:15:57 -07:00
self._reset_graphs()
2013-07-26 00:15:57 -07:00
self.graph_directed.add_node(pydot.Node('node'))
2013-07-26 00:15:57 -07:00
self.assertEqual(self.graph_directed.get_nodes()[0].get_name(), 'node')
2013-07-26 00:15:57 -07:00
def test_keyword_node_id_to_string_no_attributes(self):
2013-07-26 00:15:57 -07:00
self._reset_graphs()
2013-07-26 00:15:57 -07:00
self.graph_directed.add_node(pydot.Node('node'))
2013-07-26 00:15:57 -07:00
self.assertEqual(self.graph_directed.get_nodes()[0].to_string(), '')
2013-07-26 00:15:57 -07:00
def test_keyword_node_id_to_string_with_attributes(self):
2013-07-26 00:15:57 -07:00
self._reset_graphs()
2013-07-26 00:15:57 -07:00
self.graph_directed.add_node(pydot.Node('node', shape='box'))
2013-07-26 00:15:57 -07:00
self.assertEqual(self.graph_directed.get_nodes()[0].to_string(), 'node [shape=box];')
2013-07-26 00:15:57 -07:00
def test_names_of_a_thousand_nodes(self):
2013-07-26 00:15:57 -07:00
self._reset_graphs()
2013-07-26 00:15:57 -07:00
names = set(['node_%05d' % i for i in xrange(10 ** 4)])
2013-07-26 00:15:57 -07:00
for name in names:
2013-07-26 00:15:57 -07:00
self.graph_directed.add_node(pydot.Node(name, label=name))
2013-07-26 00:15:57 -07:00
self.assertEqual(set([n.get_name() for n in self.graph_directed.get_nodes()]), names)
2013-07-26 00:15:57 -07:00
def test_executable_not_found_exception(self):
paths = {'dot': 'invalid_executable_path'}
2013-07-26 00:15:57 -07:00
graph = pydot.Dot('graphname', graph_type='digraph')
2013-07-26 00:15:57 -07:00
graph.set_graphviz_executables(paths)
2013-07-26 00:15:57 -07:00
self.assertRaises(pydot.InvocationException, graph.create)
2013-07-26 00:15:57 -07:00
def test_graph_add_node_argument_type(self):
2013-07-26 00:15:57 -07:00
self._reset_graphs()
2013-07-26 00:15:57 -07:00
self.assertRaises(TypeError, self.graph_directed.add_node, 1)
self.assertRaises(TypeError, self.graph_directed.add_node, 'a')
2013-07-26 00:15:57 -07:00
def test_graph_add_edge_argument_type(self):
2013-07-26 00:15:57 -07:00
self._reset_graphs()
2013-07-26 00:15:57 -07:00
self.assertRaises(TypeError, self.graph_directed.add_edge, 1)
self.assertRaises(TypeError, self.graph_directed.add_edge, 'a')
2013-07-26 00:15:57 -07:00
def test_graph_add_subgraph_argument_type(self):
2013-07-26 00:15:57 -07:00
self._reset_graphs()
2013-07-26 00:15:57 -07:00
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 = pydot.Dot()
g.add_node(pydot.Node("test", label=string.printable))
#print g.to_string()
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)