Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (34.4 KB)

1
from nose.tools import assert_equal, assert_not_equal, \
2
    assert_true, assert_false, assert_raises, \
3
    assert_is, assert_is_not
4

    
5
import networkx as nx
6

    
7

    
8
# Nodes
9
class TestNodeView(object):
10
    def setup(self):
11
        self.G = nx.path_graph(9)
12
        self.nv = self.G.nodes   # NodeView(G)
13

    
14
    def test_pickle(self):
15
        import pickle
16
        nv = self.nv
17
        pnv = pickle.loads(pickle.dumps(nv, -1))
18
        assert_equal(nv, pnv)
19
        assert_equal(nv.__slots__, pnv.__slots__)
20

    
21
    def test_str(self):
22
        assert_equal(str(self.nv), "[0, 1, 2, 3, 4, 5, 6, 7, 8]")
23

    
24
    def test_repr(self):
25
        assert_equal(repr(self.nv), "NodeView((0, 1, 2, 3, 4, 5, 6, 7, 8))")
26

    
27
    def test_contains(self):
28
        nv = self.nv
29
        assert_true(7 in nv)
30
        assert_false(9 in nv)
31
        self.G.remove_node(7)
32
        self.G.add_node(9)
33
        assert_false(7 in nv)
34
        assert_true(9 in nv)
35

    
36
    def test_getitem(self):
37
        nv = self.nv
38
        self.G.nodes[3]['foo'] = 'bar'
39
        assert_equal(nv[7], {})
40
        assert_equal(nv[3], {'foo': 'bar'})
41

    
42
    def test_iter(self):
43
        nv = self.nv
44
        for i, n in enumerate(nv):
45
            assert_equal(i, n)
46
        inv = iter(nv)
47
        assert_equal(next(inv), 0)
48
        assert_not_equal(iter(nv), nv)
49
        assert_equal(iter(inv), inv)
50
        inv2 = iter(nv)
51
        next(inv2)
52
        assert_equal(list(inv), list(inv2))
53
        # odd case where NodeView calls NodeDataView with data=False
54
        nnv = nv(data=False)
55
        for i, n in enumerate(nnv):
56
            assert_equal(i, n)
57

    
58
    def test_call(self):
59
        nodes = self.nv
60
        assert_is(nodes, nodes())
61
        assert_is_not(nodes, nodes(data=True))
62
        assert_is_not(nodes, nodes(data='weight'))
63

    
64

    
65
class TestNodeDataView(object):
66
    def setup(self):
67
        self.G = nx.path_graph(9)
68
        self.nv = self.G.nodes.data()   # NodeDataView(G)
69
        self.ndv = self.G.nodes.data(True)
70
        self.nwv = self.G.nodes.data('foo')
71

    
72
    def test_viewtype(self):
73
        nv = self.G.nodes
74
        ndvfalse = nv.data(False)
75
        assert_is(nv, ndvfalse)
76
        assert_is_not(nv, self.ndv)
77

    
78
    def test_pickle(self):
79
        import pickle
80
        nv = self.nv
81
        pnv = pickle.loads(pickle.dumps(nv, -1))
82
        assert_equal(nv, pnv)
83
        assert_equal(nv.__slots__, pnv.__slots__)
84

    
85
    def test_str(self):
86
        msg = str([(n, {}) for n in range(9)])
87
        assert_equal(str(self.ndv), msg)
88

    
89
    def test_repr(self):
90
        msg = "NodeDataView({0: {}, 1: {}, 2: {}, 3: {}, " + \
91
              "4: {}, 5: {}, 6: {}, 7: {}, 8: {}})"
92
        assert_equal(repr(self.ndv), msg)
93

    
94
    def test_contains(self):
95
        self.G.nodes[3]['foo'] = 'bar'
96
        assert_true((7, {}) in self.nv)
97
        assert_true((3, {'foo': 'bar'}) in self.nv)
98
        assert_true((3, 'bar') in self.nwv)
99
        assert_true((7, None) in self.nwv)
100
        # default
101
        nwv_def = self.G.nodes(data='foo', default='biz')
102
        assert_true((7, 'biz') in nwv_def)
103
        assert_true((3, 'bar') in nwv_def)
104

    
105
    def test_getitem(self):
106
        self.G.nodes[3]['foo'] = 'bar'
107
        assert_equal(self.nv[3], {'foo': 'bar'})
108
        # default
109
        nwv_def = self.G.nodes(data='foo', default='biz')
110
        assert_true(nwv_def[7], 'biz')
111
        assert_equal(nwv_def[3], 'bar')
112

    
113
    def test_iter(self):
114
        nv = self.nv
115
        for i, (n, d) in enumerate(nv):
116
            assert_equal(i, n)
117
            assert_equal(d, {})
118
        inv = iter(nv)
119
        assert_equal(next(inv), (0, {}))
120
        self.G.nodes[3]['foo'] = 'bar'
121
        # default
122
        for n, d in nv:
123
            if n == 3:
124
                assert_equal(d, {'foo': 'bar'})
125
            else:
126
                assert_equal(d, {})
127
        # data=True
128
        for n, d in self.ndv:
129
            if n == 3:
130
                assert_equal(d, {'foo': 'bar'})
131
            else:
132
                assert_equal(d, {})
133
        # data='foo'
134
        for n, d in self.nwv:
135
            if n == 3:
136
                assert_equal(d, 'bar')
137
            else:
138
                assert_equal(d, None)
139
        # data='foo', default=1
140
        for n, d in self.G.nodes.data('foo', default=1):
141
            if n == 3:
142
                assert_equal(d, 'bar')
143
            else:
144
                assert_equal(d, 1)
145

    
146

    
147
def test_nodedataview_unhashable():
148
    G = nx.path_graph(9)
149
    G.nodes[3]['foo'] = 'bar'
150
    nvs = [G.nodes.data()]
151
    nvs.append(G.nodes.data(True))
152
    H = G.copy()
153
    H.nodes[4]['foo'] = {1, 2, 3}
154
    nvs.append(H.nodes.data(True))
155
    # raise unhashable
156
    for nv in nvs:
