Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (12.5 KB)

1 5cef0f13 tiamilani
from nose.tools import assert_equal, assert_not_equal, assert_is,\
2
    assert_is_not, assert_true, assert_false, assert_raises
3
import tempfile
4
import pickle
5
6
import networkx as nx
7
8
9
class TestAtlasView(object):
10
    # node->data
11
    def setup(self):
12
        self.d = {0: {'color': 'blue', 'weight': 1.2}, 1: {}, 2: {'color': 1}}
13
        self.av = nx.classes.coreviews.AtlasView(self.d)
14
15
    def test_pickle(self):
16
        view = self.av
17
        pview = pickle.loads(pickle.dumps(view, -1))
18
        assert_equal(view, pview)
19
        assert_equal(view.__slots__, pview.__slots__)
20
        pview = pickle.loads(pickle.dumps(view))
21
        assert_equal(view, pview)
22
        assert_equal(view.__slots__, pview.__slots__)
23
24
    def test_len(self):
25
        assert_equal(len(self.av), len(self.d))
26
27
    def test_iter(self):
28
        assert_equal(list(self.av), list(self.d))
29
30
    def test_getitem(self):
31
        assert_is(self.av[1], self.d[1])
32
        assert_equal(self.av[2]['color'], 1)
33
        assert_raises(KeyError, self.av.__getitem__, 3)
34
35
    def test_copy(self):
36
        avcopy = self.av.copy()
37
        assert_equal(avcopy[0], self.av[0])
38
        assert_equal(avcopy, self.av)
39
        assert_is_not(avcopy[0], self.av[0])
40
        assert_is_not(avcopy, self.av)
41
        avcopy[5] = {}
42
        assert_not_equal(avcopy, self.av)
43
44
        avcopy[0]['ht'] = 4
45
        assert_not_equal(avcopy[0], self.av[0])
46
        self.av[0]['ht'] = 4
47
        assert_equal(avcopy[0], self.av[0])
48
        del self.av[0]['ht']
49
50
        assert_false(hasattr(self.av, '__setitem__'))
51
52
    def test_items(self):
53
        assert_equal(sorted(self.av.items()), sorted(self.d.items()))
54
55
    def test_str(self):
56
        out = str(self.d)
57
        assert_equal(str(self.av), out)
58
59
    def test_repr(self):
60
        out = "AtlasView(" + str(self.d) + ")"
61
        assert_equal(repr(self.av), out)
62
63
64
class TestAdjacencyView(object):
65
    # node->nbr->data
66
    def setup(self):
67
        dd = {'color': 'blue', 'weight': 1.2}
68
        self.nd = {0: dd, 1: {}, 2: {'color': 1}}
69
        self.adj = {3: self.nd, 0: {3: dd}, 1: {}, 2: {3: {'color': 1}}}
70
        self.adjview = nx.classes.coreviews.AdjacencyView(self.adj)
71
72
    def test_pickle(self):
73
        view = self.adjview
74
        pview = pickle.loads(pickle.dumps(view, -1))
75
        assert_equal(view, pview)
76
        assert_equal(view.__slots__, pview.__slots__)
77
78
    def test_len(self):
79
        assert_equal(len(self.adjview), len(self.adj))
80
81
    def test_iter(self):
82
        assert_equal(list(self.adjview), list(self.adj))
83
84
    def test_getitem(self):
85
        assert_is_not(self.adjview[1], self.adj[1])
86
        assert_is(self.adjview[3][0], self.adjview[0][3])
87
        assert_equal(self.adjview[2][3]['color'], 1)
88
        assert_raises(KeyError, self.adjview.__getitem__, 4)
89
90
    def test_copy(self):
91
        avcopy = self.adjview.copy()
92
        assert_equal(avcopy[0], self.adjview[0])
93
        assert_is_not(avcopy[0], self.adjview[0])
94
95
        avcopy[2][3]['ht'] = 4
96
        assert_not_equal(avcopy[2], self.adjview[2])
97
        self.adjview[2][3]['ht'] = 4
98
        assert_equal(avcopy[2], self.adjview[2])
99
        del self.adjview[2][3]['ht']
100
101
        assert_false(hasattr(self.adjview, '__setitem__'))
102
103
    def test_items(self):
