Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / drawing / tests / test_pydot.py @ 5cef0f13

History | View | Annotate | Download (3.74 KB)

1
"""Unit tests for pydot drawing functions."""
2
try:
3
    try:
4
        from cStringIO import StringIO
5
    except ImportError:
6
        from StringIO import StringIO
7
except ImportError:
8
    from io import StringIO
9
import sys
10
import tempfile
11
from nose.tools import assert_equal, assert_is_instance, assert_true
12
import networkx as nx
13
from networkx.testing import assert_graphs_equal
14

    
15

    
16
class TestPydot(object):
17
    @classmethod
18
    def setupClass(cls):
19
        '''
20
        Fixture defining the `pydot` global to be the `pydot` module if both
21
        importable and of sufficient version _or_ skipping this test.
22
        '''
23
        global pydot
24
        pydot = nx.nx_pydot.setup_module(sys.modules[__name__])
25
        assert pydot is not None
26

    
27
    def pydot_checks(self, G, prog):
28
        '''
29
        Validate :mod:`pydot`-based usage of the passed NetworkX graph with the
30
        passed basename of an external GraphViz command (e.g., `dot`, `neato`).
31
        '''
32

    
33
        # Set the name of this graph to... "G". Failing to do so will
34
        # subsequently trip an assertion expecting this name.
35
        G.graph['name'] = 'G'
36

    
37
        # Add arbitrary nodes and edges to the passed empty graph.
38
        G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'C'), ('A', 'D')])
39
        G.add_node('E')
40

    
41
        # Validate layout of this graph with the passed GraphViz command.
42
        graph_layout = nx.nx_pydot.pydot_layout(G, prog=prog)
43
        assert_is_instance(graph_layout, dict)
44

    
45
        # Convert this graph into a "pydot.Dot" instance.
46
        P = nx.nx_pydot.to_pydot(G)
47

    
48
        # Convert this "pydot.Dot" instance back into a graph of the same type.
49
        G2 = G.__class__(nx.nx_pydot.from_pydot(P))
50

    
51
        # Validate the original and resulting graphs to be the same.
52
        assert_graphs_equal(G, G2)
53

    
54
        # Serialize this "pydot.Dot" instance to a temporary file in dot format.
55
        fname = tempfile.mktemp()
56
        P.write_raw(fname)
57

    
58
        # Deserialize a list of new "pydot.Dot" instances back from this file.
59
        Pin_list = pydot.graph_from_dot_file(path=fname, encoding='utf-8')
60

    
61
        # Validate this file to contain only one graph.
62
        assert_equal(len(Pin_list), 1)
63

    
64
        # The single "pydot.Dot" instance deserialized from this file.
65
        Pin = Pin_list[0]
66

    
67
        # Sorted list of all nodes in the original "pydot.Dot" instance.
68
        n1 = sorted([p.get_name() for p in P.get_node_list()])
69

    
70
        # Sorted list of all nodes in the deserialized "pydot.Dot" instance.
71
        n2 = sorted([p.get_name() for p in Pin.get_node_list()])
72

    
73
        # Validate these instances to contain the same nodes.
74
        assert_equal(n1, n2)
75

    
76
        # Sorted list of all edges in the original "pydot.Dot" instance.
77
        e1 = sorted([
78
            (e.get_source(), e.get_destination()) for e in P.get_edge_list()])
79

    
80
        # Sorted list of all edges in the original "pydot.Dot" instance.
81
        e2 = sorted([
82
            (e.get_source(), e.get_destination()) for e in Pin.get_edge_list()])
83

    
84
        # Validate these instances to contain the same edges.
85
        assert_equal(e1, e2)
86

    
87
        # Deserialize a new graph of the same type back from this file.
88
        Hin = nx.nx_pydot.read_dot(fname)
89
        Hin = G.__class__(Hin)
90

    
91
        # Validate the original and resulting graphs to be the same.
92
        assert_graphs_equal(G, Hin)
93

    
94
    def test_undirected(self):
95
        self.pydot_checks(nx.Graph(), prog='neato')
96

    
97
    def test_directed(self):
98
        self.pydot_checks(nx.DiGraph(), prog='dot')
99

    
100
    def test_read_write(self):
101
        G = nx.MultiGraph()
102
        G.graph['name'] = 'G'
103
        G.add_edge('1', '2', key='0')  # read assumes strings
104
        fh = StringIO()
105
        nx.nx_pydot.write_dot(G, fh)
106
        fh.seek(0)
107
        H = nx.nx_pydot.read_dot(fh)
108
        assert_graphs_equal(G, H)