init
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
from uuid import uuid4
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from PIL import Image
|
||||
|
||||
from ocr_server.app import create_app
|
||||
from ocr_server.config import Settings
|
||||
|
||||
|
||||
def client() -> TestClient:
|
||||
directory = TemporaryDirectory()
|
||||
app = create_app(Settings(upload_dir=Path(directory.name)))
|
||||
test_client = TestClient(app)
|
||||
test_client._ocr_test_directory = directory # keep temporary storage alive
|
||||
return test_client
|
||||
|
||||
|
||||
def png_bytes() -> bytes:
|
||||
output = BytesIO()
|
||||
Image.new("RGB", (20, 10), "white").save(output, format="PNG")
|
||||
return output.getvalue()
|
||||
|
||||
|
||||
def test_health() -> None:
|
||||
response = client().get("/healthz")
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {
|
||||
"code": 0,
|
||||
"message": "success",
|
||||
"data": {"status": "ok"},
|
||||
}
|
||||
|
||||
|
||||
def test_upload_and_missing_image() -> None:
|
||||
response = client().post(
|
||||
"/api/v1/upload",
|
||||
files={"file": ("sample.png", png_bytes(), "image/png")},
|
||||
)
|
||||
assert response.status_code == 201
|
||||
payload = response.json()
|
||||
assert payload["code"] == 0
|
||||
assert payload["message"] == "success"
|
||||
assert payload["data"]["filename"] == "sample.png"
|
||||
assert payload["data"]["width"] == 20
|
||||
assert payload["data"]["height"] == 10
|
||||
|
||||
missing = client().post("/api/v1/i2t", json={"file_id": str(uuid4())})
|
||||
assert missing.status_code == 404
|
||||
|
||||
|
||||
def test_rejects_invalid_image() -> None:
|
||||
response = client().post(
|
||||
"/api/v1/upload",
|
||||
files={"file": ("sample.txt", b"not an image", "text/plain")},
|
||||
)
|
||||
assert response.status_code == 400
|
||||
@@ -0,0 +1,45 @@
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
from ocr_server.task_queue import OCRTaskQueue
|
||||
|
||||
|
||||
class FakeOCR:
|
||||
def recognize(self, _: Path) -> list[dict]:
|
||||
return [
|
||||
{"text": "Hello", "confidence": 0.95, "polygon": [[0, 0], [40, 0], [40, 10], [0, 10]]},
|
||||
{"text": "世界", "confidence": 0.85, "polygon": [[45, 0], [85, 0], [85, 10], [45, 10]]},
|
||||
{"text": "下一行", "confidence": 0.92, "polygon": [[0, 20], [50, 20], [50, 30], [0, 30]]},
|
||||
]
|
||||
|
||||
|
||||
def test_build_document_groups_segments_into_lines_and_paragraphs() -> None:
|
||||
document = OCRTaskQueue.build_document(FakeOCR().recognize(Path("sample")))
|
||||
|
||||
assert document.text == "Hello世界\n下一行"
|
||||
assert list(document.polygon) == ["text1", "text2", "text3"]
|
||||
assert document.polygon["text1"].line == 1
|
||||
assert document.polygon["text1"].paragraphs == 1
|
||||
assert document.polygon["text3"].line == 2
|
||||
|
||||
|
||||
def test_filter_document_rebuilds_groups() -> None:
|
||||
document = OCRTaskQueue.build_document(FakeOCR().recognize(Path("sample")))
|
||||
filtered = OCRTaskQueue.filter_document(document, 0.9)
|
||||
|
||||
assert [item.text for item in filtered.polygon.values()] == ["Hello", "下一行"]
|
||||
assert filtered.text == "Hello\n下一行"
|
||||
|
||||
|
||||
def test_queue_completes_task() -> None:
|
||||
queue = OCRTaskQueue(lambda: FakeOCR(), worker_count=1)
|
||||
file_id = uuid4()
|
||||
task = queue.submit(file_id, Path("sample"))
|
||||
queue._queue.join()
|
||||
|
||||
assert task.status == "completed"
|
||||
assert task.progress == 100
|
||||
assert task.ocr_started_at is not None
|
||||
assert task.ocr_finished_at is not None
|
||||
assert task.ocr_elapsed_ms is not None
|
||||
assert queue.snapshot(file_id)[3] is not None
|
||||
Reference in New Issue
Block a user