157
        assert_raises(TypeError, set, nv)
158
        assert_raises(TypeError, eval, 'nv | nv', locals())
159
    # no raise... hashable
160
    Gn = G.nodes.data(False)
161
    set(Gn)
162
    Gn | Gn
163
    Gn = G.nodes.data('foo')
164
    set(Gn)
165
    Gn | Gn
166

    
167

    
168
class TestNodeViewSetOps(object):
169
    def setUp(self):
170
        self.G = nx.path_graph(9)
171
        self.G.nodes[3]['foo'] = 'bar'
172
        self.nv = self.G.nodes
173

    
174
    def n_its(self, nodes):
175
        return {node for node in nodes}
176

    
177
    def test_len(self):
178
        nv = self.nv
179
        assert_equal(len(nv), 9)
180
        self.G.remove_node(7)
181
        assert_equal(len(nv), 8)
182
        self.G.add_node(9)
183
        assert_equal(len(nv), 9)
184

    
185
    def test_and(self):
186
        # print("G & H nodes:", gnv & hnv)
187
        nv = self.nv
188
        some_nodes = self.n_its(range(5, 12))
189
        assert_equal(nv & some_nodes, self.n_its(range(5, 9)))
190
        assert_equal(some_nodes & nv, self.n_its(range(5, 9)))
191

    
192
    def test_or(self):
193
        # print("G | H nodes:", gnv | hnv)
194
        nv = self.nv
195
        some_nodes = self.n_its(range(5, 12))
196
        assert_equal(nv | some_nodes, self.n_its(range(12)))
197
        assert_equal(some_nodes | nv, self.n_its(range(12)))
198

    
199
    def test_xor(self):
200
        # print("G ^ H nodes:", gnv ^ hnv)
201
        nv = self.nv
202
        some_nodes = self.n_its(range(5, 12))
203
        nodes = {0, 1, 2, 3, 4, 9, 10, 11}
204
        assert_equal(nv ^ some_nodes, self.n_its(nodes))
205
        assert_equal(some_nodes ^ nv, self.n_its(nodes))
206

    
207
    def test_sub(self):
208
        # print("G - H nodes:", gnv - hnv)
209
        nv = self.nv
210
        some_nodes = self.n_its(range(5, 12))
211
        assert_equal(nv - some_nodes, self.n_its(range(5)))
212
        assert_equal(some_nodes - nv, self.n_its(range(9, 12)))
213

    
214

    
215
class TestNodeDataViewSetOps(TestNodeViewSetOps):
216
    def setUp(self):
217
        self.G = nx.path_graph(9)
218
        self.G.nodes[3]['foo'] = 'bar'
219
        self.nv = self.G.nodes.data('foo')
220

    
221
    def n_its(self, nodes):
222
        return {(node, 'bar' if node == 3 else None) for node in nodes}
223

    
224

    
225
class TestNodeDataViewDefaultSetOps(TestNodeDataViewSetOps):
226
    def setUp(self):
227
        self.G = nx.path_graph(9)
228
        self.G.nodes[3]['foo'] = 'bar'
229
        self.nv = self.G.nodes.data('foo', default=1)
230

    
231
    def n_its(self, nodes):
232
        return {(node, 'bar' if node == 3 else 1) for node in nodes}
233

    
234

    
235
# Edges Data View
236
class TestEdgeDataView(object):
237
    def setUp(self):
238
        self.G = nx.path_graph(9)
239
        self.eview = nx.reportviews.EdgeView
240

    
241
    def test_pickle(self):
242
        import pickle
243
        ev = self.eview(self.G)(data=True)
244
        pev = pickle.loads(pickle.dumps(ev, -1))
245
        assert_equal(list(ev), list(pev))
246
        assert_equal(ev.__slots__, pev.__slots__)
247

    
248
    def modify_edge(self, G, e, **kwds):
249
        self.G._adj[e[0]][e[1]].update(kwds)
250

    
251
    def test_str(self):
252
        ev = self.eview(self.G)(data=True)
253
        rep = str([(n, n + 1, {}) for n in range(8)])
254
        assert_equal(str(ev), rep)
255

    
256
    def test_repr(self):
257
        ev = self.eview(self.G)(data=True)
258
        rep = "EdgeDataView([(0, 1, {}), (1, 2, {}), " + \
259
              "(2, 3, {}), (3, 4, {}), " + \
260
              "(4, 5, {}), (5, 6, {}), " + \
261
              "(6, 7, {}), (7, 8, {})])"
262
        assert_equal(repr(ev), rep)
263

    
264
    def test_iterdata(self):
265
        G = self.G
266
        evr = self.eview(G)
267
        ev = evr(data=True)
268
        ev_def = evr(data='foo', default=1)
269

    
270
        for u, v, d in ev:
271
            pass
272
        assert_equal(d, {})
273

    
274
        for u, v, wt in ev_def:
275
            pass
276
        assert_equal(wt, 1)
277

    
278
        self.modify_edge(G, (2, 3), foo='bar')
279
        for e in ev:
280
            assert_equal(len(e), 3)
281
            if set(e[:2]) == {2, 3}:
282
                assert_equal(e[2], {'foo': 'bar'})
283
                checked = True
284
            else:
285
                assert_equal(e[2], {})
286
        assert_true(checked)
287

    
288
        for e in ev_def:
289
            assert_equal(len(e), 3)
290
            if set(e[:2]) == {2, 3}:
291
                assert_equal(e[2], 'bar')
292
                checked_wt = True
293
            else:
294
                assert_equal(e[2], 1)
295
        assert_true(checked_wt)
296

    
297
    def test_iter(self):
298
        evr = self.eview(self.G)
299
        ev = evr()
300
        for u, v in ev:
301
            pass
302
        iev = iter(ev)
303
        assert_equal(next(iev), (0, 1))
304
        assert_not_equal(iter(ev), ev)
305
        assert_equal(iter(iev), iev)
306

    
307
    def test_contains(self):
308
        evr = self.eview(self.G)
309
        ev = evr()
310
        if self.G.is_directed():
311
            assert_true((1, 2) in ev and (2, 1) not in ev)
