summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordi Torrents <jordi.t21@gmail.com>2015-05-10 17:06:20 +0200
committerJordi Torrents <jordi.t21@gmail.com>2015-05-10 17:06:20 +0200
commit43eb4f930f14fcf693f0656a3f0bbe749ed98d2e (patch)
treeca4586bbebd9de955c9d92cd21afa22fe5e54845
parenta2a3e84be346235ddbada86c8f2e1992be40fa12 (diff)
downloadnetworkx-43eb4f930f14fcf693f0656a3f0bbe749ed98d2e.tar.gz
Move subgraph attribute copies tests to a separate file.
-rw-r--r--networkx/algorithms/components/tests/test_subgraph_copies.py84
1 files changed, 84 insertions, 0 deletions
diff --git a/networkx/algorithms/components/tests/test_subgraph_copies.py b/networkx/algorithms/components/tests/test_subgraph_copies.py
new file mode 100644
index 00000000..b09e245b
--- /dev/null
+++ b/networkx/algorithms/components/tests/test_subgraph_copies.py
@@ -0,0 +1,84 @@
+""" Tests for subgraphs attributes
+"""
+from copy import deepcopy
+from nose.tools import assert_equal
+import networkx as nx
+
+class TestSubgraphAttributesDicts:
+
+ def setUp(self):
+ self.undirected = [
+ nx.connected_component_subgraphs,
+ nx.biconnected_component_subgraphs,
+ ]
+ self.directed = [
+ nx.weakly_connected_component_subgraphs,
+ nx.strongly_connected_component_subgraphs,
+ nx.attracting_component_subgraphs,
+ ]
+ self.subgraph_funcs = self.undirected + self.directed
+
+ self.D = nx.DiGraph()
+ self.D.add_edge(1, 2, eattr='red')
+ self.D.add_edge(2, 1, eattr='red')
+ self.D.node[1]['nattr'] = 'blue'
+ self.D.graph['gattr'] = 'green'
+
+ self.G = nx.Graph()
+ self.G.add_edge(1, 2, eattr='red')
+ self.G.node[1]['nattr'] = 'blue'
+ self.G.graph['gattr'] = 'green'
+
+ def test_subgraphs_default_copy_behavior(self):
+ # Test the default behavior of subgraph functions
+ # For the moment (1.10) the default is to copy
+ for subgraph_func in self.subgraph_funcs:
+ G = deepcopy(self.G if subgraph_func in self.undirected else self.D)
+ SG = list(subgraph_func(G))[0]
+ assert_equal(SG[1][2]['eattr'], 'red')
+ assert_equal(SG.node[1]['nattr'], 'blue')
+ assert_equal(SG.graph['gattr'], 'green')
+ SG[1][2]['eattr'] = 'foo'
+ assert_equal(G[1][2]['eattr'], 'red')
+ assert_equal(SG[1][2]['eattr'], 'foo')
+ SG.node[1]['nattr'] = 'bar'
+ assert_equal(G.node[1]['nattr'], 'blue')
+ assert_equal(SG.node[1]['nattr'], 'bar')
+ SG.graph['gattr'] = 'baz'
+ assert_equal(G.graph['gattr'], 'green')
+ assert_equal(SG.graph['gattr'], 'baz')
+
+ def test_subgraphs_copy(self):
+ for subgraph_func in self.subgraph_funcs:
+ test_graph = self.G if subgraph_func in self.undirected else self.D
+ G = deepcopy(test_graph)
+ SG = list(subgraph_func(G, copy=True))[0]
+ assert_equal(SG[1][2]['eattr'], 'red')
+ assert_equal(SG.node[1]['nattr'], 'blue')
+ assert_equal(SG.graph['gattr'], 'green')
+ SG[1][2]['eattr'] = 'foo'
+ assert_equal(G[1][2]['eattr'], 'red')
+ assert_equal(SG[1][2]['eattr'], 'foo')
+ SG.node[1]['nattr'] = 'bar'
+ assert_equal(G.node[1]['nattr'], 'blue')
+ assert_equal(SG.node[1]['nattr'], 'bar')
+ SG.graph['gattr'] = 'baz'
+ assert_equal(G.graph['gattr'], 'green')
+ assert_equal(SG.graph['gattr'], 'baz')
+
+ def test_subgraphs_no_copy(self):
+ for subgraph_func in self.subgraph_funcs:
+ G = deepcopy(self.G if subgraph_func in self.undirected else self.D)
+ SG = list(subgraph_func(G, copy=False))[0]
+ assert_equal(SG[1][2]['eattr'], 'red')
+ assert_equal(SG.node[1]['nattr'], 'blue')
+ assert_equal(SG.graph['gattr'], 'green')
+ SG[1][2]['eattr'] = 'foo'
+ assert_equal(G[1][2]['eattr'], 'foo')
+ assert_equal(SG[1][2]['eattr'], 'foo')
+ SG.node[1]['nattr'] = 'bar'
+ assert_equal(G.node[1]['nattr'], 'bar')
+ assert_equal(SG.node[1]['nattr'], 'bar')
+ SG.graph['gattr'] = 'baz'
+ assert_equal(G.graph['gattr'], 'baz')
+ assert_equal(SG.graph['gattr'], 'baz')