Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace tempfile with tmp_path fixture in test suite. #7221

Merged
merged 2 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 17 additions & 29 deletions networkx/algorithms/bipartite/tests/test_edgelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
Unit tests for bipartite edgelists.
"""
import io
import os
import tempfile

import pytest

Expand Down Expand Up @@ -101,84 +99,74 @@ def test_write_edgelist_4(self):
fh.seek(0)
assert fh.read() == b"1 2 2.0\n3 2 3.0\n"

def test_unicode(self):
def test_unicode(self, tmp_path):
G = nx.Graph()
name1 = chr(2344) + chr(123) + chr(6543)
name2 = chr(5543) + chr(1543) + chr(324)
G.add_edge(name1, "Radiohead", **{name2: 3})
G.add_node(name1, bipartite=0)
G.add_node("Radiohead", bipartite=1)
fd, fname = tempfile.mkstemp()

fname = tmp_path / "edgelist.txt"
bipartite.write_edgelist(G, fname)
H = bipartite.read_edgelist(fname)
assert graphs_equal(G, H)
os.close(fd)
os.unlink(fname)

def test_latin1_issue(self):
def test_latin1_issue(self, tmp_path):
G = nx.Graph()
name1 = chr(2344) + chr(123) + chr(6543)
name2 = chr(5543) + chr(1543) + chr(324)
G.add_edge(name1, "Radiohead", **{name2: 3})
G.add_node(name1, bipartite=0)
G.add_node("Radiohead", bipartite=1)
fd, fname = tempfile.mkstemp()
pytest.raises(
UnicodeEncodeError, bipartite.write_edgelist, G, fname, encoding="latin-1"
)
os.close(fd)
os.unlink(fname)

def test_latin1(self):
fname = tmp_path / "edgelist.txt"
with pytest.raises(UnicodeEncodeError):
bipartite.write_edgelist(G, fname, encoding="latin-1")

def test_latin1(self, tmp_path):
G = nx.Graph()
name1 = "Bj" + chr(246) + "rk"
name2 = chr(220) + "ber"
G.add_edge(name1, "Radiohead", **{name2: 3})
G.add_node(name1, bipartite=0)
G.add_node("Radiohead", bipartite=1)
fd, fname = tempfile.mkstemp()

fname = tmp_path / "edgelist.txt"
bipartite.write_edgelist(G, fname, encoding="latin-1")
H = bipartite.read_edgelist(fname, encoding="latin-1")
assert graphs_equal(G, H)
os.close(fd)
os.unlink(fname)

def test_edgelist_graph(self):
def test_edgelist_graph(self, tmp_path):
G = self.G
(fd, fname) = tempfile.mkstemp()
fname = tmp_path / "edgelist.txt"
bipartite.write_edgelist(G, fname)
H = bipartite.read_edgelist(fname)
H2 = bipartite.read_edgelist(fname)
assert H is not H2 # they should be different graphs
G.remove_node("g") # isolated nodes are not written in edgelist
assert nodes_equal(list(H), list(G))
assert edges_equal(list(H.edges()), list(G.edges()))
os.close(fd)
os.unlink(fname)

def test_edgelist_integers(self):
def test_edgelist_integers(self, tmp_path):
G = nx.convert_node_labels_to_integers(self.G)
(fd, fname) = tempfile.mkstemp()
fname = tmp_path / "edgelist.txt"
bipartite.write_edgelist(G, fname)
H = bipartite.read_edgelist(fname, nodetype=int)
# isolated nodes are not written in edgelist
G.remove_nodes_from(list(nx.isolates(G)))
assert nodes_equal(list(H), list(G))
assert edges_equal(list(H.edges()), list(G.edges()))
os.close(fd)
os.unlink(fname)

def test_edgelist_multigraph(self):
def test_edgelist_multigraph(self, tmp_path):
G = self.MG
(fd, fname) = tempfile.mkstemp()
fname = tmp_path / "edgelist.txt"
bipartite.write_edgelist(G, fname)
H = bipartite.read_edgelist(fname, nodetype=int, create_using=nx.MultiGraph())
H2 = bipartite.read_edgelist(fname, nodetype=int, create_using=nx.MultiGraph())
assert H is not H2 # they should be different graphs
assert nodes_equal(list(H), list(G))
assert edges_equal(list(H.edges()), list(G.edges()))
os.close(fd)
os.unlink(fname)

def test_empty_digraph(self):
with pytest.raises(nx.NetworkXNotImplemented):
Expand Down
27 changes: 6 additions & 21 deletions networkx/drawing/tests/test_agraph.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""Unit tests for PyGraphviz interface."""
import os
import tempfile
import warnings