312
        else:
313
            assert_true((1, 2) in ev and (2, 1) in ev)
314
        assert_false((1, 4) in ev)
315
        assert_false((1, 90) in ev)
316
        assert_false((90, 1) in ev)
317

    
318
    def test_len(self):
319
        evr = self.eview(self.G)
320
        ev = evr(data='foo')
321
        assert_equal(len(ev), 8)
322
        assert_equal(len(evr(1)), 2)
323
        assert_equal(len(evr([1, 2, 3])), 4)
324

    
325
        assert_equal(len(self.G.edges(1)), 2)
326
        assert_equal(len(self.G.edges()), 8)
327
        assert_equal(len(self.G.edges), 8)
328

    
329
        H = self.G.copy()
330
        H.add_edge(1, 1)
331
        assert_equal(len(H.edges(1)), 3)
332
        assert_equal(len(H.edges()), 9)
333
        assert_equal(len(H.edges), 9)
334

    
335

    
336
class TestOutEdgeDataView(TestEdgeDataView):
337
    def setUp(self):
338
        self.G = nx.path_graph(9, create_using=nx.DiGraph())
339
        self.eview = nx.reportviews.OutEdgeView
340

    
341
    def test_repr(self):
342
        ev = self.eview(self.G)(data=True)
343
        rep = "OutEdgeDataView([(0, 1, {}), (1, 2, {}), " + \
344
              "(2, 3, {}), (3, 4, {}), " + \
345
              "(4, 5, {}), (5, 6, {}), " + \
346
              "(6, 7, {}), (7, 8, {})])"
347
        assert_equal(repr(ev), rep)
348

    
349
    def test_len(self):
350
        evr = self.eview(self.G)
351
        ev = evr(data='foo')
352
        assert_equal(len(ev), 8)
353
        assert_equal(len(evr(1)), 1)
354
        assert_equal(len(evr([1, 2, 3])), 3)
355

    
356
        assert_equal(len(self.G.edges(1)), 1)
357
        assert_equal(len(self.G.edges()), 8)
358
        assert_equal(len(self.G.edges), 8)
359

    
360
        H = self.G.copy()
361
        H.add_edge(1, 1)
362
        assert_equal(len(H.edges(1)), 2)
363
        assert_equal(len(H.edges()), 9)
364
        assert_equal(len(H.edges), 9)
365

    
366

    
367
class TestInEdgeDataView(TestOutEdgeDataView):
368
    def setUp(self):
369
        self.G = nx.path_graph(9, create_using=nx.DiGraph())
370
        self.eview = nx.reportviews.InEdgeView
371

    
372
    def test_repr(self):
373
        ev = self.eview(self.G)(data=True)
374
        rep = "InEdgeDataView([(0, 1, {}), (1, 2, {}), " + \
375
              "(2, 3, {}), (3, 4, {}), " + \
376
              "(4, 5, {}), (5, 6, {}), " + \
377
              "(6, 7, {}), (7, 8, {})])"
378
        assert_equal(repr(ev), rep)
379

    
380

    
381
class TestMultiEdgeDataView(TestEdgeDataView):
382
    def setUp(self):
383
        self.G = nx.path_graph(9, create_using=nx.MultiGraph())
384
        self.eview = nx.reportviews.MultiEdgeView
385

    
386
    def modify_edge(self, G, e, **kwds):
387
        self.G._adj[e[0]][e[1]][0].update(kwds)
388

    
389
    def test_repr(self):
390
        ev = self.eview(self.G)(data=True)
391
        rep = "MultiEdgeDataView([(0, 1, {}), (1, 2, {}), " + \
392
              "(2, 3, {}), (3, 4, {}), " + \
393
              "(4, 5, {}), (5, 6, {}), " + \
394
              "(6, 7, {}), (7, 8, {})])"
395
        assert_equal(repr(ev), rep)
396

    
397

    
398
class TestOutMultiEdgeDataView(TestOutEdgeDataView):
399
    def setUp(self):
400
        self.G = nx.path_graph(9, create_using=nx.MultiDiGraph())
401
        self.eview = nx.reportviews.OutMultiEdgeView
402

    
403
    def modify_edge(self, G, e, **kwds):
404
        self.G._adj[e[0]][e[1]][0].update(kwds)
405

    
406
    def test_repr(self):
407
        ev = self.eview(self.G)(data=True)
408
        rep = "OutMultiEdgeDataView([(0, 1, {}), (1, 2, {}), " + \
409
              "(2, 3, {}), (3, 4, {}), " + \
410
              "(4, 5, {}), (5, 6, {}), " + \
411
              "(6, 7, {}), (7, 8, {})])"
412
        assert_equal(repr(ev), rep)
413

    
414

    
415
class TestInMultiEdgeDataView(TestOutMultiEdgeDataView):
416
    def setUp(self):
417
        self.G = nx.path_graph(9, create_using=nx.MultiDiGraph())
418
        self.eview = nx.reportviews.InMultiEdgeView
419

    
420
    def test_repr(self):
421
        ev = self.eview(self.G)(data=True)
422
        rep = "InMultiEdgeDataView([(0, 1, {}), (1, 2, {}), " + \
423
              "(2, 3, {}), (3, 4, {}), " + \
424
              "(4, 5, {}), (5, 6, {}), " + \
425
              "(6, 7, {}), (7, 8, {})])"
426
        assert_equal(repr(ev), rep)
427

    
428

    
429
# Edge Views
430
class TestEdgeView(object):
431
    def setup(self):
432
        self.G = nx.path_graph(9)
433
        self.eview = nx.reportviews.EdgeView
434

    
435
    def test_pickle(self):
436
        import pickle
437
        ev = self.eview(self.G)
438
        pev = pickle.loads(pickle.dumps(ev, -1))
439
        assert_equal(ev, pev)
440
        assert_equal(ev.__slots__, pev.__slots__)
441

    
442
    def modify_edge(self, G, e, **kwds):
443
        self.G._adj[e[0]][e[1]].update(kwds)
444

    
445
    def test_str(self):
446
        ev = self.eview(self.G)
447
        rep = str([(n, n + 1) for n in range(8)])
