Spaces:
Sleeping

card-ocr / test_main.py
sagardeswal's picture
Claude Opus 4.7 (1M context)
feat(card-ocr): PaddleOCR sidecar with Railway-ready config
6f32872
Raw History Blame Contribute Delete
3.3 kB
"""Smoke tests for the PaddleOCR sidecar.
Avoid invoking the real PaddleOCR singleton in CI -- monkeypatch
``_get_ocr`` so the unit tests run in a few hundred ms without
loading a 800MB model. Integration coverage with real PaddleOCR
lives behind the ``CARD_OCR_INTEGRATION=1`` env var so the developer
can run it locally on demand.
"""
from __future__ import annotations
import io
import os
import pytest
from fastapi.testclient import TestClient
from PIL import Image
@pytest.fixture
def client(monkeypatch):
# Ensure auth check passes without needing the real key in CI.
monkeypatch.setenv("OCR_INTERNAL_API_KEY", "test-key")
# Reload main so it picks up the env var.
import importlib
import main as ocr_main
importlib.reload(ocr_main)
# Stub the OCR singleton so tests don't need PaddlePaddle installed.
class _FakeOCR:
def ocr(self, _arr, cls=True): # noqa: ARG002
return [[
[[[0, 0], [0, 0], [0, 0], [0, 0]], ("Aanya Patel", 0.97)],
[[[0, 0], [0, 0], [0, 0], [0, 0]], ("aanya@sunrise.example", 0.92)],
]]
monkeypatch.setattr(ocr_main, "_ocr_singleton", _FakeOCR())
return TestClient(ocr_main.app)
def _png_bytes() -> bytes:
img = Image.new("RGB", (50, 30), (255, 255, 255))
buf = io.BytesIO()
img.save(buf, format="PNG")
return buf.getvalue()
def test_health_200(client):
res = client.get("/health")
assert res.status_code == 200
assert res.json() == {"ok": True}
def test_extract_requires_internal_key(client):
res = client.post(
"/extract",
files={"front_image": ("f.png", _png_bytes(), "image/png")},
)
assert res.status_code == 401
def test_extract_rejects_wrong_key(client):
res = client.post(
"/extract",
files={"front_image": ("f.png", _png_bytes(), "image/png")},
headers={"X-Internal-API-Key": "wrong"},
)
assert res.status_code == 401
def test_extract_returns_text_and_confidence(client):
res = client.post(
"/extract",
files={
"front_image": ("f.png", _png_bytes(), "image/png"),
"back_image": ("b.png", _png_bytes(), "image/png"),
},
headers={"X-Internal-API-Key": "test-key"},
)
assert res.status_code == 200, res.text
body = res.json()
assert "Aanya Patel" in body["front_text"]
assert "aanya@sunrise.example" in body["front_text"]
assert body["confidence"]["front"] > 0
# New per-block envelope -- enables per-field confidence aggregation
# in the Django side.
assert "blocks" in body
front_blocks = body["blocks"]["front"]
assert any(b["text"] == "Aanya Patel" for b in front_blocks)
assert all("confidence" in b for b in front_blocks)
def test_extract_rejects_empty_request(client):
res = client.post(
"/extract",
headers={"X-Internal-API-Key": "test-key"},
)
# FastAPI may return 400 (no-images-provided) or 422 (validation).
assert res.status_code in (400, 422)
def test_extract_rejects_invalid_image(client):
res = client.post(
"/extract",
files={"front_image": ("f.png", b"not-an-image", "image/png")},
headers={"X-Internal-API-Key": "test-key"},
)
assert res.status_code == 400