Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / algorithms / components / tests / test_subgraph_copies.py @ 5cef0f13

History | View | Annotate | Download (3.46 KB)

1
""" Tests for subgraphs attributes
2
"""
3
from copy import deepcopy
4
from nose.tools import assert_equal
5
import networkx as nx
6

    
7
# deprecated in 2.1 for removal in 2.2
8

    
9

    
10
class TestSubgraphAttributesDicts:
11

    
12
    def setUp(self):
13
        self.undirected = [
14
            nx.connected_component_subgraphs,
15
            nx.biconnected_component_subgraphs,
16
        ]
17
        self.directed = [
18
            nx.weakly_connected_component_subgraphs,
19
            nx.strongly_connected_component_subgraphs,
20
            nx.attracting_component_subgraphs,
21
        ]
22
        self.subgraph_funcs = self.undirected + self.directed
23

    
24
        self.D = nx.DiGraph()
25
        self.D.add_edge(1, 2, eattr='red')
26
        self.D.add_edge(2, 1, eattr='red')
27
        self.D.nodes[1]['nattr'] = 'blue'
28
        self.D.graph['gattr'] = 'green'
29

    
30
        self.G = nx.Graph()
31
        self.G.add_edge(1, 2, eattr='red')
32
        self.G.nodes[1]['nattr'] = 'blue'
33
        self.G.graph['gattr'] = 'green'
34

    
35
    def test_subgraphs_default_copy_behavior(self):
36
        # Test the default behavior of subgraph functions
37
        # For the moment (1.10) the default is to copy
38
        for subgraph_func in self.subgraph_funcs:
39
            G = deepcopy(self.G if subgraph_func in self.undirected else self.D)
40
            SG = list(subgraph_func(G))[0]
41
            assert_equal(SG[1][2]['eattr'], 'red')
42
            assert_equal(SG.nodes[1]['nattr'], 'blue')
43
            assert_equal(SG.graph['gattr'], 'green')
44
            SG[1][2]['eattr'] = 'foo'
45
            assert_equal(G[1][2]['eattr'], 'red')
46
            assert_equal(SG[1][2]['eattr'], 'foo')
47
            SG.nodes[1]['nattr'] = 'bar'
48
            assert_equal(G.nodes[1]['nattr'], 'blue')
49
            assert_equal(SG.nodes[1]['nattr'], 'bar')
50
            SG.graph['gattr'] = 'baz'
51
            assert_equal(G.graph['gattr'], 'green')
52
            assert_equal(SG.graph['gattr'], 'baz')
53

    
54
    def test_subgraphs_copy(self):
55
        for subgraph_func in self.subgraph_funcs:
56
            test_graph = self.G if subgraph_func in self.undirected else self.D
57
            G = deepcopy(test_graph)
58
            SG = list(subgraph_func(G, copy=True))[0]
59
            assert_equal(SG[1][2]['eattr'], 'red')
60
            assert_equal(SG.nodes[1]['nattr'], 'blue')
61
            assert_equal(SG.graph['gattr'], 'green')
62
            SG[1][2]['eattr'] = 'foo'
63
            assert_equal(G[1][2]['eattr'], 'red')
64
            assert_equal(SG[1][2]['eattr'], 'foo')
65
            SG.nodes[1]['nattr'] = 'bar'
66
            assert_equal(G.nodes[1]['nattr'], 'blue')
67
            assert_equal(SG.nodes[1]['nattr'], 'bar')
68
            SG.graph['gattr'] = 'baz'
69
            assert_equal(G.graph['gattr'], 'green')
70
            assert_equal(SG.graph['gattr'], 'baz')
71

    
72
    def test_subgraphs_no_copy(self):
73
        for subgraph_func in self.subgraph_funcs:
74
            G = deepcopy(self.G if subgraph_func in self.undirected else self.D)
75
            SG = list(subgraph_func(G, copy=False))[0]
76
            assert_equal(SG[1][2]['eattr'], 'red')
77
            assert_equal(SG.nodes[1]['nattr'], 'blue')
78
            assert_equal(SG.graph['gattr'], 'green')
79
            SG[1][2]['eattr'] = 'foo'
80
            assert_equal(G[1][2]['eattr'], 'foo')
81
            assert_equal(SG[1][2]['eattr'], 'foo')
82
            SG.nodes[1]['nattr'] = 'bar'
83
            assert_equal(G.nodes[1]['nattr'], 'bar')
84
            assert_equal(SG.nodes[1]['nattr'], 'bar')
85
            SG.graph['gattr'] = 'baz'
86
            assert_equal(G.graph['gattr'], 'baz')
87
            assert_equal(SG.graph['gattr'], 'baz')