Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / classes / tests / test_special.py @ 5cef0f13

History | View | Annotate | Download (4.89 KB)

1
#!/usr/bin/env python
2
from nose.tools import *
3
from collections import OrderedDict
4
import networkx as nx
5
from test_graph import TestGraph
6
from test_digraph import TestDiGraph
7
from test_multigraph import TestMultiGraph
8
from test_multidigraph import TestMultiDiGraph
9

    
10

    
11
def test_factories():
12
    class mydict1(dict):
13
        pass
14

    
15
    class mydict2(dict):
16
        pass
17

    
18
    class mydict3(dict):
19
        pass
20

    
21
    class mydict4(dict):
22
        pass
23

    
24
    class mydict5(dict):
25
        pass
26

    
27
    for Graph in (nx.Graph, nx.DiGraph, nx.MultiGraph, nx.MultiDiGraph):
28
        # print("testing class: ", Graph.__name__)
29
        class MyGraph(Graph):
30
            node_dict_factory = mydict1
31
            adjlist_outer_dict_factory = mydict2
32
            adjlist_inner_dict_factory = mydict3
33
            edge_key_dict_factory = mydict4
34
            edge_attr_dict_factory = mydict5
35
        G = MyGraph()
36
        assert_is_instance(G._node, mydict1)
37
        assert_is_instance(G._adj, mydict2)
38
        G.add_node(1)
39
        assert_is_instance(G._adj[1], mydict3)
40
        if G.is_directed():
41
            assert_is_instance(G._pred, mydict2)
42
            assert_is_instance(G._succ, mydict2)
43
            assert_is_instance(G._pred[1], mydict3)
44
        G.add_edge(1, 2)
45
        if G.is_multigraph():
46
            assert_is_instance(G._adj[1][2], mydict4)
47
            assert_is_instance(G._adj[1][2][0], mydict5)
48
        else:
49
            assert_is_instance(G._adj[1][2], mydict5)
50

    
51

    
52
class SpecialGraphTester(TestGraph):
53
    def setUp(self):
54
        TestGraph.setUp(self)
55
        self.Graph = nx.Graph
56

    
57

    
58
class OrderedGraphTester(TestGraph):
59
    def setUp(self):
60
        TestGraph.setUp(self)
61

    
62
        class MyGraph(nx.Graph):
63
            node_dict_factory = OrderedDict
64
            adjlist_outer_dict_factory = OrderedDict
65
            adjlist_inner_dict_factory = OrderedDict
66
            edge_attr_dict_factory = OrderedDict
67
        self.Graph = MyGraph
68

    
69

    
70
class ThinGraphTester(TestGraph):
71
    def setUp(self):
72
        all_edge_dict = {'weight': 1}
73

    
74
        class MyGraph(nx.Graph):
75
            def edge_attr_dict_factory(self): return all_edge_dict
76
        self.Graph = MyGraph
77
        # build dict-of-dict-of-dict K3
78
        ed1, ed2, ed3 = (all_edge_dict, all_edge_dict, all_edge_dict)
79
        self.k3adj = {0: {1: ed1, 2: ed2},
80
                      1: {0: ed1, 2: ed3},
81
                      2: {0: ed2, 1: ed3}}
82
        self.k3edges = [(0, 1), (0, 2), (1, 2)]
83
        self.k3nodes = [0, 1, 2]
84
        self.K3 = self.Graph()
85
        self.K3._adj = self.k3adj
86
        self.K3._node = {}
87
        self.K3._node[0] = {}
88
        self.K3._node[1] = {}
89
        self.K3._node[2] = {}
90

    
91

    
92
class SpecialDiGraphTester(TestDiGraph):
93
    def setUp(self):
94
        TestDiGraph.setUp(self)
95
        self.Graph = nx.DiGraph
96

    
97

    
98
class OrderedDiGraphTester(TestDiGraph):
99
    def setUp(self):
100
        TestGraph.setUp(self)
101

    
102
        class MyGraph(nx.DiGraph):
103
            node_dict_factory = OrderedDict
104
            adjlist_outer_dict_factory = OrderedDict
105
            adjlist_inner_dict_factory = OrderedDict
106
            edge_attr_dict_factory = OrderedDict
107
        self.Graph = MyGraph
108

    
109

    
110
class ThinDiGraphTester(TestDiGraph):
111
    def setUp(self):
112
        all_edge_dict = {'weight': 1}
113

    
114
        class MyGraph(nx.DiGraph):
115
            def edge_attr_dict_factory(self): return all_edge_dict
116
        self.Graph = MyGraph
117
        # build dict-of-dict-of-dict K3
118
        ed1, ed2, ed3 = (all_edge_dict, all_edge_dict, all_edge_dict)
119
        self.k3adj = {0: {1: ed1, 2: ed2},
120
                      1: {0: ed1, 2: ed3},
121
                      2: {0: ed2, 1: ed3}}
122
        self.k3edges = [(0, 1), (0, 2), (1, 2)]
123
        self.k3nodes = [0, 1, 2]
124
        self.K3 = self.Graph()
125
        self.K3.adj = self.k3adj
126
        self.K3._node = {}
127
        self.K3._node[0] = {}
128
        self.K3._node[1] = {}
129
        self.K3._node[2] = {}
130

    
131

    
132
class SpecialMultiGraphTester(TestMultiGraph):
133
    def setUp(self):
134
        TestMultiGraph.setUp(self)
135
        self.Graph = nx.MultiGraph
136

    
137

    
138
class OrderedMultiGraphTester(TestMultiGraph):
139
    def setUp(self):
140
        TestMultiGraph.setUp(self)
141

    
142
        class MyGraph(nx.MultiGraph):
143
            node_dict_factory = OrderedDict
144
            adjlist_outer_dict_factory = OrderedDict
145
            adjlist_inner_dict_factory = OrderedDict
146
            edge_key_dict_factory = OrderedDict
147
            edge_attr_dict_factory = OrderedDict
148
        self.Graph = MyGraph
149

    
150

    
151
class SpecialMultiDiGraphTester(TestMultiDiGraph):
152
    def setUp(self):
153
        TestMultiDiGraph.setUp(self)
154
        self.Graph = nx.MultiDiGraph
155

    
156

    
157
class OrderedMultiDiGraphTester(TestMultiDiGraph):
158
    def setUp(self):
159
        TestMultiDiGraph.setUp(self)
160

    
161
        class MyGraph(nx.MultiDiGraph):
162
            node_dict_factory = OrderedDict
163
            adjlist_outer_dict_factory = OrderedDict
164
            adjlist_inner_dict_factory = OrderedDict
165
            edge_key_dict_factory = OrderedDict
166
            edge_attr_dict_factory = OrderedDict
167
        self.Graph = MyGraph