448
        assert_equal(str(ev), rep)
449

    
450
    def test_repr(self):
451
        ev = self.eview(self.G)
452
        rep = "EdgeView([(0, 1), (1, 2), (2, 3), (3, 4), " + \
453
            "(4, 5), (5, 6), (6, 7), (7, 8)])"
454
        assert_equal(repr(ev), rep)
455

    
456
    def test_call(self):
457
        ev = self.eview(self.G)
458
        assert_equal(id(ev), id(ev()))
459
        assert_equal(id(ev), id(ev(data=False)))
460
        assert_not_equal(id(ev), id(ev(data=True)))
461
        assert_not_equal(id(ev), id(ev(nbunch=1)))
462

    
463
    def test_data(self):
464
        ev = self.eview(self.G)
465
        assert_not_equal(id(ev), id(ev.data()))
466
        assert_equal(id(ev), id(ev.data(data=False)))
467
        assert_not_equal(id(ev), id(ev.data(data=True)))
468
        assert_not_equal(id(ev), id(ev.data(nbunch=1)))
469

    
470
    def test_iter(self):
471
        ev = self.eview(self.G)
472
        for u, v in ev:
473
            pass
474
        iev = iter(ev)
475
        assert_equal(next(iev), (0, 1))
476
        assert_not_equal(iter(ev), ev)
477
        assert_equal(iter(iev), iev)
478

    
479
    def test_contains(self):
480
        ev = self.eview(self.G)
481
        edv = ev()
482
        if self.G.is_directed():
483
            assert_true((1, 2) in ev and (2, 1) not in ev)
484
            assert_true((1, 2) in edv and (2, 1) not in edv)
485
        else:
486
            assert_true((1, 2) in ev and (2, 1) in ev)
487
            assert_true((1, 2) in edv and (2, 1) in edv)
488
        assert_false((1, 4) in ev)
489
        assert_false((1, 4) in edv)
490
        # edge not in graph
491
        assert_false((1, 90) in ev)
492
        assert_false((90, 1) in ev)
493
        assert_false((1, 90) in edv)
494
        assert_false((90, 1) in edv)
495

    
496
    def test_len(self):
497
        ev = self.eview(self.G)
498
        num_ed = 9 if self.G.is_multigraph() else 8
499
        assert_equal(len(ev), num_ed)
500

    
501
        H = self.G.copy()
502
        H.add_edge(1, 1)
503
        assert_equal(len(H.edges(1)), 3 + H.is_multigraph() - H.is_directed())
504
        assert_equal(len(H.edges()), num_ed + 1)
505
        assert_equal(len(H.edges), num_ed + 1)
506

    
507
    def test_and(self):
508
        # print("G & H edges:", gnv & hnv)
509
        ev = self.eview(self.G)
510
        some_edges = {(0, 1), (1, 0), (0, 2)}
511
        if self.G.is_directed():
512
            assert_true(some_edges & ev, {(0, 1)})
513
            assert_true(ev & some_edges, {(0, 1)})
514
        else:
515
            assert_equal(ev & some_edges, {(0, 1), (1, 0)})
516
            assert_equal(some_edges & ev, {(0, 1), (1, 0)})
517
        return
518

    
519
    def test_or(self):
520
        # print("G | H edges:", gnv | hnv)
521
        ev = self.eview(self.G)
522
        some_edges = {(0, 1), (1, 0), (0, 2)}
523
        result1 = {(n, n + 1) for n in range(8)}
524
        result1.update(some_edges)
525
        result2 = {(n + 1, n) for n in range(8)}
526
        result2.update(some_edges)
527
        assert_true((ev | some_edges) in (result1, result2))
528
        assert_true((some_edges | ev) in (result1, result2))
529

    
530
    def test_xor(self):
531
        # print("G ^ H edges:", gnv ^ hnv)
532
        ev = self.eview(self.G)
533
        some_edges = {(0, 1), (1, 0), (0, 2)}
534
        if self.G.is_directed():
535
            result = {(n, n + 1) for n in range(1, 8)}
536
            result.update({(1, 0), (0, 2)})
537
            assert_equal(ev ^ some_edges, result)
538
        else:
539
            result = {(n, n + 1) for n in range(1, 8)}
540
            result.update({(0, 2)})
541
            assert_equal(ev ^ some_edges, result)
542
        return
543

    
544
    def test_sub(self):
545
        # print("G - H edges:", gnv - hnv)
546
        ev = self.eview(self.G)
547
        some_edges = {(0, 1), (1, 0), (0, 2)}
548
        result = {(n, n + 1) for n in range(8)}
549
        result.remove((0, 1))
550
        assert_true(ev - some_edges, result)
551

    
552

    
553
class TestOutEdgeView(TestEdgeView):
554
    def setup(self):
555
        self.G = nx.path_graph(9, nx.DiGraph())
556
        self.eview = nx.reportviews.OutEdgeView
557

    
558
    def test_repr(self):
559
        ev = self.eview(self.G)
560
        rep = "OutEdgeView([(0, 1), (1, 2), (2, 3), (3, 4), " + \
561
            "(4, 5), (5, 6), (6, 7), (7, 8)])"
562
        assert_equal(repr(ev), rep)
563

    
564

    
565
class TestInEdgeView(TestEdgeView):
566
    def setup(self):
567
        self.G = nx.path_graph(9, nx.DiGraph())
568
        self.eview = nx.reportviews.InEdgeView
569

    
570
    def test_repr(self):
571
        ev = self.eview(self.G)
572
        rep = "InEdgeView([(0, 1), (1, 2), (2, 3), (3, 4), " + \
573
            "(4, 5), (5, 6), (6, 7), (7, 8)])"
574
        assert_equal(repr(ev), rep)
575

    
576

    
577
class TestMultiEdgeView(TestEdgeView):
578
    def setup(self):
579
        self.G = nx.path_graph(9, nx.MultiGraph())
580
        self.G.add_edge(1, 2, key=3, foo='bar')
581
        self.eview = nx.reportviews.MultiEdgeView
582

    
583
    def modify_edge(self, G, e, **kwds):
