Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (1.77 KB)

1
import json
2
from nose.tools import assert_equal, assert_true, raises
3
import networkx as nx
4
from networkx.readwrite.json_graph import *
5

    
6

    
7
class TestAdjacency:
8

    
9
    def test_graph(self):
10
        G = nx.path_graph(4)
11
        H = adjacency_graph(adjacency_data(G))
12
        nx.is_isomorphic(G, H)
13

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

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

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

    
33
    def test_digraph(self):
34
        G = nx.DiGraph()
35
        nx.add_path(G, [1, 2, 3])
36
        H = adjacency_graph(adjacency_data(G))
37
        assert_true(H.is_directed())
38
        nx.is_isomorphic(G, H)
39

    
40
    def test_multidigraph(self):
41
        G = nx.MultiDiGraph()
42
        nx.add_path(G, [1, 2, 3])
43
        H = adjacency_graph(adjacency_data(G))
44
        assert_true(H.is_directed())
45
        assert_true(H.is_multigraph())
46

    
47
    def test_multigraph(self):
48
        G = nx.MultiGraph()
49
        G.add_edge(1, 2, key='first')
50
        G.add_edge(1, 2, key='second', color='blue')
51
        H = adjacency_graph(adjacency_data(G))
52
        nx.is_isomorphic(G, H)
53
        assert_equal(H[1][2]['second']['color'], 'blue')
54

    
55
    @raises(nx.NetworkXError)
56
    def test_exception(self):
57
        G = nx.MultiDiGraph()
58
        attrs = dict(id='node', key='node')
59
        adjacency_data(G, attrs)