Initial curated RAGcore source from c16667f0c9397e2bfc8dd0255b114fba23c8d05e

This commit is contained in:
Public Source Publisher committed 2026-10-01 01:34:21 +02:00
commit 7ca726caf2
659 files changed
+98171

No files matched your search

@@ -0,0 +1,122 @@
"""Real Postgres coverage for the ``DocumentRepository`` methods a source-keyed
sync client needs: look a document up by ``(workspace_id, source_key, external_id)``
without knowing its id, and list every version of a document regardless of state."""
from __future__ import annotations
import asyncio
import sys
from collections.abc import AsyncIterator
from uuid import uuid4
import pytest
from sqlalchemy.ext.asyncio import AsyncEngine
from ragcore.application.documents import (
DocumentRecord,
DocumentVersionRecord,
DocumentVersionState,
)
from ragcore.application.sources import implicit_source_record
from ragcore.config import Settings
from ragcore.infrastructure.database import create_database_engine, create_session_factory
from ragcore.infrastructure.database.content import PostgresDocumentRepository
from ragcore.infrastructure.database.sources import PostgresSourceRegistry
pytestmark = pytest.mark.integration
@pytest.fixture(scope="session")
def event_loop_policy() -> asyncio.AbstractEventLoopPolicy:
if sys.platform == "win32":
return asyncio.WindowsSelectorEventLoopPolicy()
return asyncio.get_event_loop_policy()
async def _register_source(factory, document) -> None: # type: ignore[no-untyped-def]
"""The content schema requires a registry row for every document's source."""
await PostgresSourceRegistry(factory).ensure_source(
implicit_source_record(
source_id=document.source_id,
workspace_id=document.workspace_id,
space_id=document.space_id,
)
)
@pytest.fixture
async def db_engine() -> AsyncIterator[AsyncEngine]:
engine = create_database_engine(Settings(env="test"))
try:
yield engine
finally:
await engine.dispose()
async def test_get_document_by_source_matches_the_workspace_source_key_external_id_triple(
db_engine: AsyncEngine,
) -> None:
factory = create_session_factory(db_engine)
documents = PostgresDocumentRepository(factory)
workspace_id = uuid4()
source_key = str(uuid4())
external_id = f"page-{uuid4()}"
document = DocumentRecord(
id=uuid4(),
workspace_id=workspace_id,
space_id=uuid4(),
source_key=source_key,
external_id=external_id,
title="Wiki page",
)
await _register_source(factory, document)
await documents.create_document(document)
found = await documents.get_document_by_source(workspace_id, source_key, external_id)
assert found == document
assert await documents.get_document_by_source(workspace_id, source_key, "other-id") is None
assert await documents.get_document_by_source(uuid4(), source_key, external_id) is None
async def test_list_versions_returns_every_state_newest_first(db_engine: AsyncEngine) -> None:
factory = create_session_factory(db_engine)
documents = PostgresDocumentRepository(factory)
document = DocumentRecord(
id=uuid4(),
workspace_id=uuid4(),
space_id=uuid4(),
source_key=str(uuid4()),
external_id=f"page-{uuid4()}",
title="Wiki page",
)
await _register_source(factory, document)
await documents.create_document(document)
first = DocumentVersionRecord(
id=uuid4(),
document_id=document.id,
source_fingerprint="a" * 64,
content_hash="b" * 64,
parser_version="text-v1",
chunk_profile_version="prose-v1",
state=DocumentVersionState.SUPERSEDED,
)
second = DocumentVersionRecord(
id=uuid4(),
document_id=document.id,
source_fingerprint="c" * 64,
content_hash="d" * 64,
parser_version="text-v1",
chunk_profile_version="prose-v1",
state=DocumentVersionState.PUBLISHED,
)
await documents.create_version(first)
await documents.create_version(second)
versions = await documents.list_versions(document.id)
assert {version.id for version in versions} == {first.id, second.id}
assert versions[0].id == second.id
assert await documents.list_versions(uuid4()) == ()