Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / readwrite / json_graph / tests / test_node_link.py @ 5cef0f13

History | View | Annotate | Download (3.29 KB)

1
#  -*- coding: utf-8 -*-
2
import json
3
from nose.tools import assert_equal, assert_true, raises
4
import networkx as nx
5
from networkx.readwrite.json_graph import *
6

    
7

    
8
class TestNodeLink:
9

    
10
    def test_graph(self):
11
        G = nx.path_graph(4)
12
        H = node_link_graph(node_link_data(G))
13
        assert_true(nx.is_isomorphic(G, H))
14

    
15
    def test_graph_attributes(self):
16
        G = nx.path_graph(4)
17
        G.add_node(1, color='red')
18
        G.add_edge(1, 2, width=7)
19
        G.graph[1] = 'one'
20
        G.graph['foo'] = 'bar'
21

    
22
        H = node_link_graph(node_link_data(G))
23
        assert_equal(H.graph['foo'], 'bar')
24
        assert_equal(H.nodes[1]['color'], 'red')
25
        assert_equal(H[1][2]['width'], 7)
26

    
27
        d = json.dumps(node_link_data(G))
28
        H = node_link_graph(json.loads(d))
29
        assert_equal(H.graph['foo'], 'bar')
30
        assert_equal(H.graph['1'], 'one')
31
        assert_equal(H.nodes[1]['color'], 'red')
32
        assert_equal(H[1][2]['width'], 7)
33

    
34
    def test_digraph(self):
35
        G = nx.DiGraph()
36
        H = node_link_graph(node_link_data(G))
37
        assert_true(H.is_directed())
38

    
39
    def test_multigraph(self):
40
        G = nx.MultiGraph()
41
        G.add_edge(1, 2, key='first')
42
        G.add_edge(1, 2, key='second', color='blue')
43
        H = node_link_graph(node_link_data(G))
44
        nx.is_isomorphic(G, H)
45
        assert_equal(H[1][2]['second']['color'], 'blue')
46

    
47
    def test_graph_with_tuple_nodes(self):
48
        G = nx.Graph()
49
        G.add_edge((0, 0), (1, 0), color=[255, 255, 0])
50
        d = node_link_data(G)
51
        dumped_d = json.dumps(d)
52
        dd = json.loads(dumped_d)
53
        H = node_link_graph(dd)
54
        assert_equal(H.nodes[(0, 0)], G.nodes[(0, 0)])
55
        assert_equal(H[(0, 0)][(1, 0)]['color'], [255, 255, 0])
56

    
57
    def test_unicode_keys(self):
58
        try:
59
            q = unicode("qualité", 'utf-8')
60
        except NameError:
61
            q = "qualité"
62
        G = nx.Graph()
63
        G.add_node(1, **{q: q})
64
        s = node_link_data(G)
65
        output = json.dumps(s, ensure_ascii=False)
66
        data = json.loads(output)
67
        H = node_link_graph(data)
68
        assert_equal(H.nodes[1][q], q)
69

    
70
    @raises(nx.NetworkXError)
71
    def test_exception(self):
72
        G = nx.MultiDiGraph()
73
        attrs = dict(name='node', source='node', target='node', key='node')
74
        node_link_data(G, attrs)
75

    
76
    def test_string_ids(self):
77
        try:
78
            q = unicode("qualité", 'utf-8')
79
        except NameError:
80
            q = "qualité"
81

    
82
        G = nx.DiGraph()
83
        G.add_node('A')
84
        G.add_node(q)
85
        G.add_edge('A', q)
86
        data = node_link_data(G)
87
        assert_equal(data['links'][0]['source'], 'A')
88
        assert_equal(data['links'][0]['target'], q)
89
        H = node_link_graph(data)
90
        assert_true(nx.is_isomorphic(G, H))
91

    
92
    def test_custom_attrs(self):
93
        G = nx.path_graph(4)
94
        G.add_node(1, color='red')
95
        G.add_edge(1, 2, width=7)
96
        G.graph[1] = 'one'
97
        G.graph['foo'] = 'bar'
98

    
99
        attrs = dict(source='c_source', target='c_target', name='c_id', key='c_key', link='c_links')
100

    
101
        H = node_link_graph(node_link_data(G, attrs=attrs), multigraph=False, attrs=attrs)
102
        assert_true(nx.is_isomorphic(G, H))
103
        assert_equal(H.graph['foo'], 'bar')
104
        assert_equal(H.nodes[1]['color'], 'red')
105
        assert_equal(H[1][2]['width'], 7)