584
        if len(e) == 2:
585
            e = e + (0,)
586
        self.G._adj[e[0]][e[1]][e[2]].update(kwds)
587

    
588
    def test_str(self):
589
        ev = self.eview(self.G)
590
        replist = [(n, n + 1, 0) for n in range(8)]
591
        replist.insert(2, (1, 2, 3))
592
        rep = str(replist)
593
        assert_equal(str(ev), rep)
594

    
595
    def test_repr(self):
596
        ev = self.eview(self.G)
597
        rep = "MultiEdgeView([(0, 1, 0), (1, 2, 0), (1, 2, 3), (2, 3, 0), " + \
598
            "(3, 4, 0), (4, 5, 0), (5, 6, 0), (6, 7, 0), (7, 8, 0)])"
599
        assert_equal(repr(ev), rep)
600

    
601
    def test_call(self):
602
        ev = self.eview(self.G)
603
        assert_equal(id(ev), id(ev(keys=True)))
604
        assert_equal(id(ev), id(ev(data=False, keys=True)))
605
        assert_not_equal(id(ev), id(ev(keys=False)))
606
        assert_not_equal(id(ev), id(ev(data=True)))
607
        assert_not_equal(id(ev), id(ev(nbunch=1)))
608

    
609
    def test_data(self):
610
        ev = self.eview(self.G)
611
        assert_not_equal(id(ev), id(ev.data()))
612
        assert_equal(id(ev), id(ev.data(data=False, keys=True)))
613
        assert_not_equal(id(ev), id(ev.data(keys=False)))
614
        assert_not_equal(id(ev), id(ev.data(data=True)))
615
        assert_not_equal(id(ev), id(ev.data(nbunch=1)))
616

    
617
    def test_iter(self):
618
        ev = self.eview(self.G)
619
        for u, v, k in ev:
620
            pass
621
        iev = iter(ev)
622
        assert_equal(next(iev), (0, 1, 0))
623
        assert_not_equal(iter(ev), ev)
624
        assert_equal(iter(iev), iev)
625

    
626
    def test_iterkeys(self):
627
        G = self.G
628
        evr = self.eview(G)
629
        ev = evr(keys=True)
630
        for u, v, k in ev:
631
            pass
632
        assert_equal(k, 0)
633
        ev = evr(keys=True, data="foo", default=1)
634
        for u, v, k, wt in ev:
635
            pass
636
        assert_equal(wt, 1)
637

    
638
        self.modify_edge(G, (2, 3, 0), foo='bar')
639
        ev = evr(keys=True, data=True)
640
        for e in ev:
641
            assert_equal(len(e), 4)
642
            print('edge:', e)
643
            if set(e[:2]) == {2, 3}:
644
                print(self.G._adj[2][3])
645
                assert_equal(e[2], 0)
646
                assert_equal(e[3], {'foo': 'bar'})
647
                checked = True
648
            elif set(e[:3]) == {1, 2, 3}:
649
                assert_equal(e[2], 3)
650
                assert_equal(e[3], {'foo': 'bar'})
651
                checked_multi = True
652
            else:
653
                assert_equal(e[2], 0)
654
                assert_equal(e[3], {})
655
        assert_true(checked)
656
        assert_true(checked_multi)
657
        ev = evr(keys=True, data='foo', default=1)
658
        for e in ev:
659
            if set(e[:2]) == {1, 2} and e[2] == 3:
660
                assert_equal(e[3], 'bar')
661
            if set(e[:2]) == {1, 2} and e[2] == 0:
662
                assert_equal(e[3], 1)
663
            if set(e[:2]) == {2, 3}:
664
                assert_equal(e[2], 0)
665
                assert_equal(e[3], 'bar')
666
                assert_equal(len(e), 4)
667
                checked_wt = True
668
        assert_true(checked_wt)
669
        ev = evr(keys=True)
670
        for e in ev:
671
            assert_equal(len(e), 3)
672
        elist = sorted([(i, i + 1, 0) for i in range(8)] + [(1, 2, 3)])
673
        assert_equal(sorted(list(ev)), elist)
674
        # test order of arguments:graph, nbunch, data, keys, default
675
        ev = evr((1, 2), 'foo', True, 1)
676
        for e in ev:
677
            if set(e[:2]) == {1, 2}:
678
                assert_true(e[2] in {0, 3})
679
                if e[2] == 3:
680
                    assert_equal(e[3], 'bar')
681
                else:  # e[2] == 0
682
                    assert_equal(e[3], 1)
683
        if G.is_directed():
684
            assert_equal(len(list(ev)), 3)
685
        else:
686
            assert_equal(len(list(ev)), 4)
687

    
688
    def test_or(self):
689
        # print("G | H edges:", gnv | hnv)
690
        ev = self.eview(self.G)
691
        some_edges = {(0, 1, 0), (1, 0, 0), (0, 2, 0)}
692
        result = {(n, n + 1, 0) for n in range(8)}
693
        result.update(some_edges)
694
        result.update({(1, 2, 3)})
695
        assert_equal(ev | some_edges, result)
696
        assert_equal(some_edges | ev, result)
697

    
698
    def test_sub(self):
699
        # print("G - H edges:", gnv - hnv)
700
        ev = self.eview(self.G)
701
        some_edges = {(0, 1, 0), (1, 0, 0), (0, 2, 0)}
702
        result = {(n, n + 1, 0) for n in range(8)}
703
        result.remove((0, 1, 0))
704
        result.update({(1, 2, 3)})
705
        assert_true(ev - some_edges, result)
706
        assert_true(some_edges - ev, result)
707

    
708
    def test_xor(self):
709
        # print("G ^ H edges:", gnv ^ hnv)
710
        ev = self.eview(self.G)
711
        some_edges = {(0, 1, 0), (1, 0, 0), (0, 2, 0)}
712
        if self.G.is_directed():
713
            result = {(n, n + 1, 0) for n in range(1, 8)}
714
            result.update({(1, 0, 0), (0, 2, 0), (1, 2, 3)})
715
            assert_equal(ev ^ some_edges, result)
716
            assert_equal(some_edges ^ ev, result)
