test_upload_page

This commit is contained in:
John Lancaster
2026-06-28 13:42:05 -05:00
parent bd33e338b3
commit b682e092ea
+29 -41
View File
@@ -1,53 +1,41 @@
"""Tests for transcription.ui.upload_page."""
"""Tests for the upload page route."""
from pathlib import Path
from uuid import uuid4
import pytest
from fastapi.testclient import TestClient
from transcription.services.upload import UploadError, UploadJobResult
from transcription.ui import upload_page
from transcription.app import create_app
from transcription.config import Settings
from transcription.config import _settings
@pytest.mark.unit
class TestUploadPageBehavior:
"""Verify upload page helper and submission behavior."""
@pytest.fixture
def client(tmp_path: Path):
"""Provide a real app client backed by in-memory SQLite."""
settings = Settings(
openrouter_api_key="test-key",
database_url="sqlite:///:memory:",
environment="test",
upload_dir=tmp_path / "uploads",
prompt_dir=tmp_path / "prompts",
)
_settings.set(settings)
def test_accepted_upload_types_contains_supported_extensions(self):
"""accepted_upload_types includes all MVP-supported upload extensions."""
accepted = upload_page.accepted_upload_types()
assert ".jpg" in accepted
assert ".jpeg" in accepted
assert ".png" in accepted
assert ".tif" in accepted
assert ".tiff" in accepted
assert ".pdf" in accepted
app = create_app()
with TestClient(app) as test_client:
yield test_client
def test_submit_upload_calls_upload_service(self, monkeypatch):
"""submit_upload delegates file persistence and job creation to upload service."""
expected = UploadJobResult(
document_id=uuid4(),
job_id=uuid4(),
stored_path=Path("uploads/mock.jpg"),
original_filename="mock.jpg",
)
def fake_create_upload_job(*, filename: str, file_bytes: bytes):
assert filename == "mock.jpg"
assert file_bytes == b"bytes"
return expected
@pytest.mark.integration
class TestPageRendering:
"""Verify the upload page is available and includes the main controls."""
monkeypatch.setattr(upload_page, "create_upload_job", fake_create_upload_job)
def test_upload_page_renders_expected_controls(self, client):
"""GET /ui/upload returns the page shell and upload controls."""
response = client.get("/ui/upload")
result = upload_page.submit_upload(filename="mock.jpg", file_bytes=b"bytes")
assert result == expected
def test_submit_upload_surfaces_upload_error(self, monkeypatch):
"""submit_upload raises UploadError for invalid upload payloads."""
def fake_create_upload_job(*, filename: str, file_bytes: bytes):
raise UploadError("invalid payload")
monkeypatch.setattr(upload_page, "create_upload_job", fake_create_upload_job)
with pytest.raises(UploadError):
upload_page.submit_upload(filename="bad.jpg", file_bytes=b"")
assert response.status_code == 200
assert "Upload Document" in response.text
assert "Select document file" in response.text
assert "View jobs" in response.text