import pytest
Expand All @@ -25,27 +23,26 @@ def assert_equal(self, G1, G2):
assert edges_equal(G1.edges(), G2.edges())
assert G1.graph["metal"] == G2.graph["metal"]

def agraph_checks(self, G):
@pytest.mark.parametrize(
"G", (nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph())
)
def test_agraph_roundtripping(self, G, tmp_path):
G = self.build_graph(G)
A = nx.nx_agraph.to_agraph(G)
H = nx.nx_agraph.from_agraph(A)
self.assert_equal(G, H)

fd, fname = tempfile.mkstemp()
fname = tmp_path / "test.dot"
nx.drawing.nx_agraph.write_dot(H, fname)
Hin = nx.nx_agraph.read_dot(fname)
self.assert_equal(H, Hin)
os.close(fd)
os.unlink(fname)

(fd, fname) = tempfile.mkstemp()
fname = tmp_path / "fh_test.dot"
with open(fname, "w") as fh:
nx.drawing.nx_agraph.write_dot(H, fh)

with open(fname) as fh:
Hin = nx.nx_agraph.read_dot(fh)
os.close(fd)
os.unlink(fname)
self.assert_equal(H, Hin)

def test_from_agraph_name(self):
Expand Down Expand Up @@ -75,18 +72,6 @@ def test_from_agraph_named_edges(self):
assert isinstance(H, nx.Graph)
assert ("0", "1", {"key": "foo"}) in H.edges(data=True)

def test_undirected(self):
self.agraph_checks(nx.Graph())

def test_directed(self):
self.agraph_checks(nx.DiGraph())

def test_multi_undirected(self):
self.agraph_checks(nx.MultiGraph())

def test_multi_directed(self):
self.agraph_checks(nx.MultiDiGraph())

def test_to_agraph_with_nodedata(self):
G = nx.Graph()
G.add_node(1, color="red")
Expand Down
16 changes: 4 additions & 12 deletions networkx/drawing/tests/test_pydot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Unit tests for pydot drawing functions."""
import os
import tempfile
from io import StringIO

Expand All @@ -12,7 +11,9 @@


class TestPydot:
def pydot_checks(self, G, prog):
@pytest.mark.parametrize("G", (nx.Graph(), nx.DiGraph()))
@pytest.mark.parametrize("prog", ("neato", "dot"))
def test_pydot(self, G, prog, tmp_path):
"""
Validate :mod:`pydot`-based usage of the passed NetworkX graph with the
passed basename of an external GraphViz command (e.g., `dot`, `neato`).
Expand All @@ -39,7 +40,7 @@ def pydot_checks(self, G, prog):
# Validate the original and resulting graphs to be the same.
assert graphs_equal(G, G2)

fd, fname = tempfile.mkstemp()
fname = tmp_path / "out.dot"

# Serialize this "pydot.Dot" instance to a temporary file in dot format
P.write_raw(fname)
Expand Down Expand Up @@ -78,15 +79,6 @@ def pydot_checks(self, G, prog):
# Validate the original and resulting graphs to be the same.
assert graphs_equal(G, Hin)

os.close(fd)
os.unlink(fname)

def test_undirected(self):
self.pydot_checks(nx.Graph(), prog="neato")

def test_directed(self):
self.pydot_checks(nx.DiGraph(), prog="dot")

def test_read_write(self):
G = nx.MultiGraph()
G.graph["name"] = "G"
Expand Down