717
        else:
718
            result = {(n, n + 1, 0) for n in range(1, 8)}
719
            result.update({(0, 2, 0), (1, 2, 3)})
720
            assert_equal(ev ^ some_edges, result)
721
            assert_equal(some_edges ^ ev, result)
722

    
723
    def test_and(self):
724
        # print("G & H edges:", gnv & hnv)
725
        ev = self.eview(self.G)
726
        some_edges = {(0, 1, 0), (1, 0, 0), (0, 2, 0)}
727
        if self.G.is_directed():
728
            assert_equal(ev & some_edges, {(0, 1, 0)})
729
            assert_equal(some_edges & ev, {(0, 1, 0)})
730
        else:
731
            assert_equal(ev & some_edges, {(0, 1, 0), (1, 0, 0)})
732
            assert_equal(some_edges & ev, {(0, 1, 0), (1, 0, 0)})
733

    
734

    
735
class TestOutMultiEdgeView(TestMultiEdgeView):
736
    def setup(self):
737
        self.G = nx.path_graph(9, nx.MultiDiGraph())
738
        self.G.add_edge(1, 2, key=3, foo='bar')
739
        self.eview = nx.reportviews.OutMultiEdgeView
740

    
741
    def modify_edge(self, G, e, **kwds):
742
        if len(e) == 2:
743
            e = e + (0,)
744
        self.G._adj[e[0]][e[1]][e[2]].update(kwds)
745

    
746
    def test_repr(self):
747
        ev = self.eview(self.G)
748
        rep = "OutMultiEdgeView([(0, 1, 0), (1, 2, 0), (1, 2, 3), (2, 3, 0),"\
749
              + " (3, 4, 0), (4, 5, 0), (5, 6, 0), (6, 7, 0), (7, 8, 0)])"
750
        assert_equal(repr(ev), rep)
751

    
752

    
753
class TestInMultiEdgeView(TestMultiEdgeView):
754
    def setup(self):
755
        self.G = nx.path_graph(9, nx.MultiDiGraph())
756
        self.G.add_edge(1, 2, key=3, foo='bar')
757
        self.eview = nx.reportviews.InMultiEdgeView
758

    
759
    def modify_edge(self, G, e, **kwds):
760
        if len(e) == 2:
761
            e = e + (0,)
762
        self.G._adj[e[0]][e[1]][e[2]].update(kwds)
763

    
764
    def test_repr(self):
765
        ev = self.eview(self.G)
766
        rep = "InMultiEdgeView([(0, 1, 0), (1, 2, 0), (1, 2, 3), (2, 3, 0), "\
767
              + "(3, 4, 0), (4, 5, 0), (5, 6, 0), (6, 7, 0), (7, 8, 0)])"
768
        assert_equal(repr(ev), rep)
769

    
770

    
771
# Degrees
772
class TestDegreeView(object):
773
    GRAPH = nx.Graph
774
    dview = nx.reportviews.DegreeView
775

    
776
    def setup(self):
777
        self.G = nx.path_graph(6, self.GRAPH())
778
        self.G.add_edge(1, 3, foo=2)
779
        self.G.add_edge(1, 3, foo=3)
780

    
781
    def test_pickle(self):
782
        import pickle
783
        deg = self.G.degree
784
        pdeg = pickle.loads(pickle.dumps(deg, -1))
785
        assert_equal(dict(deg), dict(pdeg))
786

    
787
    def test_str(self):
788
        dv = self.dview(self.G)
789
        rep = str([(0, 1), (1, 3), (2, 2), (3, 3), (4, 2), (5, 1)])
790
        assert_equal(str(dv), rep)
791
        dv = self.G.degree()
792
        assert_equal(str(dv), rep)
793

    
794
    def test_repr(self):
795
        dv = self.dview(self.G)
796
        rep = "DegreeView({0: 1, 1: 3, 2: 2, 3: 3, 4: 2, 5: 1})"
797
        assert_equal(repr(dv), rep)
798

    
799
    def test_iter(self):
800
        dv = self.dview(self.G)
801
        for n, d in dv:
802
            pass
803
        idv = iter(dv)
804
        assert_not_equal(iter(dv), dv)
805
        assert_equal(iter(idv), idv)
806
        assert_equal(next(idv), (0, dv[0]))
807
        assert_equal(next(idv), (1, dv[1]))
808
        # weighted
809
        dv = self.dview(self.G, weight='foo')
810
        for n, d in dv:
811
            pass
812
        idv = iter(dv)
813
        assert_not_equal(iter(dv), dv)
814
        assert_equal(iter(idv), idv)
815
        assert_equal(next(idv), (0, dv[0]))
816
        assert_equal(next(idv), (1, dv[1]))
817

    
818
    def test_nbunch(self):
819
        dv = self.dview(self.G)
820
        dvn = dv(0)
821
        assert_equal(dvn, 1)
822
        dvn = dv([2, 3])
823
        assert_equal(sorted(dvn), [(2, 2), (3, 3)])
824

    
825
    def test_getitem(self):
826
        dv = self.dview(self.G)
827
        assert_equal(dv[0], 1)
828
        assert_equal(dv[1], 3)
829
        assert_equal(dv[2], 2)
830
        assert_equal(dv[3], 3)
831
        dv = self.dview(self.G, weight='foo')
832
        assert_equal(dv[0], 1)
833
        assert_equal(dv[1], 5)
834
        assert_equal(dv[2], 2)
835
        assert_equal(dv[3], 5)
836

    
837
    def test_weight(self):
838
        dv = self.dview(self.G)
839
        dvw = dv(0, weight='foo')
840
        assert_equal(dvw, 1)
841
        dvw = dv(1, weight='foo')
842
        assert_equal(dvw, 5)
843
        dvw = dv([2, 3], weight='foo')
844
        assert_equal(sorted(dvw), [(2, 2), (3, 5)])
845
        dvd = dict(dv(weight='foo'))
846
        assert_equal(dvd[0], 1)
847
        assert_equal(dvd[1], 5)
848
        assert_equal(dvd[2], 2)
849
        assert_equal(dvd[3], 5)