104
        view_items = sorted((n, dict(d)) for n, d in self.adjview.items())
105
        assert_equal(view_items, sorted(self.adj.items()))
106
107
    def test_str(self):
108
        out = str(dict(self.adj))
109
        assert_equal(str(self.adjview), out)
110
111
    def test_repr(self):
112
        out = self.adjview.__class__.__name__ + "(" + str(self.adj) + ")"
113
        assert_equal(repr(self.adjview), out)
114
115
116
class TestMultiAdjacencyView(TestAdjacencyView):
117
    # node->nbr->key->data
118
    def setup(self):
119
        dd = {'color': 'blue', 'weight': 1.2}
120
        self.kd = {0: dd, 1: {}, 2: {'color': 1}}
121
        self.nd = {3: self.kd, 0: {3: dd}, 1: {0: {}}, 2: {3: {'color': 1}}}
122
        self.adj = {3: self.nd, 0: {3: {3: dd}}, 1: {}, 2: {3: {8: {}}}}
123
        self.adjview = nx.classes.coreviews.MultiAdjacencyView(self.adj)
124
125
    def test_getitem(self):
126
        assert_is_not(self.adjview[1], self.adj[1])
127
        assert_is(self.adjview[3][0][3], self.adjview[0][3][3])
128
        assert_equal(self.adjview[3][2][3]['color'], 1)
129
        assert_raises(KeyError, self.adjview.__getitem__, 4)
130
131
    def test_copy(self):
132
        avcopy = self.adjview.copy()
133
        assert_equal(avcopy[0], self.adjview[0])
134
        assert_is_not(avcopy[0], self.adjview[0])
135
136
        avcopy[2][3][8]['ht'] = 4
137
        assert_not_equal(avcopy[2], self.adjview[2])
138
        self.adjview[2][3][8]['ht'] = 4
139
        assert_equal(avcopy[2], self.adjview[2])
140
        del self.adjview[2][3][8]['ht']
141
142
        assert_false(hasattr(self.adjview, '__setitem__'))
143
144
145
class TestUnionAtlas(object):
146
    # node->data
147
    def setup(self):
148
        self.s = {0: {'color': 'blue', 'weight': 1.2}, 1: {}, 2: {'color': 1}}
149
        self.p = {3: {'color': 'blue', 'weight': 1.2}, 4: {}, 2: {'watch': 2}}
150
        self.av = nx.classes.coreviews.UnionAtlas(self.s, self.p)
151
152
    def test_pickle(self):
153
        view = self.av
154
        pview = pickle.loads(pickle.dumps(view, -1))
155
        assert_equal(view, pview)
156
        assert_equal(view.__slots__, pview.__slots__)
157
158
    def test_len(self):
159
        assert_equal(len(self.av), len(self.s) + len(self.p))
160
161
    def test_iter(self):
162
        assert_equal(set(self.av), set(self.s) | set(self.p))
163
164
    def test_getitem(self):
165
        assert_is(self.av[0], self.s[0])
166
        assert_is(self.av[4], self.p[4])
167
        assert_equal(self.av[2]['color'], 1)
168
        assert_raises(KeyError, self.av[2].__getitem__, 'watch')
169
        assert_raises(KeyError, self.av.__getitem__, 8)
170
171
    def test_copy(self):
172
        avcopy = self.av.copy()
173
        assert_equal(avcopy[0], self.av[0])
174
        assert_is_not(avcopy[0], self.av[0])
175
        assert_is_not(avcopy, self.av)
176
        avcopy[5] = {}
177
        assert_not_equal(avcopy, self.av)
178
179
        avcopy[0]['ht'] = 4
180
        assert_not_equal(avcopy[0], self.av[0])
181
        self.av[0]['ht'] = 4
182
        assert_equal(avcopy[0], self.av[0])
183
        del self.av[0]['ht']
184
185
        assert_false(hasattr(self.av, '__setitem__'))
186
187
    def test_items(self):
188
        expected = dict(self.p.items())
189
        expected.update(self.s)
190
        assert_equal(sorted(self.av.items()), sorted(expected.items()))
191
192
    def test_str(self):
193
        out = str(dict(self.av))
194
        assert_equal(str(self.av), out)
