114 lines
3.6 KiB
Python
114 lines
3.6 KiB
Python
from pathlib import Path
|
|
|
|
import pytest
|
|
from sqlalchemy import select
|
|
|
|
from graphrag_platform.config import Settings
|
|
from graphrag_platform.models import Chunk, Document, Extraction, ResolvedRelationship
|
|
from graphrag_platform.runtime import StageContext
|
|
from graphrag_platform.stage_loader import load_stage
|
|
|
|
from conftest import FakeLLM
|
|
|
|
|
|
def test_chunk_overlap_must_be_smaller_than_chunk_size(tmp_path: Path) -> None:
|
|
settings = Settings(
|
|
_env_file=None,
|
|
database_url=f"sqlite:///{tmp_path / 'graph.db'}",
|
|
auto_create_schema=True,
|
|
artifact_dir=tmp_path / "artifacts",
|
|
chunk_size=200,
|
|
chunk_overlap=200,
|
|
)
|
|
stage = load_stage(2)
|
|
|
|
with pytest.raises(ValueError, match="CHUNK_OVERLAP"):
|
|
stage.chunk_text("word " * 100, size=200, overlap=200)
|
|
|
|
|
|
def test_reconciliation_keeps_cross_chunk_relationship(tmp_path: Path) -> None:
|
|
settings = Settings(
|
|
_env_file=None,
|
|
database_url=f"sqlite:///{tmp_path / 'graph.db'}",
|
|
auto_create_schema=True,
|
|
artifact_dir=tmp_path / "artifacts",
|
|
corpus_name="unit",
|
|
)
|
|
context = StageContext.create(settings, llm=FakeLLM())
|
|
context.initialize()
|
|
with context.storage.session() as session:
|
|
document = Document(
|
|
corpus="unit",
|
|
url="https://example.test/source",
|
|
text="Acme created Atlas.",
|
|
content_hash="document",
|
|
metadata_json={},
|
|
)
|
|
session.add(document)
|
|
session.flush()
|
|
first = Chunk(
|
|
document_id=document.id,
|
|
corpus="unit",
|
|
ordinal=0,
|
|
text="Acme created",
|
|
content_hash="first",
|
|
)
|
|
second = Chunk(
|
|
document_id=document.id,
|
|
corpus="unit",
|
|
ordinal=1,
|
|
text="Atlas",
|
|
content_hash="second",
|
|
)
|
|
session.add_all([first, second])
|
|
session.flush()
|
|
session.add_all(
|
|
[
|
|
Extraction(
|
|
chunk_id=first.id,
|
|
corpus="unit",
|
|
model_name="test",
|
|
payload_json={
|
|
"entities": [
|
|
{
|
|
"canonical_name": "Acme",
|
|
"entity_type": "Organization",
|
|
"aliases": [],
|
|
}
|
|
],
|
|
"relationships": [
|
|
{
|
|
"source": "Acme",
|
|
"target": "Atlas",
|
|
"relationship_type": "CREATED",
|
|
}
|
|
],
|
|
},
|
|
),
|
|
Extraction(
|
|
chunk_id=second.id,
|
|
corpus="unit",
|
|
model_name="test",
|
|
payload_json={
|
|
"entities": [
|
|
{
|
|
"canonical_name": "Atlas",
|
|
"entity_type": "Product",
|
|
"aliases": [],
|
|
}
|
|
],
|
|
"relationships": [],
|
|
},
|
|
),
|
|
]
|
|
)
|
|
|
|
result = load_stage(5).run(context)
|
|
|
|
assert result == {"entities": 2, "relationships": 1}
|
|
with context.storage.session() as session:
|
|
relationship = session.scalar(select(ResolvedRelationship))
|
|
assert relationship is not None
|
|
assert relationship.source_key == "acme"
|
|
assert relationship.target_key == "atlas"
|