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())