Fix lint ans test errors

This commit is contained in:
Pier-Jean Malandrino 2026-03-31 16:35:48 +02:00
parent fe4e792885
commit a63af61b73
5 changed files with 29 additions and 21 deletions

View file

@ -6,9 +6,10 @@ Infrastructure adapters (local Docling, Docling Serve, etc.) implement these.
from __future__ import annotations from __future__ import annotations
from typing import Protocol from typing import TYPE_CHECKING, Protocol
from domain.value_objects import ConversionOptions, ConversionResult if TYPE_CHECKING:
from domain.value_objects import ConversionOptions, ConversionResult
class DocumentConverter(Protocol): class DocumentConverter(Protocol):

View file

@ -214,16 +214,17 @@ def _extract_bbox(bbox_data: dict, page_height: float) -> list[float]:
if not isinstance(bbox_data, dict): if not isinstance(bbox_data, dict):
return [0.0, 0.0, 0.0, 0.0] return [0.0, 0.0, 0.0, 0.0]
l = bbox_data.get("l", 0.0) left = bbox_data.get("l", 0.0)
t = bbox_data.get("t", 0.0) top = bbox_data.get("t", 0.0)
r = bbox_data.get("r", 0.0) right = bbox_data.get("r", 0.0)
b = bbox_data.get("b", 0.0) bottom = bbox_data.get("b", 0.0)
coord_origin = bbox_data.get("coord_origin", "TOPLEFT") coord_origin = bbox_data.get("coord_origin", "TOPLEFT")
if coord_origin == "BOTTOMLEFT": if coord_origin == "BOTTOMLEFT":
# Convert: top = page_height - old_top, bottom = page_height - old_bottom # In BOTTOMLEFT: top has higher y, bottom has lower y
new_t = page_height - b # In TOPLEFT: flip both — new_top = page_height - old_top
new_b = page_height - t new_top = page_height - top
t, b = new_t, new_b new_bottom = page_height - bottom
top, bottom = new_top, new_bottom
return [l, t, r, b] return [left, top, right, bottom]

View file

@ -10,10 +10,13 @@ import asyncio
import json import json
import logging import logging
from dataclasses import asdict from dataclasses import asdict
from typing import TYPE_CHECKING
from domain.models import AnalysisJob from domain.models import AnalysisJob
from domain.ports import DocumentConverter
from domain.value_objects import ConversionOptions, ConversionResult from domain.value_objects import ConversionOptions, ConversionResult
if TYPE_CHECKING:
from domain.ports import DocumentConverter
from persistence import analysis_repo, document_repo from persistence import analysis_repo, document_repo
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

View file

@ -14,6 +14,8 @@ from docling.datamodel.pipeline_options import (
from domain.value_objects import ConversionOptions from domain.value_objects import ConversionOptions
from infra.local_converter import ( from infra.local_converter import (
_build_docling_converter as build_converter, _build_docling_converter as build_converter,
)
from infra.local_converter import (
_convert_sync as convert_document, _convert_sync as convert_document,
) )
@ -457,13 +459,15 @@ class TestAnalysisEndpointPipelineOptions:
@pytest.fixture @pytest.fixture
def client(self): def client(self):
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from main import app from main import app
return TestClient(app, raise_server_exceptions=False) return TestClient(app, raise_server_exceptions=False)
@pytest.fixture @pytest.fixture
def mock_svc(self, client): def mock_svc(self, client):
from main import app
from unittest.mock import MagicMock from unittest.mock import MagicMock
from main import app
mock = MagicMock() mock = MagicMock()
original = getattr(app.state, "analysis_service", None) original = getattr(app.state, "analysis_service", None)
app.state.analysis_service = mock app.state.analysis_service = mock

View file

@ -8,16 +8,14 @@ from unittest.mock import AsyncMock, MagicMock, patch
import httpx import httpx
import pytest import pytest
from domain.value_objects import ConversionOptions, ConversionResult, PageDetail, PageElement from domain.value_objects import ConversionOptions, ConversionResult
from infra.serve_converter import ( from infra.serve_converter import (
ServeConverter, ServeConverter,
_build_form_data, _build_form_data,
_extract_bbox, _extract_bbox,
_extract_pages_from_docling_document,
_parse_response, _parse_response,
) )
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Unit tests — form data building # Unit tests — form data building
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
@ -182,8 +180,9 @@ class TestExtractBbox:
assert bbox == [10, 20, 100, 50] assert bbox == [10, 20, 100, 50]
def test_bottomleft_conversion(self): def test_bottomleft_conversion(self):
bbox = _extract_bbox({"l": 10, "t": 742, "r": 100, "b": 772, "coord_origin": "BOTTOMLEFT"}, 792.0) # In BOTTOMLEFT: t (top of box) has higher y than b (bottom of box)
# new_t = 792 - 772 = 20, new_b = 792 - 742 = 50 bbox = _extract_bbox({"l": 10, "t": 772, "r": 100, "b": 742, "coord_origin": "BOTTOMLEFT"}, 792.0)
# new_top = 792 - 772 = 20, new_bottom = 792 - 742 = 50
assert bbox == [10, 20, 100, 50] assert bbox == [10, 20, 100, 50]
def test_missing_coord_origin_defaults_topleft(self): def test_missing_coord_origin_defaults_topleft(self):
@ -308,9 +307,9 @@ class TestServeConverterConvert:
conv = ServeConverter(base_url="http://localhost:5001") conv = ServeConverter(base_url="http://localhost:5001")
with patch("infra.serve_converter.httpx.AsyncClient", return_value=mock_client): with patch("infra.serve_converter.httpx.AsyncClient", return_value=mock_client), \
with pytest.raises(httpx.HTTPStatusError): pytest.raises(httpx.HTTPStatusError):
await conv.convert(str(test_file), ConversionOptions()) await conv.convert(str(test_file), ConversionOptions())
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_health_check_success(self): async def test_health_check_success(self):