Fixed database typo and removed unnecessary class identifier.

This commit is contained in:
Batuhan Berk Başoğlu 2020-10-14 10:10:37 -04:00
parent 00ad49a143
commit 45fb349a7d
5098 changed files with 952558 additions and 85 deletions

View file

@ -0,0 +1,2 @@
from networkx.testing.utils import *
from networkx.testing.test import run

View file

@ -0,0 +1,34 @@
def run(verbosity=1, doctest=False):
"""Run NetworkX tests.
Parameters
----------
verbosity: integer, optional
Level of detail in test reports. Higher numbers provide more detail.
doctest: bool, optional
True to run doctests in code modules
"""
import pytest
pytest_args = ["-l"]
if verbosity and int(verbosity) > 1:
pytest_args += ["-" + "v" * (int(verbosity) - 1)]
if doctest:
pytest_args += ["--doctest-modules"]
pytest_args += ["--pyargs", "networkx"]
try:
code = pytest.main(pytest_args)
except SystemExit as exc:
code = exc.code
return code == 0
if __name__ == "__main__":
run()

View file

@ -0,0 +1,160 @@
import networkx as nx
from networkx.testing import assert_graphs_equal, assert_edges_equal, assert_nodes_equal
# thanks to numpy for this GenericTest class (numpy/testing/test_utils.py)
class _GenericTest:
@classmethod
def _test_equal(cls, a, b):
cls._assert_func(a, b)
@classmethod
def _test_not_equal(cls, a, b):
try:
cls._assert_func(a, b)
passed = True
except AssertionError:
pass
else:
raise AssertionError("a and b are found equal but are not")
class TestNodesEqual(_GenericTest):
_assert_func = assert_nodes_equal
def test_nodes_equal(self):
a = [1, 2, 5, 4]
b = [4, 5, 1, 2]
self._test_equal(a, b)
def test_nodes_not_equal(self):
a = [1, 2, 5, 4]
b = [4, 5, 1, 3]
self._test_not_equal(a, b)
def test_nodes_with_data_equal(self):
G = nx.Graph()
G.add_nodes_from([1, 2, 3], color="red")
H = nx.Graph()
H.add_nodes_from([1, 2, 3], color="red")
self._test_equal(G.nodes(data=True), H.nodes(data=True))
def test_edges_with_data_not_equal(self):
G = nx.Graph()
G.add_nodes_from([1, 2, 3], color="red")
H = nx.Graph()
H.add_nodes_from([1, 2, 3], color="blue")
self._test_not_equal(G.nodes(data=True), H.nodes(data=True))
class TestEdgesEqual(_GenericTest):
_assert_func = assert_edges_equal
def test_edges_equal(self):
a = [(1, 2), (5, 4)]
b = [(4, 5), (1, 2)]
self._test_equal(a, b)
def test_edges_not_equal(self):
a = [(1, 2), (5, 4)]
b = [(4, 5), (1, 3)]
self._test_not_equal(a, b)
def test_edges_with_data_equal(self):
G = nx.MultiGraph()
nx.add_path(G, [0, 1, 2], weight=1)
H = nx.MultiGraph()
nx.add_path(H, [0, 1, 2], weight=1)
self._test_equal(G.edges(data=True, keys=True), H.edges(data=True, keys=True))
def test_edges_with_data_not_equal(self):
G = nx.MultiGraph()
nx.add_path(G, [0, 1, 2], weight=1)
H = nx.MultiGraph()
nx.add_path(H, [0, 1, 2], weight=2)
self._test_not_equal(
G.edges(data=True, keys=True), H.edges(data=True, keys=True)
)
def test_no_edges(self):
G = nx.MultiGraph()
H = nx.MultiGraph()
self._test_equal(G.edges(data=True, keys=True), H.edges(data=True, keys=True))
def test_duplicate_edges(self):
a = [(1, 2), (5, 4), (1, 2)]
b = [(4, 5), (1, 2)]
self._test_not_equal(a, b)
def test_duplicate_edges_with_data(self):
a = [(1, 2, {"weight": 10}), (5, 4), (1, 2, {"weight": 1})]
b = [(4, 5), (1, 2), (1, 2, {"weight": 1})]
self._test_not_equal(a, b)
def test_order_of_edges_with_data(self):
a = [(1, 2, {"weight": 10}), (1, 2, {"weight": 1})]
b = [(1, 2, {"weight": 1}), (1, 2, {"weight": 10})]
self._test_equal(a, b)
def test_order_of_multiedges(self):
wt1 = {"weight": 1}
wt2 = {"weight": 2}
a = [(1, 2, wt1), (1, 2, wt1), (1, 2, wt2)]
b = [(1, 2, wt1), (1, 2, wt2), (1, 2, wt2)]
self._test_not_equal(a, b)
def test_order_of_edges_with_keys(self):
a = [(1, 2, 0, {"weight": 10}), (1, 2, 1, {"weight": 1}), (1, 2, 2)]
b = [(1, 2, 1, {"weight": 1}), (1, 2, 2), (1, 2, 0, {"weight": 10})]
self._test_equal(a, b)
a = [(1, 2, 1, {"weight": 10}), (1, 2, 0, {"weight": 1}), (1, 2, 2)]
b = [(1, 2, 1, {"weight": 1}), (1, 2, 2), (1, 2, 0, {"weight": 10})]
self._test_not_equal(a, b)
class TestGraphsEqual(_GenericTest):
_assert_func = assert_graphs_equal
def test_graphs_equal(self):
G = nx.path_graph(4)
H = nx.Graph()
nx.add_path(H, range(4))
self._test_equal(G, H)
def test_digraphs_equal(self):
G = nx.path_graph(4, create_using=nx.DiGraph())
H = nx.DiGraph()
nx.add_path(H, range(4))
self._test_equal(G, H)
def test_multigraphs_equal(self):
G = nx.path_graph(4, create_using=nx.MultiGraph())
H = nx.MultiGraph()
nx.add_path(H, range(4))
self._test_equal(G, H)
def test_multidigraphs_equal(self):
G = nx.path_graph(4, create_using=nx.MultiDiGraph())
H = nx.MultiDiGraph()
nx.add_path(H, range(4))
self._test_equal(G, H)
def test_graphs_not_equal(self):
G = nx.path_graph(4)
H = nx.Graph()
nx.add_cycle(H, range(4))
self._test_not_equal(G, H)
def test_graphs_not_equal2(self):
G = nx.path_graph(4)
H = nx.Graph()
nx.add_path(H, range(3))
self._test_not_equal(G, H)
def test_graphs_not_equal3(self):
G = nx.path_graph(4)
H = nx.Graph()
nx.add_path(H, range(4))
H.name = "path_graph(4)"
self._test_not_equal(G, H)

