mirror of
https://github.com/safishamsi/graphify.git
synced 2026-07-12 18:37:12 +00:00
143 lines
4.7 KiB
Python
143 lines
4.7 KiB
Python
import json
|
|
import tempfile
|
|
from pathlib import Path
|
|
from graphify.build import build_from_json
|
|
from graphify.cluster import cluster
|
|
from graphify.export import to_json, to_cypher, to_graphml, to_html, to_canvas
|
|
|
|
FIXTURES = Path(__file__).parent / "fixtures"
|
|
|
|
def make_graph():
|
|
return build_from_json(json.loads((FIXTURES / "extraction.json").read_text()))
|
|
|
|
def test_to_json_creates_file():
|
|
G = make_graph()
|
|
communities = cluster(G)
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
out = Path(tmp) / "graph.json"
|
|
to_json(G, communities, str(out))
|
|
assert out.exists()
|
|
|
|
def test_to_json_valid_json():
|
|
G = make_graph()
|
|
communities = cluster(G)
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
out = Path(tmp) / "graph.json"
|
|
to_json(G, communities, str(out))
|
|
data = json.loads(out.read_text())
|
|
assert "nodes" in data
|
|
assert "links" in data
|
|
|
|
def test_to_json_nodes_have_community():
|
|
G = make_graph()
|
|
communities = cluster(G)
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
out = Path(tmp) / "graph.json"
|
|
to_json(G, communities, str(out))
|
|
data = json.loads(out.read_text())
|
|
for node in data["nodes"]:
|
|
assert "community" in node
|
|
|
|
def test_to_cypher_creates_file():
|
|
G = make_graph()
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
out = Path(tmp) / "cypher.txt"
|
|
to_cypher(G, str(out))
|
|
assert out.exists()
|
|
|
|
def test_to_cypher_contains_merge_statements():
|
|
G = make_graph()
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
out = Path(tmp) / "cypher.txt"
|
|
to_cypher(G, str(out))
|
|
content = out.read_text()
|
|
assert "MERGE" in content
|
|
|
|
def test_to_graphml_creates_file():
|
|
G = make_graph()
|
|
communities = cluster(G)
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
out = Path(tmp) / "graph.graphml"
|
|
to_graphml(G, communities, str(out))
|
|
assert out.exists()
|
|
|
|
def test_to_graphml_valid_xml():
|
|
G = make_graph()
|
|
communities = cluster(G)
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
out = Path(tmp) / "graph.graphml"
|
|
to_graphml(G, communities, str(out))
|
|
content = out.read_text()
|
|
assert "<graphml" in content
|
|
assert "<node" in content
|
|
|
|
def test_to_graphml_has_community_attribute():
|
|
G = make_graph()
|
|
communities = cluster(G)
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
out = Path(tmp) / "graph.graphml"
|
|
to_graphml(G, communities, str(out))
|
|
content = out.read_text()
|
|
assert "community" in content
|
|
|
|
def test_to_html_creates_file():
|
|
G = make_graph()
|
|
communities = cluster(G)
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
out = Path(tmp) / "graph.html"
|
|
to_html(G, communities, str(out))
|
|
assert out.exists()
|
|
|
|
def test_to_html_contains_visjs():
|
|
G = make_graph()
|
|
communities = cluster(G)
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
out = Path(tmp) / "graph.html"
|
|
to_html(G, communities, str(out))
|
|
content = out.read_text()
|
|
assert "vis-network" in content
|
|
|
|
def test_to_html_contains_search():
|
|
G = make_graph()
|
|
communities = cluster(G)
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
out = Path(tmp) / "graph.html"
|
|
to_html(G, communities, str(out))
|
|
content = out.read_text()
|
|
assert "search" in content.lower()
|
|
|
|
def test_to_html_contains_legend_with_labels():
|
|
G = make_graph()
|
|
communities = cluster(G)
|
|
labels = {cid: f"Group {cid}" for cid in communities}
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
out = Path(tmp) / "graph.html"
|
|
to_html(G, communities, str(out), community_labels=labels)
|
|
content = out.read_text()
|
|
assert "Group 0" in content
|
|
|
|
def test_to_html_contains_nodes_and_edges():
|
|
G = make_graph()
|
|
communities = cluster(G)
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
out = Path(tmp) / "graph.html"
|
|
to_html(G, communities, str(out))
|
|
content = out.read_text()
|
|
assert "RAW_NODES" in content
|
|
assert "RAW_EDGES" in content
|
|
|
|
|
|
def test_to_canvas_file_paths_relative_to_vault():
|
|
"""Node file paths in canvas must be vault-root-relative (just fname.md), not hardcoded."""
|
|
G = make_graph()
|
|
communities = cluster(G)
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
out = Path(tmp) / "graph.canvas"
|
|
to_canvas(G, communities, str(out))
|
|
data = json.loads(out.read_text())
|
|
file_nodes = [n for n in data["nodes"] if n.get("type") == "file"]
|
|
assert file_nodes, "canvas should contain file nodes"
|
|
for node in file_nodes:
|
|
assert "/" not in node["file"], f"file path should not contain '/': {node['file']}"
|
|
assert node["file"].endswith(".md")
|