generated from john/python-template
test_upload_page
This commit is contained in:
@@ -1,53 +1,41 @@
|
|||||||
"""Tests for transcription.ui.upload_page."""
|
"""Tests for the upload page route."""
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from uuid import uuid4
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
from transcription.services.upload import UploadError, UploadJobResult
|
from transcription.app import create_app
|
||||||
from transcription.ui import upload_page
|
from transcription.config import Settings
|
||||||
|
from transcription.config import _settings
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.unit
|
@pytest.fixture
|
||||||
class TestUploadPageBehavior:
|
def client(tmp_path: Path):
|
||||||
"""Verify upload page helper and submission behavior."""
|
"""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):
|
app = create_app()
|
||||||
"""accepted_upload_types includes all MVP-supported upload extensions."""
|
with TestClient(app) as test_client:
|
||||||
accepted = upload_page.accepted_upload_types()
|
yield test_client
|
||||||
assert ".jpg" in accepted
|
|
||||||
assert ".jpeg" in accepted
|
|
||||||
assert ".png" in accepted
|
|
||||||
assert ".tif" in accepted
|
|
||||||
assert ".tiff" in accepted
|
|
||||||
assert ".pdf" in accepted
|
|
||||||
|
|
||||||
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):
|
@pytest.mark.integration
|
||||||
assert filename == "mock.jpg"
|
class TestPageRendering:
|
||||||
assert file_bytes == b"bytes"
|
"""Verify the upload page is available and includes the main controls."""
|
||||||
return expected
|
|
||||||
|
|
||||||
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 response.status_code == 200
|
||||||
assert result == expected
|
assert "Upload Document" in response.text
|
||||||
|
assert "Select document file" in response.text
|
||||||
def test_submit_upload_surfaces_upload_error(self, monkeypatch):
|
assert "View jobs" in response.text
|
||||||
"""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"")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user