summaryrefslogtreecommitdiff
path: root/networkx/readwrite
diff options
context:
space:
mode:
authorRoss Barnowski <rossbar@berkeley.edu>2021-01-05 13:02:51 -0800
committerGitHub <noreply@github.com>2021-01-05 13:02:51 -0800
commitb563423cd4359e10b3492d2fd6d0413d20d104b6 (patch)
tree7aae92b51ad1faf0ea9392c14f3716106a829788 /networkx/readwrite
parent3e12d7683676481fcaca2f4f1166789d46189c06 (diff)
downloadnetworkx-b563423cd4359e10b3492d2fd6d0413d20d104b6.tar.gz
TST: An approach to parametrizing read_edgelist tests. (#4292)
* TST: An approach to parametrizing read_edgelist tests. Use parametrization to improve code re-use. Pull tests outside class. * TST: Add tests for parse_edgelist. Co-authored-by: Andrew Eckart <andrew.g.eckart@gmail.com> Co-authored-by: Andrew Eckart <andrew.g.eckart@gmail.com>
Diffstat (limited to 'networkx/readwrite')
-rw-r--r--networkx/readwrite/tests/test_edgelist.py247
1 files changed, 127 insertions, 120 deletions
diff --git a/networkx/readwrite/tests/test_edgelist.py b/networkx/readwrite/tests/test_edgelist.py
index 56141c30..f159f2b1 100644
--- a/networkx/readwrite/tests/test_edgelist.py
+++ b/networkx/readwrite/tests/test_edgelist.py
@@ -5,11 +5,138 @@ import pytest
import io
import tempfile
import os
+import textwrap
import networkx as nx
from networkx.testing import assert_edges_equal, assert_nodes_equal, assert_graphs_equal
+edges_no_data = textwrap.dedent(
+ """
+ # comment line
+ 1 2
+ # comment line
+ 2 3
+ """
+)
+
+
+edges_with_values = textwrap.dedent(
+ """
+ # comment line
+ 1 2 2.0
+ # comment line
+ 2 3 3.0
+ """
+)
+
+
+edges_with_weight = textwrap.dedent(
+ """
+ # comment line
+ 1 2 {'weight':2.0}
+ # comment line
+ 2 3 {'weight':3.0}
+ """
+)
+
+
+edges_with_multiple_attrs = textwrap.dedent(
+ """
+ # comment line
+ 1 2 {'weight':2.0, 'color':'green'}
+ # comment line
+ 2 3 {'weight':3.0, 'color':'red'}
+ """
+)
+
+
+edges_with_multiple_attrs_csv = textwrap.dedent(
+ """
+ # comment line
+ 1, 2, {'weight':2.0, 'color':'green'}
+ # comment line
+ 2, 3, {'weight':3.0, 'color':'red'}
+ """
+)
+
+
+_expected_edges_weights = [(1, 2, {"weight": 2.0}), (2, 3, {"weight": 3.0})]
+_expected_edges_multiattr = [
+ (1, 2, {"weight": 2.0, "color": "green"}),
+ (2, 3, {"weight": 3.0, "color": "red"}),
+]
+
+
+@pytest.mark.parametrize(
+ ("data", "extra_kwargs"),
+ (
+ (edges_no_data, {}),
+ (edges_with_values, {}),
+ (edges_with_weight, {}),
+ (edges_with_multiple_attrs, {}),
+ (edges_with_multiple_attrs_csv, {"delimiter": ","}),
+ ),
+)
+def test_read_edgelist_no_data(data, extra_kwargs):
+ bytesIO = io.BytesIO(data.encode("utf-8"))
+ G = nx.read_edgelist(bytesIO, nodetype=int, data=False, **extra_kwargs)
+ assert_edges_equal(G.edges(), [(1, 2), (2, 3)])
+
+
+def test_read_weighted_edgelist():
+ bytesIO = io.BytesIO(edges_with_values.encode("utf-8"))
+ G = nx.read_weighted_edgelist(bytesIO, nodetype=int)
+ assert_edges_equal(G.edges(data=True), _expected_edges_weights)
+
+
+@pytest.mark.parametrize(
+ ("data", "extra_kwargs", "expected"),
+ (
+ (edges_with_weight, {}, _expected_edges_weights),
+ (edges_with_multiple_attrs, {}, _expected_edges_multiattr),
+ (edges_with_multiple_attrs_csv, {"delimiter": ","}, _expected_edges_multiattr),
+ ),
+)
+def test_read_edgelist_with_data(data, extra_kwargs, expected):
+ bytesIO = io.BytesIO(data.encode("utf-8"))
+ G = nx.read_edgelist(bytesIO, nodetype=int, **extra_kwargs)
+ assert_edges_equal(G.edges(data=True), expected)
+
+
+@pytest.fixture
+def example_graph():
+ G = nx.Graph()
+ G.add_weighted_edges_from([(1, 2, 3.0), (2, 3, 27.0), (3, 4, 3.0)])
+ return G
+
+
+def test_parse_edgelist_no_data(example_graph):
+ G = example_graph
+ H = nx.parse_edgelist(["1 2", "2 3", "3 4"], nodetype=int)
+ assert_nodes_equal(G.nodes, H.nodes)
+ assert_edges_equal(G.edges, H.edges)
+
+
+def test_parse_edgelist_with_data_dict(example_graph):
+ G = example_graph
+ H = nx.parse_edgelist(
+ ["1 2 {'weight': 3}", "2 3 {'weight': 27}", "3 4 {'weight': 3.0}"],
+ nodetype=int,
+ )
+ assert_nodes_equal(G.nodes, H.nodes)
+ assert_edges_equal(G.edges(data=True), H.edges(data=True))
+
+
+def test_parse_edgelist_with_data_list(example_graph):
+ G = example_graph
+ H = nx.parse_edgelist(
+ ["1 2 3", "2 3 27", "3 4 3.0"], nodetype=int, data=(("weight", float),)
+ )
+ assert_nodes_equal(G.nodes, H.nodes)
+ assert_edges_equal(G.edges(data=True), H.edges(data=True))
+
+
def test_parse_edgelist():
# ignore lines with less than 2 nodes
lines = ["1;2", "2 3", "3 4"]
@@ -47,126 +174,6 @@ class TestEdgelist:
cls.XG.add_weighted_edges_from([(1, 2, 5), (1, 2, 5), (1, 2, 1), (3, 3, 42)])
cls.XDG = nx.MultiDiGraph(cls.XG)
- def test_read_edgelist_1(self):
- s = b"""\
-# comment line
-1 2
-# comment line
-2 3
-"""
- bytesIO = io.BytesIO(s)
- G = nx.read_edgelist(bytesIO, nodetype=int)
- assert_edges_equal(G.edges(), [(1, 2), (2, 3)])
-
- def test_read_edgelist_2(self):
- s = b"""\
-# comment line
-1 2 2.0
-# comment line
-2 3 3.0
-"""
- bytesIO = io.BytesIO(s)
- G = nx.read_edgelist(bytesIO, nodetype=int, data=False)
- assert_edges_equal(G.edges(), [(1, 2), (2, 3)])
-
- bytesIO = io.BytesIO(s)
- G = nx.read_weighted_edgelist(bytesIO, nodetype=int)
- assert_edges_equal(
- G.edges(data=True), [(1, 2, {"weight": 2.0}), (2, 3, {"weight": 3.0})]
- )
-
- def test_read_edgelist_3(self):
- s = b"""\
-# comment line
-1 2 {'weight':2.0}
-# comment line
-2 3 {'weight':3.0}
-"""
- bytesIO = io.BytesIO(s)
- G = nx.read_edgelist(bytesIO, nodetype=int, data=False)
- assert_edges_equal(G.edges(), [(1, 2), (2, 3)])
-
- bytesIO = io.BytesIO(s)
- G = nx.read_edgelist(bytesIO, nodetype=int, data=True)
- assert_edges_equal(
- G.edges(data=True), [(1, 2, {"weight": 2.0}), (2, 3, {"weight": 3.0})]
- )
-
- def test_read_edgelist_4(self):
- s = b"""\
-# comment line
-1 2 {'weight':2.0}
-# comment line
-2 3 {'weight':3.0}
-"""
- bytesIO = io.BytesIO(s)
- G = nx.read_edgelist(bytesIO, nodetype=int, data=False)
- assert_edges_equal(G.edges(), [(1, 2), (2, 3)])
-
- bytesIO = io.BytesIO(s)
- G = nx.read_edgelist(bytesIO, nodetype=int, data=True)
- assert_edges_equal(
- G.edges(data=True), [(1, 2, {"weight": 2.0}), (2, 3, {"weight": 3.0})]
- )
-
- s = """\
-# comment line
-1 2 {'weight':2.0}
-# comment line
-2 3 {'weight':3.0}
-"""
- StringIO = io.StringIO(s)
- G = nx.read_edgelist(StringIO, nodetype=int, data=False)
- assert_edges_equal(G.edges(), [(1, 2), (2, 3)])
-
- StringIO = io.StringIO(s)
- G = nx.read_edgelist(StringIO, nodetype=int, data=True)
- assert_edges_equal(
- G.edges(data=True), [(1, 2, {"weight": 2.0}), (2, 3, {"weight": 3.0})]
- )
-
- def test_read_edgelist_5(self):
- s = b"""\
-# comment line
-1 2 {'weight':2.0, 'color':'green'}
-# comment line
-2 3 {'weight':3.0, 'color':'red'}
-"""
- bytesIO = io.BytesIO(s)
- G = nx.read_edgelist(bytesIO, nodetype=int, data=False)
- assert_edges_equal(G.edges(), [(1, 2), (2, 3)])
-
- bytesIO = io.BytesIO(s)
- G = nx.read_edgelist(bytesIO, nodetype=int, data=True)
- assert_edges_equal(
- G.edges(data=True),
- [
- (1, 2, {"weight": 2.0, "color": "green"}),
- (2, 3, {"weight": 3.0, "color": "red"}),
- ],
- )
-
- def test_read_edgelist_6(self):
- s = b"""\
-# comment line
-1, 2, {'weight':2.0, 'color':'green'}
-# comment line
-2, 3, {'weight':3.0, 'color':'red'}
-"""
- bytesIO = io.BytesIO(s)
- G = nx.read_edgelist(bytesIO, nodetype=int, data=False, delimiter=",")
- assert_edges_equal(G.edges(), [(1, 2), (2, 3)])
-
- bytesIO = io.BytesIO(s)
- G = nx.read_edgelist(bytesIO, nodetype=int, data=True, delimiter=",")
- assert_edges_equal(
- G.edges(data=True),
- [
- (1, 2, {"weight": 2.0, "color": "green"}),
- (2, 3, {"weight": 3.0, "color": "red"}),
- ],
- )
-
def test_write_edgelist_1(self):
fh = io.BytesIO()
G = nx.OrderedGraph()