Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (3.19 KB)

1
"""Unit tests for PyGraphviz interface."""
2
import os
3
import tempfile
4
from nose import SkipTest
5
from nose.tools import assert_true, assert_equal, assert_raises
6
from networkx.testing import assert_edges_equal, assert_nodes_equal, \
7
        assert_graphs_equal
8

    
9
import networkx as nx
10

    
11

    
12
class TestAGraph(object):
13
    @classmethod
14
    def setupClass(cls):
15
        global pygraphviz
16
        try:
17
            import pygraphviz
18
        except ImportError:
19
            raise SkipTest('PyGraphviz not available.')
20

    
21
    def build_graph(self, G):
22
        edges = [('A', 'B'), ('A', 'C'), ('A', 'C'), ('B', 'C'), ('A', 'D')]
23
        G.add_edges_from(edges)
24
        G.add_node('E')
25
        G.graph['metal'] = 'bronze'
26
        return G
27

    
28
    def assert_equal(self, G1, G2):
29
        assert_nodes_equal(G1.nodes(), G2.nodes())
30
        assert_edges_equal(G1.edges(), G2.edges())
31
        assert_equal(G1.graph['metal'], G2.graph['metal'])
32

    
33
    def agraph_checks(self, G):
34
        G = self.build_graph(G)
35
        A = nx.nx_agraph.to_agraph(G)
36
        H = nx.nx_agraph.from_agraph(A)
37
        self.assert_equal(G, H)
38

    
39
        fname = tempfile.mktemp()
40
        nx.drawing.nx_agraph.write_dot(H, fname)
41
        Hin = nx.nx_agraph.read_dot(fname)
42
        os.unlink(fname)
43
        self.assert_equal(H, Hin)
44

    
45
        (fd, fname) = tempfile.mkstemp()
46
        with open(fname, 'w') as fh:
47
            nx.drawing.nx_agraph.write_dot(H, fh)
48

    
49
        with open(fname, 'r') as fh:
50
            Hin = nx.nx_agraph.read_dot(fh)
51
        os.unlink(fname)
52
        self.assert_equal(H, Hin)
53

    
54
    def test_from_agraph_name(self):
55
        G = nx.Graph(name='test')
56
        A = nx.nx_agraph.to_agraph(G)
57
        H = nx.nx_agraph.from_agraph(A)
58
        assert_equal(G.name, 'test')
59

    
60
    def test_undirected(self):
61
        self.agraph_checks(nx.Graph())
62

    
63
    def test_directed(self):
64
        self.agraph_checks(nx.DiGraph())
65

    
66
    def test_multi_undirected(self):
67
        self.agraph_checks(nx.MultiGraph())
68

    
69
    def test_multi_directed(self):
70
        self.agraph_checks(nx.MultiDiGraph())
71

    
72
    def test_view_pygraphviz(self):
73
        G = nx.Graph()  # "An empty graph cannot be drawn."
74
        assert_raises(nx.NetworkXException, nx.nx_agraph.view_pygraphviz, G)
75
        G = nx.barbell_graph(4, 6)
76
        nx.nx_agraph.view_pygraphviz(G)
77

    
78
    def test_view_pygraphviz_edgelable(self):
79
        G = nx.Graph()
80
        G.add_edge(1, 2, weight=7)
81
        G.add_edge(2, 3, weight=8)
82
        nx.nx_agraph.view_pygraphviz(G, edgelabel='weight')
83

    
84
    def test_graph_with_reserved_keywords(self):
85
        # test attribute/keyword clash case for #1582
86
        # node: n
87
        # edges: u,v
88
        G = nx.Graph()
89
        G = self.build_graph(G)
90
        G.node['E']['n'] = 'keyword'
91
        G.edges[('A', 'B')]['u'] = 'keyword'
92
        G.edges[('A', 'B')]['v'] = 'keyword'
93
        A = nx.nx_agraph.to_agraph(G)
94

    
95
    def test_round_trip(self):
96
        G = nx.Graph()
97
        A = nx.nx_agraph.to_agraph(G)
98
        H = nx.nx_agraph.from_agraph(A)
99
        #assert_graphs_equal(G, H)
100
        AA = nx.nx_agraph.to_agraph(H)
101
        HH = nx.nx_agraph.from_agraph(AA)
102
        assert_graphs_equal(H, HH)
103
        G.graph['graph'] = {}
104
        G.graph['node'] = {}
105
        G.graph['edge'] = {}
106
        assert_graphs_equal(G, HH)