Skip to content
This repository was archived by the owner on Nov 19, 2018. It is now read-only.

Commit 197ea7d

Browse files
committed
Add tests and refactor graph_from_edges
1 parent cc9e892 commit 197ea7d

File tree

2 files changed

+104
-17
lines changed

2 files changed

+104
-17
lines changed

pydot_ng/__init__.py

+14-17
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from __future__ import print_function
1515

1616
import copy
17+
import functools
1718
import os
1819
import re
1920
import subprocess
@@ -306,27 +307,19 @@ def graph_from_edges(edge_list, node_prefix='', directed=False):
306307
If the graph is undirected by default, it is only
307308
calculated from one of the symmetric halves of the matrix.
308309
"""
310+
if edge_list is None:
311+
edge_list = []
309312

310-
if directed:
311-
graph = Dot(graph_type='digraph')
312-
313-
else:
314-
graph = Dot(graph_type='graph')
313+
graph_type = "digraph" if directed else "graph"
314+
with_prefix = functools.partial("{0}{1}".format, node_prefix)
315315

316-
for edge in edge_list:
316+
graph = Dot(graph_type=graph_type)
317317

318-
if isinstance(edge[0], str):
319-
src = node_prefix + edge[0]
320-
else:
321-
src = node_prefix + str(edge[0])
322-
323-
if isinstance(edge[1], str):
324-
dst = node_prefix + edge[1]
325-
else:
326-
dst = node_prefix + str(edge[1])
318+
for src, dst in edge_list:
319+
src = with_prefix(src)
320+
dst = with_prefix(dst)
327321

328-
e = Edge(src, dst)
329-
graph.add_edge(e)
322+
graph.add_edge(Edge(src, dst))
330323

331324
return graph
332325

@@ -881,10 +874,14 @@ def get_source(self):
881874
"""Get the edges source node name."""
882875
return self.obj_dict['points'][0]
883876

877+
source = property(get_source)
878+
884879
def get_destination(self):
885880
"""Get the edge's destination node name."""
886881
return self.obj_dict['points'][1]
887882

883+
destination = property(get_destination)
884+
888885
def __hash__(self):
889886
return hash(hash(self.get_source()) + hash(self.get_destination()))
890887

test/test_graph_from_edges.py

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import functools
4+
from textwrap import dedent
5+
6+
import pytest
7+
8+
import pydot_ng
9+
10+
11+
@pytest.mark.parametrize(
12+
"directed, graph_type",
13+
((True, "digraph"), (False, "graph")),
14+
ids=("graph", "digraph"),
15+
)
16+
@pytest.mark.parametrize("edges", (None, [], ()), ids=(None, "list", "tuple"))
17+
def test_empty_list(directed, graph_type, edges):
18+
graph = pydot_ng.graph_from_edges(edges, directed=directed)
19+
20+
assert graph.graph_type == graph_type
21+
assert graph.to_string() == dedent(
22+
"""\
23+
{graph_type} G {{
24+
}}
25+
""".format(
26+
graph_type=graph_type
27+
)
28+
)
29+
assert not graph.get_edges()
30+
31+
32+
@pytest.mark.parametrize(
33+
"prefix", ("", "some_prefix_"), ids=("no prefix", "prefix")
34+
)
35+
@pytest.mark.parametrize("src", (1, 2.123, "a", "ą", True))
36+
@pytest.mark.parametrize("dst", (1, 2.123, "a", "ą", True))
37+
def test_edge_types(prefix, src, dst):
38+
input_edges = [(src, dst)]
39+
graph = pydot_ng.graph_from_edges(input_edges, node_prefix=prefix)
40+
41+
edges = graph.get_edges()
42+
assert len(edges) == 1
43+
edge = edges[0]
44+
45+
with_prefix = functools.partial("{0}{1}".format, prefix)
46+
47+
assert edge.source == pydot_ng.quote_if_necessary(with_prefix(src))
48+
assert edge.destination == pydot_ng.quote_if_necessary(with_prefix(dst))
49+
50+
51+
@pytest.mark.parametrize(
52+
"prefix, output",
53+
(
54+
(
55+
"",
56+
dedent(
57+
"""\
58+
graph G {
59+
1 -- 2;
60+
2 -- "3.14";
61+
"3.14" -- a;
62+
a -- "ą";
63+
"ą" -- True;
64+
}
65+
"""
66+
),
67+
),
68+
(
69+
"prefix_",
70+
dedent(
71+
"""\
72+
graph G {
73+
prefix_1 -- prefix_2;
74+
prefix_2 -- "prefix_3.14";
75+
"prefix_3.14" -- prefix_a;
76+
prefix_a -- "prefix_ą";
77+
"prefix_ą" -- prefix_True;
78+
}
79+
"""
80+
),
81+
),
82+
),
83+
ids=("no prefix", "prefix"),
84+
)
85+
def test_from_edge_to_string(prefix, output):
86+
input_edges = [(1, 2), (2, 3.14), (3.14, "a"), ("a", "ą"), ("ą", True)]
87+
88+
graph = pydot_ng.graph_from_edges(input_edges, node_prefix=prefix)
89+
assert len(graph.get_edges()) == len(input_edges)
90+
assert graph.to_string() == output

0 commit comments

Comments
 (0)