Skip to content

Commit 39ed2e8

Browse files
authored
Merge pull request #133 from neo4j-labs/fix-flaky-pytests
Fix flaky python tests
2 parents bbcea74 + b49925b commit 39ed2e8

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

crates/mate/tests/ds_test.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import numpy as np
22
import pandas as pd
33

4-
from graph_mate import DiGraph, Graph
4+
from graph_mate import DiGraph, Graph, Layout
55

66

77
def test_numpy_graph():
88
el = np.array([[0, 1], [2, 3], [4, 1]], dtype=np.uint32)
9-
g = Graph.from_numpy(el)
9+
g = Graph.from_numpy(el, layout=Layout.Sorted)
1010

1111
assert g.node_count() == 5
1212
assert g.edge_count() == 3
@@ -20,7 +20,7 @@ def test_numpy_graph():
2020

2121
def test_pandas_graph():
2222
df = pd.DataFrame({"source": [0, 2, 4], "target": [1, 3, 1]})
23-
g = Graph.from_pandas(df)
23+
g = Graph.from_pandas(df, layout=Layout.Sorted)
2424

2525
assert g.node_count() == 5
2626
assert g.edge_count() == 3
@@ -34,7 +34,7 @@ def test_pandas_graph():
3434

3535
def test_numpy_digraph():
3636
el = np.array([[0, 1], [2, 3], [4, 1]], dtype=np.uint32)
37-
g = DiGraph.from_numpy(el)
37+
g = DiGraph.from_numpy(el, layout=Layout.Sorted)
3838

3939
assert g.node_count() == 5
4040
assert g.edge_count() == 3
@@ -49,7 +49,7 @@ def test_numpy_digraph():
4949

5050
def test_pandas_digraph():
5151
df = pd.DataFrame({"source": [0, 2, 4], "target": [1, 3, 1]})
52-
g = DiGraph.from_pandas(df)
52+
g = DiGraph.from_pandas(df, layout=Layout.Sorted)
5353

5454
assert g.node_count() == 5
5555
assert g.edge_count() == 3

crates/mate/tests/graph_test.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,33 @@ def test_load_graph(g: DiGraph):
1212

1313

1414
def test_to_undirected(g: DiGraph, ug: Graph):
15-
g = g.to_undirected()
15+
undirected = g.to_undirected()
1616

17-
for n in range(g.node_count()):
18-
assert set(g.copy_neighbors(n)) == set(ug.copy_neighbors(n))
17+
for n in range(undirected.node_count()):
18+
assert set(undirected.copy_neighbors(n)) == set(ug.copy_neighbors(n))
1919

2020

2121
def test_to_undirected_with_layout():
2222
g = DiGraph.from_numpy(
2323
np.array([[0, 1], [0, 1], [0, 2], [1, 2], [2, 1], [0, 3]], dtype=np.uint32)
2424
)
2525

26+
def compare_unsorted(expect, actual):
27+
sorted = expect.copy()
28+
sorted.sort()
29+
return np.array_equal(sorted, actual)
30+
2631
ug = g.to_undirected()
27-
assert np.array_equal(ug.neighbors(0), [1, 1, 2, 3])
28-
assert np.array_equal(ug.neighbors(1), [2, 0, 0, 2])
29-
assert np.array_equal(ug.neighbors(2), [1, 0, 1])
30-
assert np.array_equal(ug.neighbors(3), [0])
32+
assert compare_unsorted(ug.neighbors(0), [1, 1, 2, 3])
33+
assert compare_unsorted(ug.neighbors(1), [0, 0, 2, 2])
34+
assert compare_unsorted(ug.neighbors(2), [0, 1, 1])
35+
assert compare_unsorted(ug.neighbors(3), [0])
3136

3237
ug = g.to_undirected(Layout.Unsorted)
33-
assert np.array_equal(ug.neighbors(0), [1, 1, 2, 3])
34-
assert np.array_equal(ug.neighbors(1), [2, 0, 0, 2])
35-
assert np.array_equal(ug.neighbors(2), [1, 0, 1])
36-
assert np.array_equal(ug.neighbors(3), [0])
38+
assert compare_unsorted(ug.neighbors(0), [1, 1, 2, 3])
39+
assert compare_unsorted(ug.neighbors(1), [0, 0, 2, 2])
40+
assert compare_unsorted(ug.neighbors(2), [0, 1, 1])
41+
assert compare_unsorted(ug.neighbors(3), [0])
3742

3843
ug = g.to_undirected(Layout.Sorted)
3944
assert np.array_equal(ug.neighbors(0), [1, 1, 2, 3])

0 commit comments

Comments
 (0)