9f97f3abbe
CI - Python Bindings / sdist (push) Failing after 1s
CI - Python Bindings / Build x86_64-unknown-linux-musl (push) Failing after 1s
CI / fmt (push) Failing after 1s
E2E Output Validation / compare-outputs (push) Failing after 1s
Sync Docs to Developer Hub / sync-docs (push) Failing after 1s
CI - Python Bindings / Build x86_64-unknown-linux-gnu (push) Failing after 1s
CI - WASM Bindings / Build WASM (push) Failing after 0s
CI / clippy (push) Failing after 1s
CI / build-and-test (ubuntu-latest) (push) Failing after 0s
CI - WASM Bindings / Edge runtime PDF parse test (push) Has been skipped
CI - WASM Bindings / Browser PDF parse test (push) Has been skipped
Deploy Demo to GitHub Pages / deploy (push) Failing after 1s
CI / build-docker-image (push) Failing after 3s
CI - Node Bindings / Build darwin-x64 (push) Has been cancelled
CI - Node Bindings / Test win32-x64-msvc (push) Has been cancelled
CI - Node Bindings / Build linux-arm64-gnu (push) Has been cancelled
CI - Node Bindings / Build linux-x64-gnu (push) Has been cancelled
CI - Node Bindings / Build linux-x64-musl (push) Has been cancelled
CI - Node Bindings / Build win32-arm64-msvc (push) Has been cancelled
CI - Node Bindings / Build win32-x64-msvc (push) Has been cancelled
CI - Node Bindings / Test darwin-arm64 (push) Has been cancelled
CI - Node Bindings / Test darwin-x64 (push) Has been cancelled
CI - Node Bindings / Test linux-x64-gnu (push) Has been cancelled
CI - Node Bindings / Test linux-x64-musl (push) Has been cancelled
CI - Node Bindings / Test win32-arm64-msvc (push) Has been cancelled
CI - Python Bindings / Build aarch64-pc-windows-msvc (push) Has been cancelled
CI - Python Bindings / Build x86_64-pc-windows-msvc (push) Has been cancelled
CI - Python Bindings / Build x86_64-apple-darwin (push) Has been cancelled
CI - Python Bindings / Build aarch64-apple-darwin (push) Has been cancelled
CI - Python Bindings / Build aarch64-unknown-linux-gnu (push) Has been cancelled
CI - Node Bindings / Build darwin-arm64 (push) Has been cancelled
CI - Python Bindings / Test x86_64-apple-darwin (push) Has been cancelled
CI - Python Bindings / Test aarch64-apple-darwin (push) Has been cancelled
CI - Python Bindings / Test x86_64-unknown-linux-gnu (push) Has been cancelled
CI - Python Bindings / Test x86_64-unknown-linux-musl (push) Has been cancelled
CI - Python Bindings / Test aarch64-pc-windows-msvc (push) Has been cancelled
CI - Python Bindings / Test x86_64-pc-windows-msvc (push) Has been cancelled
CI / build-and-test (macos-26-intel) (push) Has been cancelled
CI / build-and-test (macos-latest) (push) Has been cancelled
CI / build-and-test (windows-11-arm) (push) Has been cancelled
CI / build-and-test (windows-latest) (push) Has been cancelled
E2E Output Validation / upload-dataset (push) Has been cancelled
66 lines
2.0 KiB
Python
66 lines
2.0 KiB
Python
import io
|
|
from typing import Any
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
from PIL import Image
|
|
|
|
from server import EasyOCRServer
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def server() -> EasyOCRServer:
|
|
return EasyOCRServer()
|
|
|
|
|
|
class MockEasyOCRReader:
|
|
def __init__(self, *args, **kwargs) -> None:
|
|
self.results = [
|
|
([[10, 20], [200, 40]], "Hello World", 0.98),
|
|
([[10, 50], [250, 70]], "Total: $42.00", 0.95),
|
|
([[10, 80], [180, 100]], "Thank you!", 0.87),
|
|
]
|
|
self.transformed_results = [
|
|
{"text": "Hello World", "bbox": [10, 20, 200, 40], "confidence": 0.98},
|
|
{"text": "Total: $42.00", "bbox": [10, 50, 250, 70], "confidence": 0.95},
|
|
{"text": "Thank you!", "bbox": [10, 80, 180, 100], "confidence": 0.87},
|
|
]
|
|
|
|
def readtext(self, *args, **kwargs) -> list[Any]:
|
|
return self.results
|
|
|
|
|
|
def test_server_init(server: EasyOCRServer) -> None:
|
|
assert server.current_language is None
|
|
assert server.reader is None
|
|
|
|
|
|
def test_server_health_endpoint(server: EasyOCRServer) -> None:
|
|
app = server._create_ocr_server()
|
|
client = TestClient(app)
|
|
response = client.get("/health")
|
|
assert response.status_code == 200
|
|
assert response.json() == {"status": "healthy"}
|
|
|
|
|
|
def test_server_ocr_endpoint(server: EasyOCRServer) -> None:
|
|
image = Image.new("RGB", (1, 1), color=(255, 255, 255))
|
|
|
|
# Save to bytes (to simulate a file upload)
|
|
buffer = io.BytesIO()
|
|
image.save(buffer, format="PNG")
|
|
buffer.seek(0)
|
|
app = server._create_ocr_server()
|
|
mock_ocr = MockEasyOCRReader()
|
|
server.reader = mock_ocr # type: ignore
|
|
server.current_language = "en"
|
|
client = TestClient(app)
|
|
|
|
response = client.post(
|
|
"/ocr",
|
|
files={"file": ("test.png", buffer, "image/png")},
|
|
data={"language": "en"},
|
|
)
|
|
assert response.status_code == 200
|
|
assert response.json().get("results", []) == mock_ocr.transformed_results
|