195
196
    def test_repr(self):
197
        out = "{}({}, {})".format(self.av.__class__.__name__, self.s, self.p)
198
        assert_equal(repr(self.av), out)
199
200
201
class TestUnionAdjacency(object):
202
    # node->nbr->data
203
    def setup(self):
204
        dd = {'color': 'blue', 'weight': 1.2}
205
        self.nd = {0: dd, 1: {}, 2: {'color': 1}}
206
        self.s = {3: self.nd, 0: {}, 1: {}, 2: {3: {'color': 1}}}
207
        self.p = {3: {}, 0: {3: dd}, 1: {0: {}}, 2: {1: {'color': 1}}}
208
        self.adjview = nx.classes.coreviews.UnionAdjacency(self.s, self.p)
209
210
    def test_pickle(self):
211
        view = self.adjview
212
        pview = pickle.loads(pickle.dumps(view, -1))
213
        assert_equal(view, pview)
214
        assert_equal(view.__slots__, pview.__slots__)
215
216
    def test_len(self):
217
        assert_equal(len(self.adjview), len(self.s))
218
219
    def test_iter(self):
220
        assert_equal(sorted(self.adjview), sorted(self.s))
221
222
    def test_getitem(self):
223
        assert_is_not(self.adjview[1], self.s[1])
224
        assert_is(self.adjview[3][0], self.adjview[0][3])
225
        assert_equal(self.adjview[2][3]['color'], 1)
226
        assert_raises(KeyError, self.adjview.__getitem__, 4)
227
228
    def test_copy(self):
229
        avcopy = self.adjview.copy()
230
        assert_equal(avcopy[0], self.adjview[0])
231
        assert_is_not(avcopy[0], self.adjview[0])
232
233
        avcopy[2][3]['ht'] = 4
234
        assert_not_equal(avcopy[2], self.adjview[2])
235
        self.adjview[2][3]['ht'] = 4
236
        assert_equal(avcopy[2], self.adjview[2])
237
        del self.adjview[2][3]['ht']
238
239
        assert_false(hasattr(self.adjview, '__setitem__'))
240
241
    def test_str(self):
242
        out = str(dict(self.adjview))
243
        assert_equal(str(self.adjview), out)
244
245
    def test_repr(self):
246
        clsname = self.adjview.__class__.__name__
247
        out = "{}({}, {})".format(clsname, self.s, self.p)
248
        assert_equal(repr(self.adjview), out)
249
250
251
class TestUnionMultiInner(TestUnionAdjacency):
252
    # nbr->key->data
253
    def setup(self):
254
        dd = {'color': 'blue', 'weight': 1.2}
255
        self.kd = {7: {}, 'ekey': {}, 9: {'color': 1}}
256
        self.s = {3: self.kd, 0: {7: dd}, 1: {}, 2: {'key': {'color': 1}}}
257
        self.p = {3: {}, 0: {3: dd}, 1: {}, 2: {1: {'span': 2}}}
258
        self.adjview = nx.classes.coreviews.UnionMultiInner(self.s, self.p)
259
260
    def test_len(self):
261
        assert_equal(len(self.adjview), len(self.s) + len(self.p))
262
263
    def test_getitem(self):
264
        assert_is_not(self.adjview[1], self.s[1])
265
        assert_is(self.adjview[0][7], self.adjview[0][3])
266
        assert_equal(self.adjview[2]['key']['color'], 1)
267
        assert_equal(self.adjview[2][1]['span'], 2)
268
        assert_raises(KeyError, self.adjview.__getitem__, 4)
269
        assert_raises(KeyError, self.adjview[1].__getitem__, 'key')
270
271
    def test_copy(self):
272
        avcopy = self.adjview.copy()
273
        assert_equal(avcopy[0], self.adjview[0])
274
        assert_is_not(avcopy[0], self.adjview[0])
275
276
        avcopy[2][1]['width'] = 8
277
        assert_not_equal(avcopy[2], self.adjview[2])
278
        self.adjview[2][1]['width'] = 8
279
        assert_equal(avcopy[2], self.adjview[2])
280
        del self.adjview[2][1]['width']
281
282
        assert_false(hasattr(self.adjview, '__setitem__'))
