summaryrefslogtreecommitdiff
path: root/networkx
diff options
context:
space:
mode:
authorAric Hagberg <aric.hagberg@gmail.com>2012-07-08 10:09:17 -0600
committerAric Hagberg <aric.hagberg@gmail.com>2012-07-08 10:09:17 -0600
commitb094906673e9fa5c941aaea5f4e8e005253c9d8b (patch)
tree3f893a932c79a77e8842af1c357496a5060bd360 /networkx
parent8bf2b8aac8b8da923cf994575624f24cc3b63ec1 (diff)
downloadnetworkx-b094906673e9fa5c941aaea5f4e8e005253c9d8b.tar.gz
Fix/adjust/remove/modify tests that depend on dictionary ordering.
Addresses #741 There may still be more that need fixing since 1) NumPy and some other optional packages do not yet work with Python3.3 so those tests weren't checked 2) Finding these errors by running the tests is nondeterministic itself (e.g. passes 4 times and fails on the 5th). And inspecting all of the tests by hand is error-prone too.
Diffstat (limited to 'networkx')
-rw-r--r--networkx/__init__.py8
-rw-r--r--networkx/algorithms/bipartite/__init__.py15
-rw-r--r--networkx/algorithms/bipartite/projection.py4
-rw-r--r--networkx/algorithms/bipartite/tests/test_project.py7
-rw-r--r--networkx/algorithms/flow/maxflow.py6
-rw-r--r--networkx/algorithms/flow/mincost.py6
-rw-r--r--networkx/algorithms/isomorphism/tests/test_isomorphvf2.py26
-rw-r--r--networkx/algorithms/operators/product.py24
-rw-r--r--networkx/algorithms/operators/tests/test_all.py13
-rw-r--r--networkx/algorithms/operators/tests/test_binary.py12
-rw-r--r--networkx/algorithms/shortest_paths/tests/test_astar.py7
-rw-r--r--networkx/algorithms/shortest_paths/tests/test_dense.py9
-rw-r--r--networkx/algorithms/shortest_paths/tests/test_weighted.py10
-rw-r--r--networkx/classes/digraph.py3
-rw-r--r--networkx/classes/graph.py3
-rw-r--r--networkx/classes/tests/historical_tests.py39
-rw-r--r--networkx/readwrite/multiline_adjlist.py3
-rw-r--r--networkx/readwrite/tests/test_adjlist.py4
-rw-r--r--networkx/readwrite/tests/test_edgelist.py7
-rw-r--r--networkx/readwrite/tests/test_pajek.py12
-rw-r--r--networkx/relabel.py7
-rw-r--r--networkx/testing/utils.py2
-rw-r--r--networkx/tests/test_relabel.py5
23 files changed, 121 insertions, 111 deletions
diff --git a/networkx/__init__.py b/networkx/__init__.py
index 2ea6f5be..70907942 100644
--- a/networkx/__init__.py
+++ b/networkx/__init__.py
@@ -15,10 +15,10 @@ Using
>>> import networkx as nx
>>> G=nx.Graph()
>>> G.add_edge(1,2)
- >>> G.add_node("spam")
- >>> print(G.nodes())
- [1, 2, 'spam']
- >>> print(G.edges())
+ >>> G.add_node(42)
+ >>> print(sorted(G.nodes()))
+ [1, 2, 42]
+ >>> print(sorted(G.edges()))
[(1, 2)]
"""
# Copyright (C) 2004-2010 by
diff --git a/networkx/algorithms/bipartite/__init__.py b/networkx/algorithms/bipartite/__init__.py
index 64018ef7..28bbc525 100644
--- a/networkx/algorithms/bipartite/__init__.py
+++ b/networkx/algorithms/bipartite/__init__.py
@@ -31,9 +31,10 @@ algorithm:
>>> nx.is_connected(B)
True
>>> bottom_nodes, top_nodes = bipartite.sets(B)
->>> list(top_nodes)
+
+list(top_nodes)
[1, 2, 3, 4]
->>> list(bottom_nodes)
+list(bottom_nodes)
['a', 'c', 'b']
However, if the input graph is not connected, there are more than one possible
@@ -43,18 +44,20 @@ colorations. Thus, the following result is correct:
>>> nx.is_connected(B)
False
>>> bottom_nodes, top_nodes = bipartite.sets(B)
->>> list(top_nodes)
+
+list(top_nodes)
[1, 2, 4, 'c']
->>> list(bottom_nodes)
+list(bottom_nodes)
['a', 3, 'b']
Using the "bipartite" node attribute, you can easily get the two node sets:
>>> top_nodes = set(n for n,d in B.nodes(data=True) if d['bipartite']==0)
>>> bottom_nodes = set(B) - top_nodes
->>> list(top_nodes)
+
+list(top_nodes)
[1, 2, 3, 4]
->>> list(bottom_nodes)
+list(bottom_nodes)
['a', 'c', 'b']
So you can easily use the bipartite algorithms that require, as an argument, a
diff --git a/networkx/algorithms/bipartite/projection.py b/networkx/algorithms/bipartite/projection.py
index cf47c419..e0813a31 100644
--- a/networkx/algorithms/bipartite/projection.py
+++ b/networkx/algorithms/bipartite/projection.py
@@ -59,8 +59,8 @@ def projected_graph(B, nodes, multigraph=False):
>>> B = nx.Graph()
>>> B.add_edges_from([('a', 1), ('b', 1), ('a', 2), ('b', 2)])
>>> G = bipartite.projected_graph(B, ['a', 'b'], multigraph=True)
- >>> print(G.edges(keys=True))
- [('a', 'b', 1), ('a', 'b', 2)]
+ >>> print([sorted((u,v)) for u,v in G.edges()])
+ [['a', 'b'], ['a', 'b']]
Notes
------
diff --git a/networkx/algorithms/bipartite/tests/test_project.py b/networkx/algorithms/bipartite/tests/test_project.py
index 1a1ba613..bbefc7fc 100644
--- a/networkx/algorithms/bipartite/tests/test_project.py
+++ b/networkx/algorithms/bipartite/tests/test_project.py
@@ -2,6 +2,7 @@
from nose.tools import *
import networkx as nx
from networkx.algorithms import bipartite
+from networkx.testing import *
class TestBipartiteProject:
@@ -94,11 +95,11 @@ class TestBipartiteProject:
G.add_edge('a',2)
G.add_edge('b',2)
P=bipartite.projected_graph(G,'ab')
- assert_equal(sorted(P.edges()),[('a','b')])
+ assert_edges_equal(P.edges(),[('a','b')])
P=bipartite.weighted_projected_graph(G,'ab')
- assert_equal(sorted(P.edges()),[('a','b')])
+ assert_edges_equal(P.edges(),[('a','b')])
P=bipartite.projected_graph(G,'ab',multigraph=True)
- assert_equal(sorted(P.edges()),[('a','b'),('a','b')])
+ assert_edges_equal(P.edges(),[('a','b'),('a','b')])
def test_project_collaboration(self):
G=nx.Graph()
diff --git a/networkx/algorithms/flow/maxflow.py b/networkx/algorithms/flow/maxflow.py
index 596467fa..3e1ca915 100644
--- a/networkx/algorithms/flow/maxflow.py
+++ b/networkx/algorithms/flow/maxflow.py
@@ -258,15 +258,15 @@ def ford_fulkerson_flow(G, s, t, capacity='capacity'):
>>> G.add_edge('c','y', capacity=2.0)
>>> G.add_edge('e','y', capacity=3.0)
>>> F = nx.ford_fulkerson_flow(G, 'x', 'y')
- >>> for u, v in G.edges_iter():
+ >>> for u, v in sorted(G.edges_iter()):
... print('(%s, %s) %.2f' % (u, v, F[u][v]))
...
(a, c) 2.00
- (c, y) 2.00
(b, c) 0.00
(b, d) 1.00
- (e, y) 1.00
+ (c, y) 2.00
(d, e) 1.00
+ (e, y) 1.00
(x, a) 2.00
(x, b) 1.00
"""
diff --git a/networkx/algorithms/flow/mincost.py b/networkx/algorithms/flow/mincost.py
index 94b94a58..98de1c52 100644
--- a/networkx/algorithms/flow/mincost.py
+++ b/networkx/algorithms/flow/mincost.py
@@ -317,8 +317,8 @@ def network_simplex(G, demand = 'demand', capacity = 'capacity',
>>> flowCost, flowDict = nx.network_simplex(G)
>>> flowCost == nx.shortest_path_length(G, 's', 'v', weight = 'weight')
True
- >>> [(u, v) for u in flowDict for v in flowDict[u] if flowDict[u][v] > 0]
- [('x', 'u'), ('s', 'x'), ('u', 'v')]
+ >>> sorted([(u, v) for u in flowDict for v in flowDict[u] if flowDict[u][v] > 0])
+ [('s', 'x'), ('u', 'v'), ('x', 'u')]
>>> nx.shortest_path(G, 's', 'v', weight = 'weight')
['s', 'x', 'u', 'v']
@@ -651,8 +651,6 @@ def min_cost_flow(G, demand = 'demand', capacity = 'capacity',
>>> G.add_edge('b', 'd', weight = 1, capacity = 9)
>>> G.add_edge('c', 'd', weight = 2, capacity = 5)
>>> flowDict = nx.min_cost_flow(G)
- >>> flowDict
- {'a': {'c': 1, 'b': 4}, 'c': {'d': 1}, 'b': {'d': 4}, 'd': {}}
"""
return network_simplex(G, demand = demand, capacity = capacity,
weight = weight)[1]
diff --git a/networkx/algorithms/isomorphism/tests/test_isomorphvf2.py b/networkx/algorithms/isomorphism/tests/test_isomorphvf2.py
index 75f85d0e..562d3964 100644
--- a/networkx/algorithms/isomorphism/tests/test_isomorphvf2.py
+++ b/networkx/algorithms/isomorphism/tests/test_isomorphvf2.py
@@ -6,7 +6,7 @@ import os
import struct
import random
-from nose.tools import assert_true
+from nose.tools import assert_true, assert_equal
import networkx as nx
from networkx.algorithms import isomorphism as iso
@@ -35,9 +35,11 @@ class TestWikipediaExample(object):
assert_true(gm.is_isomorphic())
mapping = sorted(gm.mapping.items())
- isomap = [('a', 1), ('b', 6), ('c', 3), ('d', 8),
- ('g', 2), ('h', 5), ('i', 4), ('j', 7)]
- assert_true(mapping==isomap)
+# this mapping is only one of the possibilies
+# so this test needs to be reconsidered
+# isomap = [('a', 1), ('b', 6), ('c', 3), ('d', 8),
+# ('g', 2), ('h', 5), ('i', 4), ('j', 7)]
+# assert_equal(mapping, isomap)
def test_subgraph(self):
g1 = nx.Graph()
@@ -174,8 +176,8 @@ def test_isomorphism_iter1():
assert_true({'B':'Y','C':'Z'} in x)
assert_true({'A':'Z','B':'Y'} in y)
assert_true({'B':'Z','C':'Y'} in y)
- assert_true(len(x)==len(y))
- assert_true(len(x)==2)
+ assert_equal(len(x),len(y))
+ assert_equal(len(x),2)
def test_isomorphism_iter2():
# Path
@@ -183,13 +185,13 @@ def test_isomorphism_iter2():
g1 = nx.path_graph(L)
gm = iso.GraphMatcher(g1,g1)
s = len(list(gm.isomorphisms_iter()))
- assert_true(s == 2, s)
+ assert_equal(s,2)
# Cycle
for L in range(3,10):
g1 = nx.cycle_graph(L)
gm = iso.GraphMatcher(g1,g1)
s = len(list(gm.isomorphisms_iter()))
- assert_true(s == 2*L)
+ assert_equal(s, 2*L)
def test_multiple():
# Verify that we can use the graph matcher multiple times
@@ -208,8 +210,8 @@ def test_multiple():
g2.remove_node('C')
assert_true(gmA.subgraph_is_isomorphic())
assert_true(gmB.subgraph_is_isomorphic())
- for m in [gmB.mapping, gmB.mapping]:
- assert_true(m['A'] == 'A')
- assert_true(m['B'] == 'B')
- assert_true('C' not in m)
+# for m in [gmB.mapping, gmB.mapping]:
+# assert_true(m['A'] == 'A')
+# assert_true(m['B'] == 'B')
+# assert_true('C' not in m)
diff --git a/networkx/algorithms/operators/product.py b/networkx/algorithms/operators/product.py
index ae4a6157..0fa8a17b 100644
--- a/networkx/algorithms/operators/product.py
+++ b/networkx/algorithms/operators/product.py
@@ -157,8 +157,8 @@ def tensor_product(G,H):
>>> G.add_node(0,a1=True)
>>> H.add_node('a',a2='Spam')
>>> P = nx.tensor_product(G,H)
- >>> P.nodes(data=True)
- [((0, 'a'), {'a1': (True, None), 'a2': (None, 'Spam')})]
+ >>> P.nodes()
+ [(0, 'a')]
Edge attributes and edge keys (for multigraphs) are also copied to the
new product graph
@@ -207,9 +207,9 @@ def cartesian_product(G,H):
>>> H = nx.Graph()
>>> G.add_node(0,a1=True)
>>> H.add_node('a',a2='Spam')
- >>> P = nx.tensor_product(G,H)
- >>> P.nodes(data=True)
- [((0, 'a'), {'a1': (True, None), 'a2': (None, 'Spam')})]
+ >>> P = nx.cartesian_product(G,H)
+ >>> P.nodes()
+ [(0, 'a')]
Edge attributes and edge keys (for multigraphs) are also copied to the
new product graph
@@ -259,9 +259,9 @@ def lexicographic_product(G,H):
>>> H = nx.Graph()
>>> G.add_node(0,a1=True)
>>> H.add_node('a',a2='Spam')
- >>> P = nx.tensor_product(G,H)
- >>> P.nodes(data=True)
- [((0, 'a'), {'a1': (True, None), 'a2': (None, 'Spam')})]
+ >>> P = nx.lexicographic_product(G,H)
+ >>> P.nodes()
+ [(0, 'a')]
Edge attributes and edge keys (for multigraphs) are also copied to the
new product graph
@@ -272,7 +272,7 @@ def lexicographic_product(G,H):
GH.add_edges_from(_edges_cross_nodes_and_nodes(G,H))
# For each x in G, only if there is an edge in H
GH.add_edges_from(_nodes_cross_edges(G,H))
- GH.name = "Lexographic product("+G.name+","+H.name+")"
+ GH.name = "Lexicographic product("+G.name+","+H.name+")"
return GH
def strong_product(G,H):
@@ -312,9 +312,9 @@ def strong_product(G,H):
>>> H = nx.Graph()
>>> G.add_node(0,a1=True)
>>> H.add_node('a',a2='Spam')
- >>> P = nx.tensor_product(G,H)
- >>> P.nodes(data=True)
- [((0, 'a'), {'a1': (True, None), 'a2': (None, 'Spam')})]
+ >>> P = nx.strong_product(G,H)
+ >>> P.nodes()
+ [(0, 'a')]
Edge attributes and edge keys (for multigraphs) are also copied to the
new product graph
diff --git a/networkx/algorithms/operators/tests/test_all.py b/networkx/algorithms/operators/tests/test_all.py
index 028bea99..2e6454d9 100644
--- a/networkx/algorithms/operators/tests/test_all.py
+++ b/networkx/algorithms/operators/tests/test_all.py
@@ -1,5 +1,6 @@
from nose.tools import *
import networkx as nx
+from networkx.testing import *
def test_union_all_attributes():
g = nx.Graph()
@@ -88,14 +89,14 @@ def test_union_all_and_compose_all():
G1.add_edge('A','C')
G1.add_edge('A','D')
G2=nx.DiGraph()
- G2.add_edge(1,2)
- G2.add_edge(1,3)
- G2.add_edge(1,4)
+ G2.add_edge('1','2')
+ G2.add_edge('1','3')
+ G2.add_edge('1','4')
G=nx.union_all([G1,G2])
H=nx.compose_all([G1,G2])
- assert_true(G.edges()==H.edges())
- assert_false(G.has_edge('A',1))
+ assert_edges_equal(G.edges(),H.edges())
+ assert_false(G.has_edge('A','1'))
assert_raises(nx.NetworkXError, nx.union, K3, P3)
H1=nx.union_all([H,G1],rename=('H','G1'))
assert_equal(sorted(H1.nodes()),
@@ -110,7 +111,7 @@ def test_union_all_and_compose_all():
assert_false(H1.has_edge('NB','NA'))
G=nx.compose_all([G,G])
- assert_equal(G.edges(),H.edges())
+ assert_edges_equal(G.edges(),H.edges())
G2=nx.union_all([G2,G2],rename=('','copy'))
assert_equal(sorted(G2.nodes()),
diff --git a/networkx/algorithms/operators/tests/test_binary.py b/networkx/algorithms/operators/tests/test_binary.py
index c8bf9118..34b7e6e8 100644
--- a/networkx/algorithms/operators/tests/test_binary.py
+++ b/networkx/algorithms/operators/tests/test_binary.py
@@ -1,7 +1,7 @@
from nose.tools import *
import networkx as nx
from networkx import *
-
+from networkx.testing import *
def test_union_attributes():
g = nx.Graph()
@@ -186,13 +186,13 @@ def test_union_and_compose():
G1.add_edge('A','C')
G1.add_edge('A','D')
G2=nx.DiGraph()
- G2.add_edge(1,2)
- G2.add_edge(1,3)
- G2.add_edge(1,4)
+ G2.add_edge('1','2')
+ G2.add_edge('1','3')
+ G2.add_edge('1','4')
G=union(G1,G2)
H=compose(G1,G2)
- assert_true(G.edges()==H.edges())
+ assert_edges_equal(G.edges(),H.edges())
assert_false(G.has_edge('A',1))
assert_raises(nx.NetworkXError, nx.union, K3, P3)
H1=union(H,G1,rename=('H','G1'))
@@ -208,7 +208,7 @@ def test_union_and_compose():
assert_false(H1.has_edge('NB','NA'))
G=compose(G,G)
- assert_equal(G.edges(),H.edges())
+ assert_edges_equal(G.edges(),H.edges())
G2=union(G2,G2,rename=('','copy'))
assert_equal(sorted(G2.nodes()),
diff --git a/networkx/algorithms/shortest_paths/tests/test_astar.py b/networkx/algorithms/shortest_paths/tests/test_astar.py
index fb29df08..81ba6abc 100644
--- a/networkx/algorithms/shortest_paths/tests/test_astar.py
+++ b/networkx/algorithms/shortest_paths/tests/test_astar.py
@@ -54,9 +54,12 @@ class TestAStar:
def test_astar_undirected(self):
GG=self.XG.to_undirected()
+ # make sure we get lower weight
+ # to_undirected might choose either edge with weight 2 or weight 3
+ GG['u']['x']['weight']=2
GG['y']['v']['weight'] = 2
- assert nx.astar_path(GG,'s','v')==['s', 'x', 'u', 'v']
- assert nx.astar_path_length(GG,'s','v')==8
+ assert_equal(nx.astar_path(GG,'s','v'),['s', 'x', 'u', 'v'])
+ assert_equal(nx.astar_path_length(GG,'s','v'),8)
def test_astar_directed2(self):
XG2=nx.DiGraph()
diff --git a/networkx/algorithms/shortest_paths/tests/test_dense.py b/networkx/algorithms/shortest_paths/tests/test_dense.py
index bf9ca0e2..48deee50 100644
--- a/networkx/algorithms/shortest_paths/tests/test_dense.py
+++ b/networkx/algorithms/shortest_paths/tests/test_dense.py
@@ -26,9 +26,13 @@ class TestFloyd:
GG=XG.to_undirected()
+ # make sure we get lower weight
+ # to_undirected might choose either edge with weight 2 or weight 3
+ GG['u']['x']['weight']=2
path, dist = nx.floyd_warshall_predecessor_and_distance(GG)
assert_equal(dist['s']['v'],8)
- assert_equal(path['s']['v'],'y')
+ # skip this test, could be alternate path s-u-v
+# assert_equal(path['s']['v'],'y')
G=nx.DiGraph() # no weights
G.add_edges_from([('s','u'), ('s','x'),
@@ -38,7 +42,8 @@ class TestFloyd:
('y','s'), ('y','v')])
path, dist = nx.floyd_warshall_predecessor_and_distance(G)
assert_equal(dist['s']['v'],2)
- assert_equal(path['s']['v'],'x')
+ # skip this test, could be alternate path s-u-v
+ # assert_equal(path['s']['v'],'x')
# alternate interface
dist = nx.floyd_warshall(G)
diff --git a/networkx/algorithms/shortest_paths/tests/test_weighted.py b/networkx/algorithms/shortest_paths/tests/test_weighted.py
index ccda4a3e..c3998d43 100644
--- a/networkx/algorithms/shortest_paths/tests/test_weighted.py
+++ b/networkx/algorithms/shortest_paths/tests/test_weighted.py
@@ -57,6 +57,9 @@ class TestWeightedPath:
['s', 'x', 'u', 'v'])
GG=self.XG.to_undirected()
+ # make sure we get lower weight
+ # to_undirected might choose either edge with weight 2 or weight 3
+ GG['u']['x']['weight']=2
(D,P)= nx.single_source_dijkstra(GG,'s')
assert_equal(P['v'] , ['s', 'x', 'u', 'v'])
assert_equal(D['v'],8) # uses lower weight of 2 on u<->x edge
@@ -89,8 +92,11 @@ class TestWeightedPath:
def test_bidirectional_dijkstra(self):
assert_equal(nx.bidirectional_dijkstra(self.XG, 's', 'v'),
(9, ['s', 'x', 'u', 'v']))
- assert_equal(nx.bidirectional_dijkstra(self.G,'s','v'),
- (2, ['s', 'x', 'v']))
+ (dist,path) = nx.bidirectional_dijkstra(self.G,'s','v')
+ assert_equal(dist,2)
+ # skip this test, correct path could also be ['s','u','v']
+# assert_equal(nx.bidirectional_dijkstra(self.G,'s','v'),
+# (2, ['s', 'x', 'v']))
assert_equal(nx.bidirectional_dijkstra(self.cycle,0,3),
(3, [0, 1, 2, 3]))
assert_equal(nx.bidirectional_dijkstra(self.cycle,0,4),
diff --git a/networkx/classes/digraph.py b/networkx/classes/digraph.py
index e972ba82..37b9f9de 100644
--- a/networkx/classes/digraph.py
+++ b/networkx/classes/digraph.py
@@ -140,9 +140,6 @@ class DiGraph(Graph):
[1, 2]
>>> len(G) # number of nodes in graph
5
- >>> G[1] # adjacency dict keyed by neighbor to edge attributes
- ... # Note: you should not change this dict manually!
- {2: {'color': 'blue', 'weight': 4}}
The fastest way to traverse all edges of a graph is via
adjacency_iter(), but the edges() method is often more convenient.
diff --git a/networkx/classes/graph.py b/networkx/classes/graph.py
index 056c4287..9ef7c234 100644
--- a/networkx/classes/graph.py
+++ b/networkx/classes/graph.py
@@ -148,9 +148,6 @@ class Graph(object):
[1, 2]
>>> len(G) # number of nodes in graph
5
- >>> G[1] # adjacency dict keyed by neighbor to edge attributes
- ... # Note: you should not change this dict manually!
- {2: {'color': 'blue', 'weight': 4}}
The fastest way to traverse all edges of a graph is via
adjacency_iter(), but the edges() method is often more convenient.
diff --git a/networkx/classes/tests/historical_tests.py b/networkx/classes/tests/historical_tests.py
index 8fc11f45..5dd398cc 100644
--- a/networkx/classes/tests/historical_tests.py
+++ b/networkx/classes/tests/historical_tests.py
@@ -4,6 +4,7 @@ from nose.tools import *
import networkx
import networkx as nx
from networkx import convert_node_labels_to_integers as cnlti
+from networkx.testing import *
class HistoricalTests(object):
@@ -239,22 +240,20 @@ class HistoricalTests(object):
else:
elist=[('A', 'B'), ('A', 'C'), ('B', 'C'), ('B', 'D')]
# nbunch can be a list
- assert_equals(sorted(G.edges(['A','B'])),elist)
+ assert_edges_equal(G.edges(['A','B']),elist)
# nbunch can be a set
- assert_equals(sorted(G.edges(set(['A','B']))),elist)
+ assert_edges_equal(G.edges(set(['A','B'])),elist)
# nbunch can be a graph
G1=self.G()
G1.add_nodes_from('AB')
- assert_equals(sorted(G.edges(G1)),elist)
+ assert_edges_equal(G.edges(G1),elist)
# nbunch can be a dict with nodes as keys
ndict={'A': "thing1", 'B': "thing2"}
- assert_equals(sorted(G.edges(ndict)),elist)
+ assert_edges_equal(G.edges(ndict),elist)
# nbunch can be a single node
- assert_equals(sorted(G.edges('A')),
- [('A', 'B'), ('A', 'C')])
+ assert_edges_equal(G.edges('A'), [('A', 'B'), ('A', 'C')])
- assert_equals(sorted(G.nodes_iter()),
- ['A', 'B', 'C', 'D'])
+ assert_edges_equal(G.nodes_iter(), ['A', 'B', 'C', 'D'])
def test_edges_iter_nbunch(self):
G=self.G()
@@ -270,24 +269,22 @@ class HistoricalTests(object):
else:
elist=[('A', 'B'), ('A', 'C'), ('B', 'C'), ('B', 'D')]
# nbunch can be a list
- assert_equals(sorted(G.edges_iter(['A','B'])),elist)
+ assert_edges_equal(G.edges_iter(['A','B']),elist)
# nbunch can be a set
- assert_equals(sorted(G.edges_iter(set(['A','B']))),elist)
+ assert_edges_equal(G.edges_iter(set(['A','B'])),elist)
# nbunch can be a graph
G1=self.G()
G1.add_nodes_from(['A','B'])
- assert_equals(sorted(G.edges_iter(G1)),elist)
+ assert_edges_equal(G.edges_iter(G1),elist)
# nbunch can be a dict with nodes as keys
ndict={'A': "thing1", 'B': "thing2"}
- assert_equals(sorted(G.edges_iter(ndict)),elist)
+ assert_edges_equal(G.edges_iter(ndict),elist)
# nbunch can be a single node
- assert_equals(sorted(G.edges_iter('A')),
- [('A', 'B'), ('A', 'C')])
+ assert_edges_equal(G.edges_iter('A'), [('A', 'B'), ('A', 'C')])
# nbunch can be nothing (whole graph)
- assert_equals(sorted(G.edges_iter()),
- [('A', 'B'), ('A', 'C'), ('B', 'D'),
- ('C', 'B'), ('C', 'D')])
+ assert_edges_equal(G.edges_iter(), [('A', 'B'), ('A', 'C'), ('B', 'D'),
+ ('C', 'B'), ('C', 'D')])
def test_degree(self):
@@ -299,7 +296,7 @@ class HistoricalTests(object):
# degree of single node in iterable container must return dict
assert_equal(list(G.degree(['A']).values()),[2])
assert_equal(G.degree(['A']),{'A': 2})
- assert_equal(list(G.degree(['A','B']).values()),[2, 3])
+ assert_equal(sorted(G.degree(['A','B']).values()),[2, 3])
assert_equal(G.degree(['A','B']),{'A': 2, 'B': 3})
assert_equal(sorted(G.degree().values()),[2, 2, 3, 3])
assert_equal(sorted([v for k,v in G.degree_iter()]),
@@ -351,8 +348,8 @@ class HistoricalTests(object):
G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'D'),
('C', 'B'), ('C', 'D')])
SG=G.subgraph(['A','B','D'])
- assert_equal(sorted(SG.nodes()),['A', 'B', 'D'])
- assert_equal(sorted(SG.edges()),[('A', 'B'), ('B', 'D')])
+ assert_nodes_equal(SG.nodes(),['A', 'B', 'D'])
+ assert_edges_equal(SG.edges(),[('A', 'B'), ('B', 'D')])
def test_to_directed(self):
G=self.G()
@@ -412,7 +409,7 @@ class HistoricalTests(object):
G.add_nodes_from('GJK')
assert_equal(sorted(G.nodes_iter()),
['A', 'B', 'C', 'D', 'G', 'J', 'K'])
- assert_equal(sorted(G.edges_iter()),
+ assert_edges_equal(G.edges_iter(),
[('A', 'B'), ('A', 'C'), ('B', 'D'), ('C', 'B'), ('C', 'D')])
assert_equal(sorted([v for k,v in G.degree_iter()]),
diff --git a/networkx/readwrite/multiline_adjlist.py b/networkx/readwrite/multiline_adjlist.py
index 21f33bd7..30c3234d 100644
--- a/networkx/readwrite/multiline_adjlist.py
+++ b/networkx/readwrite/multiline_adjlist.py
@@ -226,9 +226,6 @@ def parse_multiline_adjlist(lines, comments = '#', delimiter = None,
>>> G = nx.parse_multiline_adjlist(iter(lines), nodetype = int)
>>> G.nodes()
[1, 2, 3, 5]
- >>> G.edges(data = True)
- [(1, 2, {'name': 'Frodo', 'weight': 3}), (1, 3, {}), (2, 5, {'name': 'Saruman', 'weight': 6})]
-
"""
from ast import literal_eval
if create_using is None:
diff --git a/networkx/readwrite/tests/test_adjlist.py b/networkx/readwrite/tests/test_adjlist.py
index b077d4fc..adc29183 100644
--- a/networkx/readwrite/tests/test_adjlist.py
+++ b/networkx/readwrite/tests/test_adjlist.py
@@ -2,11 +2,11 @@
"""
Unit tests for adjlist.
"""
-
from nose.tools import assert_equal, assert_raises, assert_not_equal
import os
import tempfile
import networkx as nx
+from networkx.testing import *
class TestAdjlist():
@@ -226,7 +226,7 @@ class TestMultilineAdjlist():
H2=nx.read_multiline_adjlist(fname,create_using=nx.DiGraph())
assert_not_equal(H,H2) # they should be different graphs
assert_equal(sorted(H.nodes()),sorted(G.nodes()))
- assert_equal(sorted(H.edges()),sorted(G.edges()))
+ assert_edges_equal(H.edges(),G.edges())
os.close(fd)
os.unlink(fname)
diff --git a/networkx/readwrite/tests/test_edgelist.py b/networkx/readwrite/tests/test_edgelist.py
index 9f4fffec..74416bf3 100644
--- a/networkx/readwrite/tests/test_edgelist.py
+++ b/networkx/readwrite/tests/test_edgelist.py
@@ -1,7 +1,6 @@
"""
Unit tests for edgelists.
"""
-
from nose.tools import assert_equal, assert_raises, assert_not_equal
import networkx as nx
import io
@@ -182,12 +181,12 @@ class TestEdgelist:
def test_edgelist_integers(self):
- G=nx.convert_node_labels_to_integers(self.G)
+ G=nx.convert_node_labels_to_integers(self.G,discard_old_labels=False)
(fd,fname)=tempfile.mkstemp()
nx.write_edgelist(G,fname)
H=nx.read_edgelist(fname,nodetype=int)
- H2=nx.read_edgelist(fname,nodetype=int)
- G.remove_node(5) # isolated nodes are not written in edgelist
+ # isolated nodes are not written in edgelist
+ G.remove_nodes_from(nx.isolates(G))
assert_equal(sorted(H.nodes()),sorted(G.nodes()))
assert_equal(sorted(H.edges()),sorted(G.edges()))
os.close(fd)
diff --git a/networkx/readwrite/tests/test_pajek.py b/networkx/readwrite/tests/test_pajek.py
index fd3cd678..a2e43d00 100644
--- a/networkx/readwrite/tests/test_pajek.py
+++ b/networkx/readwrite/tests/test_pajek.py
@@ -2,11 +2,11 @@
"""
Pajek tests
"""
-
from nose.tools import assert_equal
from networkx import *
import os,tempfile
from io import open
+from networkx.testing import *
class TestPajek(object):
def setUp(self):
@@ -32,20 +32,20 @@ class TestPajek(object):
data="""*Vertices 2\n1 "1"\n2 "2"\n*Edges\n1 2\n2 1"""
G=parse_pajek(data)
assert_equal(sorted(G.nodes()), ['1', '2'])
- assert_equal(sorted(G.edges()), [('1', '2'), ('1', '2')])
+ assert_edges_equal(G.edges(), [('1', '2'), ('1', '2')])
def test_parse_pajek(self):
G=parse_pajek(self.data)
assert_equal(sorted(G.nodes()), ['A1', 'Bb', 'C', 'D2'])
- assert_equal(sorted(G.edges()),
- [('A1', 'A1'), ('A1', 'Bb'), ('A1', 'C'), ('Bb', 'A1'),
- ('C', 'C'), ('C', 'D2'), ('D2', 'Bb')])
+ assert_edges_equal(G.edges(), [('A1', 'A1'), ('A1', 'Bb'),
+ ('A1', 'C'), ('Bb', 'A1'),
+ ('C', 'C'), ('C', 'D2'), ('D2', 'Bb')])
def test_read_pajek(self):
G=parse_pajek(self.data)
Gin=read_pajek(self.fname)
assert_equal(sorted(G.nodes()), sorted(Gin.nodes()))
- assert_equal(sorted(G.edges()), sorted(Gin.edges()))
+ assert_edges_equal(G.edges(), Gin.edges())
assert_equal(self.G.graph,Gin.graph)
for n in G.node:
assert_equal(G.node[n],Gin.node[n])
diff --git a/networkx/relabel.py b/networkx/relabel.py
index 10830371..6055c1f1 100644
--- a/networkx/relabel.py
+++ b/networkx/relabel.py
@@ -30,8 +30,8 @@ def relabel_nodes(G, mapping, copy=True):
>>> G=nx.path_graph(3) # nodes 0-1-2
>>> mapping={0:'a',1:'b',2:'c'}
>>> H=nx.relabel_nodes(G,mapping)
- >>> print(H.nodes())
- ['a', 'c', 'b']
+ >>> print(sorted(H.nodes()))
+ ['a', 'b', 'c']
>>> G=nx.path_graph(26) # nodes 0..25
>>> mapping=dict(zip(G.nodes(),"abcdefghijklmnopqrstuvwxyz"))
@@ -44,7 +44,8 @@ def relabel_nodes(G, mapping, copy=True):
>>> G=nx.path_graph(3) # nodes 0-1-2
>>> mapping={0:'a',1:'b'} # 0->'a' and 1->'b'
>>> G=nx.relabel_nodes(G,mapping, copy=False)
- >>> print(G.nodes())
+
+ print(G.nodes())
[2, 'b', 'a']
Mapping as function:
diff --git a/networkx/testing/utils.py b/networkx/testing/utils.py
index d3292329..5cca7f88 100644
--- a/networkx/testing/utils.py
+++ b/networkx/testing/utils.py
@@ -25,6 +25,8 @@ def assert_edges_equal(elist1, elist2):
e1 = sorted(elist1,key=lambda x: sorted(x[0:2]))
e2 = sorted(elist2,key=lambda x: sorted(x[0:2]))
assert_equal(len(e1),len(e2))
+ if len(e1) == 0:
+ return True
if len(e1[0]) == 2:
for a,b in zip(e1,e2):
assert_equal(set(a[0:2]),set(b[0:2]))
diff --git a/networkx/tests/test_relabel.py b/networkx/tests/test_relabel.py
index 9645b5d1..8c05b1b2 100644
--- a/networkx/tests/test_relabel.py
+++ b/networkx/tests/test_relabel.py
@@ -5,6 +5,7 @@ from networkx import *
from networkx.convert import *
from networkx.algorithms.operators import *
from networkx.generators.classic import barbell_graph,cycle_graph
+from networkx.testing import *
class TestRelabel():
def test_convert_node_labels_to_integers(self):
@@ -123,8 +124,8 @@ class TestRelabel():
mapping={'a':'aardvark','b':'bear'}
G=relabel_nodes(G,mapping,copy=False)
assert_equal(sorted(G.nodes()), ['aardvark', 'bear'])
- assert_equal(sorted(G.edges()),
- [('aardvark', 'bear'), ('aardvark', 'bear')])
+ assert_edges_equal(sorted(G.edges()),
+ [('aardvark', 'bear'), ('aardvark', 'bear')])
def test_relabel_nodes_multidigraph(self):
G=MultiDiGraph([('a','b'),('a','b')])