Introduce an admin portal (React + Nginx), WebSocket routing, and API versioning middleware with `/api/v1/` prefix deprecation. Add master API key authentication, new Prometheus metrics for AI token consumption and active WebSockets, and extend S3 config with a public endpoint URL. Update test paths and fixtures to align with the new routing structure.
49 lines
1.9 KiB
Python
49 lines
1.9 KiB
Python
import pytest
|
|
from unittest.mock import patch, MagicMock
|
|
from app.services.ocr_service import extract_text, _detect_language
|
|
|
|
def test_detect_language():
|
|
assert _detect_language("C'est une phrase en français.") == "fr"
|
|
assert _detect_language("This is a sentence in English.") == "en"
|
|
assert _detect_language("") == "unknown"
|
|
assert _detect_language("123456789") == "unknown"
|
|
|
|
@pytest.mark.asyncio
|
|
@patch("app.services.ocr_service.pytesseract")
|
|
@patch("app.services.ocr_service.PILImage")
|
|
@patch("app.services.ocr_service.Path.exists", return_value=True)
|
|
@patch("app.services.ocr_service.settings")
|
|
@patch("app.services.ocr_service.get_storage_backend")
|
|
async def test_extract_text_success(mock_get_storage_backend, mock_settings, mock_path_exists, mock_pil, mock_tesseract):
|
|
from unittest.mock import AsyncMock
|
|
mock_backend = MagicMock()
|
|
mock_backend.get_bytes = AsyncMock(return_value=b"fake")
|
|
mock_get_storage_backend.return_value = mock_backend
|
|
mock_settings.OCR_ENABLED = True
|
|
mock_settings.OCR_LANGUAGES = "fra+eng"
|
|
mock_settings.TESSERACT_CMD = None
|
|
|
|
# Mock image_to_data
|
|
mock_tesseract.image_to_data.return_value = {
|
|
"conf": ["90", "80", "-1", "70"]
|
|
}
|
|
mock_tesseract.Output.DICT = "dict"
|
|
|
|
# Mock image_to_string
|
|
mock_tesseract.image_to_string.return_value = "This is a test OCR output."
|
|
|
|
result = await extract_text("fake/path.jpg")
|
|
|
|
assert result["has_text"] is True
|
|
assert result["text"] == "This is a test OCR output."
|
|
assert result["confidence"] == 0.8 # (90+80+70)/3 / 100 = 0.8
|
|
assert result["language"] == "en"
|
|
|
|
@pytest.mark.asyncio
|
|
@patch("app.services.ocr_service.settings")
|
|
async def test_extract_text_disabled(mock_settings):
|
|
mock_settings.OCR_ENABLED = False
|
|
result = await extract_text("any/path.jpg")
|
|
assert result["has_text"] is False
|
|
assert result["text"] is None
|