Files
unredact/tests/test_app.py
T
Alex-GilbertandClaude Opus 4.6 c95b6cb9d7 feat: FastAPI backend with upload, overlay, and data endpoints
Adds POST /api/upload (runs full pipeline: rasterize, OCR, font detect,
overlay), GET endpoints for original/overlay images and OCR data JSON.
Also adds python-multipart, anyio, and pytest-anyio dependencies.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
2026-02-15 18:53:48 -06:00

80 lines
2.5 KiB
Python

import io
from pathlib import Path
import pytest
from httpx import ASGITransport, AsyncClient
from unredact.app import app
@pytest.fixture
def pdf_bytes(sample_pdf: Path) -> bytes:
return sample_pdf.read_bytes()
@pytest.mark.anyio
async def test_upload_pdf(pdf_bytes: bytes):
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as client:
resp = await client.post(
"/api/upload",
files={"file": ("test.pdf", pdf_bytes, "application/pdf")},
)
assert resp.status_code == 200
data = resp.json()
assert "doc_id" in data
assert data["page_count"] > 0
@pytest.mark.anyio
async def test_get_page_overlay(pdf_bytes: bytes):
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as client:
# Upload first
resp = await client.post(
"/api/upload",
files={"file": ("test.pdf", pdf_bytes, "application/pdf")},
)
doc_id = resp.json()["doc_id"]
# Get page 1 overlay
resp = await client.get(f"/api/doc/{doc_id}/page/1/overlay")
assert resp.status_code == 200
assert resp.headers["content-type"] == "image/png"
assert len(resp.content) > 1000 # Should be a real image
@pytest.mark.anyio
async def test_get_page_original(pdf_bytes: bytes):
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as client:
resp = await client.post(
"/api/upload",
files={"file": ("test.pdf", pdf_bytes, "application/pdf")},
)
doc_id = resp.json()["doc_id"]
resp = await client.get(f"/api/doc/{doc_id}/page/1/original")
assert resp.status_code == 200
assert resp.headers["content-type"] == "image/png"
@pytest.mark.anyio
async def test_get_page_data(pdf_bytes: bytes):
"""Should return OCR data and font info as JSON."""
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as client:
resp = await client.post(
"/api/upload",
files={"file": ("test.pdf", pdf_bytes, "application/pdf")},
)
doc_id = resp.json()["doc_id"]
resp = await client.get(f"/api/doc/{doc_id}/page/1/data")
assert resp.status_code == 200
data = resp.json()
assert "font" in data
assert "lines" in data
assert data["font"]["name"]
assert data["font"]["size"] > 0