View file

@ -0,0 +1,65 @@
__all__ = [
"assert_nodes_equal",
"assert_edges_equal",
"assert_graphs_equal",
"almost_equal",
]
def almost_equal(x, y, places=7):
return round(abs(x - y), places) == 0
def assert_nodes_equal(nodes1, nodes2):
# Assumes iterables of nodes, or (node,datadict) tuples
nlist1 = list(nodes1)
nlist2 = list(nodes2)
try:
d1 = dict(nlist1)
d2 = dict(nlist2)
except (ValueError, TypeError):
d1 = dict.fromkeys(nlist1)
d2 = dict.fromkeys(nlist2)
assert d1 == d2
def assert_edges_equal(edges1, edges2):
# Assumes iterables with u,v nodes as
# edge tuples (u,v), or
# edge tuples with data dicts (u,v,d), or
# edge tuples with keys and data dicts (u,v,k, d)
from collections import defaultdict
d1 = defaultdict(dict)
d2 = defaultdict(dict)
c1 = 0
for c1, e in enumerate(edges1):
u, v = e[0], e[1]
data = [e[2:]]
if v in d1[u]:
data = d1[u][v] + data
d1[u][v] = data
d1[v][u] = data
c2 = 0
for c2, e in enumerate(edges2):
u, v = e[0], e[1]
data = [e[2:]]
if v in d2[u]:
data = d2[u][v] + data
d2[u][v] = data
d2[v][u] = data
assert c1 == c2
# can check one direction because lengths are the same.
for n, nbrdict in d1.items():
for nbr, datalist in nbrdict.items():
assert n in d2
assert nbr in d2[n]
d2datalist = d2[n][nbr]
for data in datalist:
assert datalist.count(data) == d2datalist.count(data)
def assert_graphs_equal(graph1, graph2):
assert graph1.adj == graph2.adj
assert graph1.nodes == graph2.nodes
assert graph1.graph == graph2.graph