Files
graphify/tests/test_cluster.py
T
Safi ce47198be1 feat: Claude Code skill, Obsidian vault, install, tests
skill.md with full pipeline steps, Obsidian as default output (canvas, tags,
dataview, graph colors), two-command install, 71 tests, .gitignore, deps
2026-04-04 18:53:43 +01:00

53 lines
1.5 KiB
Python

import json
import networkx as nx
from pathlib import Path
from graphify.build import build_from_json
from graphify.cluster import cluster, cohesion_score, score_all
FIXTURES = Path(__file__).parent / "fixtures"
def make_graph():
return build_from_json(json.loads((FIXTURES / "extraction.json").read_text()))
def test_cluster_returns_dict():
G = make_graph()
communities = cluster(G)
assert isinstance(communities, dict)
def test_cluster_covers_all_nodes():
G = make_graph()
communities = cluster(G)
all_nodes = {n for nodes in communities.values() for n in nodes}
assert all_nodes == set(G.nodes)
def test_cohesion_score_complete_graph():
G = nx.complete_graph(4)
G = nx.relabel_nodes(G, {i: str(i) for i in G.nodes})
score = cohesion_score(G, list(G.nodes))
assert score == 1.0
def test_cohesion_score_single_node():
G = nx.Graph()
G.add_node("a")
score = cohesion_score(G, ["a"])
assert score == 1.0
def test_cohesion_score_disconnected():
G = nx.Graph()
G.add_nodes_from(["a", "b", "c"])
score = cohesion_score(G, ["a", "b", "c"])
assert score == 0.0
def test_cohesion_score_range():
G = make_graph()
communities = cluster(G)
for cid, nodes in communities.items():
score = cohesion_score(G, nodes)
assert 0.0 <= score <= 1.0
def test_score_all_keys_match_communities():
G = make_graph()
communities = cluster(G)
scores = score_all(G, communities)
assert set(scores.keys()) == set(communities.keys())