283
        assert_true(hasattr(avcopy, '__setitem__'))
284
285
286
class TestUnionMultiAdjacency(TestUnionAdjacency):
287
    # node->nbr->key->data
288
    def setup(self):
289
        dd = {'color': 'blue', 'weight': 1.2}
290
        self.kd = {7: {}, 8: {}, 9: {'color': 1}}
291
        self.nd = {3: self.kd, 0: {9: dd}, 1: {8: {}}, 2: {9: {'color': 1}}}
292
        self.s = {3: self.nd, 0: {3: {7: dd}}, 1: {}, 2: {3: {8: {}}}}
293
        self.p = {3: {}, 0: {3: {9: dd}}, 1: {}, 2: {1: {8: {}}}}
294
        self.adjview = nx.classes.coreviews.UnionMultiAdjacency(self.s, self.p)
295
296
    def test_getitem(self):
297
        assert_is_not(self.adjview[1], self.s[1])
298
        assert_is(self.adjview[3][0][9], self.adjview[0][3][9])
299
        assert_equal(self.adjview[3][2][9]['color'], 1)
300
        assert_raises(KeyError, self.adjview.__getitem__, 4)
301
302
    def test_copy(self):
303
        avcopy = self.adjview.copy()
304
        assert_equal(avcopy[0], self.adjview[0])
305
        assert_is_not(avcopy[0], self.adjview[0])
306
307
        avcopy[2][3][8]['ht'] = 4
308
        assert_not_equal(avcopy[2], self.adjview[2])
309
        self.adjview[2][3][8]['ht'] = 4
310
        assert_equal(avcopy[2], self.adjview[2])
311
        del self.adjview[2][3][8]['ht']
312
313
        assert_false(hasattr(self.adjview, '__setitem__'))
314
        assert_true(hasattr(avcopy, '__setitem__'))
315
316
317
class TestFilteredGraphs(object):
318
    def setup(self):
319
        self.Graphs = [nx.Graph,
320
                       nx.DiGraph,
321
                       nx.MultiGraph,
322
                       nx.MultiDiGraph]
323
        self.SubGraphs = [nx.graphviews.SubGraph,
324
                          nx.graphviews.SubDiGraph,
325
                          nx.graphviews.SubMultiGraph,
326
                          nx.graphviews.SubMultiDiGraph]
327
328
    def test_hide_show_nodes(self):
329
        for Graph, SubGraph in zip(self.Graphs, self.SubGraphs):
330
            G = nx.path_graph(4, Graph)
331
            SG = G.subgraph([2, 3])
332
            RG = SubGraph(G, nx.filters.hide_nodes([0, 1]))
333
            assert_equal(SG.nodes, RG.nodes)
334
            assert_equal(SG.edges, RG.edges)
335
            SGC = SG.copy()
336
            RGC = RG.copy()
337
            assert_equal(SGC.nodes, RGC.nodes)
338
            assert_equal(SGC.edges, RGC.edges)
339
340
    def test_str_repr(self):
341
        for Graph, SubGraph in zip(self.Graphs, self.SubGraphs):
342
            G = nx.path_graph(4, Graph)
343
            SG = G.subgraph([2, 3])
344
            RG = SubGraph(G, nx.filters.hide_nodes([0, 1]))
345
            str(SG.adj)
346
            str(RG.adj)
347
            repr(SG.adj)
348
            repr(RG.adj)
349
            str(SG.adj[2])
350
            str(RG.adj[2])
351
            repr(SG.adj[2])
352
            repr(RG.adj[2])
353
354
    def test_copy(self):
355
        for Graph, SubGraph in zip(self.Graphs, self.SubGraphs):
356
            G = nx.path_graph(4, Graph)
357
            SG = G.subgraph([2, 3])
358
            RG = SubGraph(G, nx.filters.hide_nodes([0, 1]))
359
            assert_equal(G.adj.copy(), G.adj)
360
            assert_equal(G.adj[2].copy(), G.adj[2])
361
            assert_equal(SG.adj.copy(), SG.adj)
362
            assert_equal(SG.adj[2].copy(), SG.adj[2])
363
            assert_equal(RG.adj.copy(), RG.adj)
364
            assert_equal(RG.adj[2].copy(), RG.adj[2])