generated from john/python-template
This commit is contained in:
@@ -10,9 +10,11 @@ from transcription.db.models import Document
|
||||
from transcription.db.models import Job
|
||||
from transcription.db.models import JobSource
|
||||
from transcription.db.models import Source
|
||||
from transcription.errors import ErrorCategory
|
||||
from transcription.services.people import store_person_portrait
|
||||
from transcription.services.sources import source_mime_type
|
||||
from transcription.services.store import SourceStorageError
|
||||
from transcription.services.store import StoredSourceFile
|
||||
from transcription.services.store import create_document_job
|
||||
from transcription.services.store import create_job_for_document
|
||||
from transcription.services.store import store_source_file
|
||||
@@ -152,3 +154,72 @@ async def test_source_storage_rejects_unsupported_format(tmp_path):
|
||||
|
||||
with pytest.raises(SourceStorageError):
|
||||
await store_source_file(filename="page.txt", file_bytes=b"text", settings=settings)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_document_job_db_failure_maps_to_internal_category(tmp_path, monkeypatch):
|
||||
settings = Settings(openrouter_api_key="test-key", upload_dir=tmp_path)
|
||||
stored_path = tmp_path / "documents" / "stored-file.pdf"
|
||||
stored_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
stored_path.write_bytes(b"payload")
|
||||
|
||||
async def _fake_store_source_file(**_kwargs) -> StoredSourceFile:
|
||||
return StoredSourceFile(
|
||||
path=stored_path,
|
||||
file_hash="f" * 64,
|
||||
file_size_bytes=7,
|
||||
)
|
||||
|
||||
async def _boom_create_records(**_kwargs):
|
||||
raise RuntimeError("database unavailable")
|
||||
|
||||
monkeypatch.setattr("transcription.services.store.store_source_file", _fake_store_source_file)
|
||||
monkeypatch.setattr("transcription.services.store._create_document_job_records", _boom_create_records)
|
||||
|
||||
with pytest.raises(SourceStorageError) as exc_info:
|
||||
await create_document_job(
|
||||
filename="single-page.pdf",
|
||||
file_bytes=b"payload",
|
||||
settings=settings,
|
||||
)
|
||||
|
||||
assert exc_info.value.category == ErrorCategory.INFRA_PERSISTENT
|
||||
assert exc_info.value.retriable is False
|
||||
assert not stored_path.exists()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_job_for_document_db_failure_maps_to_internal_category(async_session, tmp_path, monkeypatch):
|
||||
document = Document(id=uuid4(), name="db-failure-doc")
|
||||
async_session.add(document)
|
||||
await async_session.commit()
|
||||
|
||||
settings = Settings(openrouter_api_key="test-key", upload_dir=tmp_path)
|
||||
stored_path = tmp_path / "documents" / str(document.id) / "stored-file.pdf"
|
||||
stored_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
stored_path.write_bytes(b"payload")
|
||||
|
||||
async def _fake_store_source_file(**_kwargs) -> StoredSourceFile:
|
||||
return StoredSourceFile(
|
||||
path=stored_path,
|
||||
file_hash="f" * 64,
|
||||
file_size_bytes=7,
|
||||
)
|
||||
|
||||
async def _boom_create_records(**_kwargs):
|
||||
raise RuntimeError("database unavailable")
|
||||
|
||||
monkeypatch.setattr("transcription.services.store.store_source_file", _fake_store_source_file)
|
||||
monkeypatch.setattr("transcription.services.store._create_job_for_document_records", _boom_create_records)
|
||||
|
||||
with pytest.raises(SourceStorageError) as exc_info:
|
||||
await create_job_for_document(
|
||||
document_id=document.id,
|
||||
source_files=[("single-page.pdf", b"payload")],
|
||||
session=async_session,
|
||||
settings=settings,
|
||||
)
|
||||
|
||||
assert exc_info.value.category == ErrorCategory.INFRA_PERSISTENT
|
||||
assert exc_info.value.retriable is False
|
||||
assert not stored_path.exists()
|
||||
|
||||
Reference in New Issue
Block a user