Files
rag-technical-docs-assistant/tests/integration/test_constraints.py
T
amirhosein mazaheri baf58da562 feat(ingestion): add Phase 1 database and application foundation
Add the scoped PostgreSQL/pgvector schema, independent ingestion settings and ports, embedding profile checks, and trusted administrator bootstrap.

Include repository contracts and guidance, organized provider-free and PostgreSQL tests, deployment setup, and byte-preserving migration attributes.

Validation: 163 tests passed; all 48 PostgreSQL cases passed with pgvector Python 0.5.0 and 0.3.6. No production schema or historical metadata was changed.
2026-10-05 16:38:28 +03:30

132 lines
6.5 KiB
Python

"""Real PostgreSQL scoped foreign keys, states and uniqueness."""
from uuid import uuid4
import pytest
from sqlalchemy import text
from tests.support.database_rows import seed, rejected
from app.infrastructure.database import SqlDocumentRepository
@pytest.mark.parametrize("sql", [
"UPDATE users SET role='superuser' WHERE id=:actor",
"UPDATE documents SET status='unknown' WHERE id=:doc",
"UPDATE documents SET status='ready' WHERE id=:doc",
"UPDATE documents SET is_active=true WHERE id=:doc",
"UPDATE documents SET size_bytes=0 WHERE id=:doc",
"UPDATE documents SET chunk_count=-1 WHERE id=:doc",
"UPDATE ingestion_jobs SET status='unknown' WHERE document_id=:doc",
"UPDATE ingestion_jobs SET stage='unknown' WHERE document_id=:doc",
"UPDATE ingestion_jobs SET attempt_count=-1 WHERE document_id=:doc",
"UPDATE ingestion_jobs SET retry_generation=-1 WHERE document_id=:doc",
"UPDATE ingestion_jobs SET status='processing' WHERE document_id=:doc",
"UPDATE ingestion_jobs SET claim_token=:token WHERE document_id=:doc",
"UPDATE organization_settings SET access_epoch=0 WHERE organization_id=:org",
"UPDATE organization_settings SET revision=0 WHERE organization_id=:org",
"UPDATE organization_settings SET knowledge_revision=0 WHERE organization_id=:org",
"UPDATE users SET username='UPPERCASE' WHERE id=:actor",
"UPDATE users SET username='' WHERE id=:actor",
"UPDATE documents SET upload_payload_hash='invalid' WHERE id=:doc",
"UPDATE documents SET file_sha256='invalid' WHERE id=:doc",
"UPDATE ingestion_jobs SET lease_expires_at=now() WHERE document_id=:doc",
"UPDATE ingestion_jobs SET status='processing',claim_token=:token WHERE document_id=:doc",
"UPDATE ingestion_jobs SET status='processing',lease_expires_at=now() WHERE document_id=:doc",
])
def test_invalid_states_rejected(migrated_db, sql):
engine, _ = migrated_db
with engine.begin() as connection:
org, actor, doc = seed(connection)
connection.execute(text("""
INSERT INTO ingestion_jobs(id,organization_id,document_id) VALUES (:id,:org,:doc)
"""), {"id": uuid4(), "org": org, "doc": doc})
connection.execute(text("""
INSERT INTO organization_settings(organization_id,assistant_name,instruction)
VALUES (:org,'test','')
"""), {"org": org})
rejected(engine, sql, {"org": org, "actor": actor, "doc": doc, "token": uuid4()})
def test_all_scoped_foreign_keys(migrated_db):
engine, _ = migrated_db
with engine.begin() as connection:
org, actor, doc = seed(connection)
other_org, other_actor, other_doc = seed(connection)
connection.execute(text("""
INSERT INTO organization_settings(organization_id,assistant_name,instruction)
VALUES (:org,'test','')
"""), {"org": org})
for sql in (
"UPDATE users SET organization_id=:org WHERE id=:actor",
"UPDATE documents SET uploaded_by=:actor WHERE id=:doc",
"UPDATE organization_settings SET updated_by=:actor WHERE organization_id=:org",
"""INSERT INTO ingestion_jobs(id,organization_id,document_id)
VALUES (:id,:org,:other_doc)""",
"""INSERT INTO document_chunks(id,organization_id,document_id,ordinal,text,embedding)
VALUES (:id,:org,:other_doc,0,'test','[1,2,3]')""",
"""INSERT INTO audit_events(id,organization_id,actor_user_id,action)
VALUES (:id,:org,:actor,'upload')""",
):
rejected(engine, sql, {"id": uuid4(), "org": org, "actor": other_actor,
"doc": doc, "other_doc": other_doc})
with engine.connect() as connection:
repository = SqlDocumentRepository(connection)
assert repository.get_document(other_org, doc) is None
assert repository.get_document(org, doc)["id"] == doc
assert repository.get_enabled_administrator(actor)["organization_id"] == org
@pytest.mark.parametrize("ordinal,content,location,vector,tokens", [
(-1, "test", "{}", "[1,2,3]", None),
(0, "", "{}", "[1,2,3]", None),
(0, "test", '{"page":0}', "[1,2,3]", None),
(0, "test", '{"page":1.5}', "[1,2,3]", None),
(0, "test", '{"page":"1"}', "[1,2,3]", None),
(0, "test", "[]", "[1,2,3]", None),
(0, "test", "{}", "[1,2]", None),
(0, "test", "{}", "[1,2,3]", -1),
])
def test_chunk_constraints(migrated_db, ordinal, content, location, vector, tokens):
engine, _ = migrated_db
with engine.begin() as connection:
org, _, doc = seed(connection)
rejected(engine, """
INSERT INTO document_chunks(id,organization_id,document_id,ordinal,text,
location,embedding,token_count)
VALUES (:id,:org,:doc,:ordinal,:content,CAST(:location AS jsonb),
CAST(:vector AS vector),:tokens)
""", {"id": uuid4(), "org": org, "doc": doc, "ordinal": ordinal,
"content": content, "location": location, "vector": vector, "tokens": tokens})
@pytest.mark.parametrize("fields", [
{"action": "unknown"},
{"action": "document_deleted", "target": None, "key": None},
{"action": "ingestion_retry", "actor": None},
{"request_hash": "invalid"},
])
def test_audit_constraints(migrated_db, fields):
engine, _ = migrated_db
with engine.begin() as connection:
org, actor, doc = seed(connection)
params = {"id": uuid4(), "org": org, "actor": actor, "action": "upload",
"target": doc, "key": uuid4(), "request_hash": "a" * 64, **fields}
rejected(engine, """
INSERT INTO audit_events(id,organization_id,actor_user_id,action,target_id,
idempotency_key,request_hash)
VALUES (:id,:org,:actor,:action,:target,:key,:request_hash)
""", params)
def test_global_username_and_original_storage_key_uniqueness(migrated_db):
engine, _ = migrated_db
with engine.begin() as connection:
org, actor, doc = seed(connection)
_, other_actor, other_doc = seed(connection)
username = connection.execute(text("SELECT username FROM users WHERE id=:id"),
{"id": actor}).scalar_one()
storage = connection.execute(text("SELECT storage_key FROM documents WHERE id=:id"),
{"id": doc}).scalar_one()
rejected(engine, "UPDATE users SET username=:name WHERE id=:id",
{"id": other_actor, "name": username})
rejected(engine, "UPDATE documents SET storage_key=:key WHERE id=:id",
{"id": other_doc, "key": storage})