70 lines
2.8 KiB
Python
70 lines
2.8 KiB
Python
import json
|
|
from pathlib import Path
|
|
|
|
from sqlalchemy import func, select
|
|
|
|
from graphrag_platform.config import Settings
|
|
from graphrag_platform.models import Community, Edge, Node, SearchItem
|
|
from graphrag_platform.runtime import StageContext
|
|
from graphrag_platform.stage_loader import load_stage
|
|
|
|
from conftest import FakeLLM
|
|
|
|
|
|
def test_all_ten_stages_build_and_query_graph(tmp_path: Path) -> None:
|
|
input_file = tmp_path / "documents.jsonl"
|
|
input_file.write_text(
|
|
json.dumps(
|
|
{
|
|
"schema_version": "1.0",
|
|
"external_id": "atlas-doc",
|
|
"corpus": "atlas-corpus",
|
|
"url": "https://docs.example/atlas",
|
|
"retrieved_at": "2026-10-13T18:00:00Z",
|
|
"body": (
|
|
"<html><head><title>Atlas</title></head><body><main>"
|
|
"<h1>Atlas</h1><p>Acme created Atlas. Atlas provides graph "
|
|
"retrieval for knowledge applications.</p></main></body></html>"
|
|
),
|
|
"metadata": {"collector": "Web-Access", "stored_by": "Data-Store"},
|
|
}
|
|
)
|
|
+ "\n",
|
|
encoding="utf-8",
|
|
)
|
|
settings = Settings(
|
|
_env_file=None,
|
|
database_url=f"sqlite:///{tmp_path / 'graph.db'}",
|
|
auto_create_schema=True,
|
|
artifact_dir=tmp_path / "artifacts",
|
|
corpus_name="atlas-corpus",
|
|
chunk_size=300,
|
|
chunk_overlap=30,
|
|
retrieval_top_k=5,
|
|
llm_embedding_dimensions=3,
|
|
)
|
|
context = StageContext.create(settings, llm=FakeLLM())
|
|
|
|
assert load_stage(1).run(context, input_jsonl=input_file)["normalized"] == 1
|
|
assert load_stage(2).run(context)["chunks"] == 1
|
|
assert load_stage(3).run(context)["entity_types"] == 6
|
|
assert load_stage(4).run(context)["extracted_chunks"] == 1
|
|
assert load_stage(5).run(context) == {"entities": 2, "relationships": 1}
|
|
assert load_stage(6).run(context) == {"nodes": 2, "edges": 1}
|
|
assert load_stage(7).run(context)["communities"] == 1
|
|
assert load_stage(8).run(context)["indexed_items"] == 5
|
|
|
|
retrieval = load_stage(9).run(context, question="Who created Atlas?")
|
|
response = load_stage(10).run(context, question="Who created Atlas?")
|
|
|
|
assert retrieval["context"]
|
|
assert retrieval["graph_context"][0]["relationship"] == "CREATED"
|
|
assert retrieval["sources"][0]["url"] == "https://docs.example/atlas"
|
|
assert response["answer"] == "Acme created Atlas. [S1]"
|
|
assert response["sources"][0]["citation"] == "S1"
|
|
with context.storage.session() as session:
|
|
assert session.scalar(select(func.count()).select_from(Node)) == 2
|
|
assert session.scalar(select(func.count()).select_from(Edge)) == 1
|
|
assert session.scalar(select(func.count()).select_from(Community)) == 1
|
|
assert session.scalar(select(func.count()).select_from(SearchItem)) == 5
|