850

    
851
    def test_len(self):
852
        dv = self.dview(self.G)
853
        assert_equal(len(dv), 6)
854

    
855

    
856
class TestDiDegreeView(TestDegreeView):
857
    GRAPH = nx.DiGraph
858
    dview = nx.reportviews.DiDegreeView
859

    
860
    def test_repr(self):
861
        dv = self.G.degree()
862
        rep = "DiDegreeView({0: 1, 1: 3, 2: 2, 3: 3, 4: 2, 5: 1})"
863
        assert_equal(repr(dv), rep)
864

    
865

    
866
class TestOutDegreeView(TestDegreeView):
867
    GRAPH = nx.DiGraph
868
    dview = nx.reportviews.OutDegreeView
869

    
870
    def test_str(self):
871
        dv = self.dview(self.G)
872
        rep = str([(0, 1), (1, 2), (2, 1), (3, 1), (4, 1), (5, 0)])
873
        assert_equal(str(dv), rep)
874
        dv = self.G.out_degree()
875
        assert_equal(str(dv), rep)
876

    
877
    def test_repr(self):
878
        dv = self.G.out_degree()
879
        rep = "OutDegreeView({0: 1, 1: 2, 2: 1, 3: 1, 4: 1, 5: 0})"
880
        assert_equal(repr(dv), rep)
881

    
882
    def test_nbunch(self):
883
        dv = self.dview(self.G)
884
        dvn = dv(0)
885
        assert_equal(dvn, 1)
886
        dvn = dv([2, 3])
887
        assert_equal(sorted(dvn), [(2, 1), (3, 1)])
888

    
889
    def test_getitem(self):
890
        dv = self.dview(self.G)
891
        assert_equal(dv[0], 1)
892
        assert_equal(dv[1], 2)
893
        assert_equal(dv[2], 1)
894
        assert_equal(dv[3], 1)
895
        dv = self.dview(self.G, weight='foo')
896
        assert_equal(dv[0], 1)
897
        assert_equal(dv[1], 4)
898
        assert_equal(dv[2], 1)
899
        assert_equal(dv[3], 1)
900

    
901
    def test_weight(self):
902
        dv = self.dview(self.G)
903
        dvw = dv(0, weight='foo')
904
        assert_equal(dvw, 1)
905
        dvw = dv(1, weight='foo')
906
        assert_equal(dvw, 4)
907
        dvw = dv([2, 3], weight='foo')
908
        assert_equal(sorted(dvw), [(2, 1), (3, 1)])
909
        dvd = dict(dv(weight='foo'))
910
        assert_equal(dvd[0], 1)
911
        assert_equal(dvd[1], 4)
912
        assert_equal(dvd[2], 1)
913
        assert_equal(dvd[3], 1)
914

    
915

    
916
class TestInDegreeView(TestDegreeView):
917
    GRAPH = nx.DiGraph
918
    dview = nx.reportviews.InDegreeView
919

    
920
    def test_str(self):
921
        dv = self.dview(self.G)
922
        rep = str([(0, 0), (1, 1), (2, 1), (3, 2), (4, 1), (5, 1)])
923
        assert_equal(str(dv), rep)
924
        dv = self.G.in_degree()
925
        assert_equal(str(dv), rep)
926

    
927
    def test_repr(self):
928
        dv = self.G.in_degree()
929
        rep = "InDegreeView({0: 0, 1: 1, 2: 1, 3: 2, 4: 1, 5: 1})"
930
        assert_equal(repr(dv), rep)
931

    
932
    def test_nbunch(self):
933
        dv = self.dview(self.G)
934
        dvn = dv(0)
935
        assert_equal(dvn, 0)
936
        dvn = dv([2, 3])
937
        assert_equal(sorted(dvn), [(2, 1), (3, 2)])
938

    
939
    def test_getitem(self):
940
        dv = self.dview(self.G)
941
        assert_equal(dv[0], 0)
942
        assert_equal(dv[1], 1)
943
        assert_equal(dv[2], 1)
944
        assert_equal(dv[3], 2)
945
        dv = self.dview(self.G, weight='foo')
946
        assert_equal(dv[0], 0)
947
        assert_equal(dv[1], 1)
948
        assert_equal(dv[2], 1)
949
        assert_equal(dv[3], 4)
950

    
951
    def test_weight(self):
952
        dv = self.dview(self.G)
953
        dvw = dv(0, weight='foo')
954
        assert_equal(dvw, 0)
955
        dvw = dv(1, weight='foo')
956
        assert_equal(dvw, 1)
957
        dvw = dv([2, 3], weight='foo')
958
        assert_equal(sorted(dvw), [(2, 1), (3, 4)])
959
        dvd = dict(dv(weight='foo'))
960
        assert_equal(dvd[0], 0)
961
        assert_equal(dvd[1], 1)
962
        assert_equal(dvd[2], 1)
963
        assert_equal(dvd[3], 4)
964

    
965

    
966
class TestMultiDegreeView(TestDegreeView):
967
    GRAPH = nx.MultiGraph
968
    dview = nx.reportviews.MultiDegreeView
969

    
970
    def test_str(self):
971
        dv = self.dview(self.G)
972
        rep = str([(0, 1), (1, 4), (2, 2), (3, 4), (4, 2), (5, 1)])
973
        assert_equal(str(dv), rep)
974
        dv = self.G.degree()
975
        assert_equal(str(dv), rep)
976

    
977
    def test_repr(self):
978
        dv = self.G.degree()
979
        rep = "MultiDegreeView({0: 1, 1: 4, 2: 2, 3: 4, 4: 2, 5: 1})"
980
        assert_equal(repr(dv), rep)
981

    
982
    def test_nbunch(self):
983
        dv = self.dview(self.G)
984
        dvn = dv(0)
985
        assert_equal(dvn, 1)
986
        dvn = dv([2, 3])
987
        assert_equal(sorted(dvn), [(2, 2), (3, 4)])
988

    
989
    def test_getitem(self):
990
        dv = self.dview(self.G)
991
        assert_equal(dv[0], 1)
992
        assert_equal(dv[1], 4)
993
        assert_equal(dv[2], 2)
