Skip to content

Commit c0fa9f9

Browse files
tests: add BFSPartitioner tests
1 parent f9dc25f commit c0fa9f9

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import tempfile
2+
3+
import pytest
4+
5+
from graphgen.bases.datatypes import Community
6+
from graphgen.models import BFSPartitioner, NetworkXStorage
7+
8+
9+
@pytest.mark.asyncio
10+
async def test_empty_graph():
11+
with tempfile.TemporaryDirectory() as tmpdir:
12+
storage = NetworkXStorage(working_dir=tmpdir, namespace="empty")
13+
partitioner = BFSPartitioner()
14+
communities = await partitioner.partition(storage, max_units_per_community=5)
15+
assert communities == []
16+
17+
18+
@pytest.mark.asyncio
19+
async def test_single_node():
20+
nodes = [("A", {"desc": "alone"})]
21+
edges = []
22+
with tempfile.TemporaryDirectory() as tmpdir:
23+
storage = NetworkXStorage(working_dir=tmpdir, namespace="single_node")
24+
25+
for nid, ndata in nodes:
26+
await storage.upsert_node(nid, ndata)
27+
for src, tgt, edata in edges:
28+
await storage.upsert_edge(src, tgt, edata)
29+
30+
partitioner = BFSPartitioner()
31+
communities: list[Community] = await partitioner.partition(
32+
storage, max_units_per_community=5
33+
)
34+
assert len(communities) == 1
35+
assert communities[0].nodes == ["A"]
36+
assert communities[0].edges == []
37+
38+
39+
@pytest.mark.asyncio
40+
async def test_small_graph():
41+
"""
42+
0 - 1 - 2
43+
| | |
44+
3 - 4 - 5
45+
6 nodes & 7 edges, max_units=4 => at least 3 communities
46+
"""
47+
nodes = [(str(i), {"desc": f"node{i}"}) for i in range(6)]
48+
edges = [
49+
("0", "1", {"desc": "e01"}),
50+
("1", "2", {"desc": "e12"}),
51+
("0", "3", {"desc": "e03"}),
52+
("1", "4", {"desc": "e14"}),
53+
("2", "5", {"desc": "e25"}),
54+
("3", "4", {"desc": "e34"}),
55+
("4", "5", {"desc": "e45"}),
56+
]
57+
58+
with tempfile.TemporaryDirectory() as tmpdir:
59+
storage = NetworkXStorage(working_dir=tmpdir, namespace="small_graph")
60+
61+
for nid, ndata in nodes:
62+
await storage.upsert_node(nid, ndata)
63+
for src, tgt, edata in edges:
64+
await storage.upsert_edge(src, tgt, edata)
65+
66+
partitioner = BFSPartitioner()
67+
communities: list[Community] = await partitioner.partition(
68+
storage, max_units_per_community=4
69+
)
70+
71+
assert len(communities) <= 5
72+
73+
all_nodes = set()
74+
all_edges = set()
75+
for c in communities:
76+
assert len(c.nodes) + len(c.edges) <= 4
77+
all_nodes.update(c.nodes)
78+
all_edges.update(c.edges)
79+
80+
assert all_nodes == {str(i) for i in range(6)}
81+
assert len(all_edges) == 7

0 commit comments

Comments
 (0)