994
        assert_equal(dv[3], 4)
995
        dv = self.dview(self.G, weight='foo')
996
        assert_equal(dv[0], 1)
997
        assert_equal(dv[1], 7)
998
        assert_equal(dv[2], 2)
999
        assert_equal(dv[3], 7)
1000

    
1001
    def test_weight(self):
1002
        dv = self.dview(self.G)
1003
        dvw = dv(0, weight='foo')
1004
        assert_equal(dvw, 1)
1005
        dvw = dv(1, weight='foo')
1006
        assert_equal(dvw, 7)
1007
        dvw = dv([2, 3], weight='foo')
1008
        assert_equal(sorted(dvw), [(2, 2), (3, 7)])
1009
        dvd = dict(dv(weight='foo'))
1010
        assert_equal(dvd[0], 1)
1011
        assert_equal(dvd[1], 7)
1012
        assert_equal(dvd[2], 2)
1013
        assert_equal(dvd[3], 7)
1014

    
1015

    
1016
class TestDiMultiDegreeView(TestMultiDegreeView):
1017
    GRAPH = nx.MultiDiGraph
1018
    dview = nx.reportviews.DiMultiDegreeView
1019

    
1020
    def test_repr(self):
1021
        dv = self.G.degree()
1022
        rep = "DiMultiDegreeView({0: 1, 1: 4, 2: 2, 3: 4, 4: 2, 5: 1})"
1023
        assert_equal(repr(dv), rep)
1024

    
1025

    
1026
class TestOutMultiDegreeView(TestDegreeView):
1027
    GRAPH = nx.MultiDiGraph
1028
    dview = nx.reportviews.OutMultiDegreeView
1029

    
1030
    def test_str(self):
1031
        dv = self.dview(self.G)
1032
        rep = str([(0, 1), (1, 3), (2, 1), (3, 1), (4, 1), (5, 0)])
1033
        assert_equal(str(dv), rep)
1034
        dv = self.G.out_degree()
1035
        assert_equal(str(dv), rep)
1036

    
1037
    def test_repr(self):
1038
        dv = self.G.out_degree()
1039
        rep = "OutMultiDegreeView({0: 1, 1: 3, 2: 1, 3: 1, 4: 1, 5: 0})"
1040
        assert_equal(repr(dv), rep)
1041

    
1042
    def test_nbunch(self):
1043
        dv = self.dview(self.G)
1044
        dvn = dv(0)
1045
        assert_equal(dvn, 1)
1046
        dvn = dv([2, 3])
1047
        assert_equal(sorted(dvn), [(2, 1), (3, 1)])
1048

    
1049
    def test_getitem(self):
1050
        dv = self.dview(self.G)
1051
        assert_equal(dv[0], 1)
1052
        assert_equal(dv[1], 3)
1053
        assert_equal(dv[2], 1)
1054
        assert_equal(dv[3], 1)
1055
        dv = self.dview(self.G, weight='foo')
1056
        assert_equal(dv[0], 1)
1057
        assert_equal(dv[1], 6)
1058
        assert_equal(dv[2], 1)
1059
        assert_equal(dv[3], 1)
1060

    
1061
    def test_weight(self):
1062
        dv = self.dview(self.G)
1063
        dvw = dv(0, weight='foo')
1064
        assert_equal(dvw, 1)
1065
        dvw = dv(1, weight='foo')
1066
        assert_equal(dvw, 6)
1067
        dvw = dv([2, 3], weight='foo')
1068
        assert_equal(sorted(dvw), [(2, 1), (3, 1)])
1069
        dvd = dict(dv(weight='foo'))
1070
        assert_equal(dvd[0], 1)
1071
        assert_equal(dvd[1], 6)
1072
        assert_equal(dvd[2], 1)
1073
        assert_equal(dvd[3], 1)
1074

    
1075

    
1076
class TestInMultiDegreeView(TestDegreeView):
1077
    GRAPH = nx.MultiDiGraph
1078
    dview = nx.reportviews.InMultiDegreeView
1079

    
1080
    def test_str(self):
1081
        dv = self.dview(self.G)
1082
        rep = str([(0, 0), (1, 1), (2, 1), (3, 3), (4, 1), (5, 1)])
1083
        assert_equal(str(dv), rep)
1084
        dv = self.G.in_degree()
1085
        assert_equal(str(dv), rep)
1086

    
1087
    def test_repr(self):
1088
        dv = self.G.in_degree()
1089
        rep = "InMultiDegreeView({0: 0, 1: 1, 2: 1, 3: 3, 4: 1, 5: 1})"
1090
        assert_equal(repr(dv), rep)
1091

    
1092
    def test_nbunch(self):
1093
        dv = self.dview(self.G)
1094
        dvn = dv(0)
1095
        assert_equal(dvn, 0)
1096
        dvn = dv([2, 3])
1097
        assert_equal(sorted(dvn), [(2, 1), (3, 3)])
1098

    
1099
    def test_getitem(self):
1100
        dv = self.dview(self.G)
1101
        assert_equal(dv[0], 0)
1102
        assert_equal(dv[1], 1)
1103
        assert_equal(dv[2], 1)
1104
        assert_equal(dv[3], 3)
1105
        dv = self.dview(self.G, weight='foo')
1106
        assert_equal(dv[0], 0)
1107
        assert_equal(dv[1], 1)
1108
        assert_equal(dv[2], 1)
1109
        assert_equal(dv[3], 6)
1110

    
1111
    def test_weight(self):
1112
        dv = self.dview(self.G)
1113
        dvw = dv(0, weight='foo')
1114
        assert_equal(dvw, 0)
1115
        dvw = dv(1, weight='foo')
1116
        assert_equal(dvw, 1)
1117
        dvw = dv([2, 3], weight='foo')
1118
        assert_equal(sorted(dvw), [(2, 1), (3, 6)])
1119
        dvd = dict(dv(weight='foo'))
1120
        assert_equal(dvd[0], 0)
1121
        assert_equal(dvd[1], 1)
1122
        assert_equal(dvd[2], 1)
1123
        assert_equal(dvd[3], 6)