fix: ruff lint + format fixes in tests, ESLint fixes in frontend
This commit is contained in:
+28
-14
@@ -2,7 +2,6 @@
|
||||
|
||||
import io
|
||||
import uuid
|
||||
from datetime import date
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
@@ -11,15 +10,14 @@ from httpx import ASGITransport, AsyncClient
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.database import Base, get_db
|
||||
from app.database import get_db
|
||||
from app.main import app
|
||||
from app.models.ocr_result import OCRResult, OCRStatus
|
||||
from app.models.vehicle import Vehicle
|
||||
from app.services import ocr_service
|
||||
from app.services.ocr_service import CONFIDENCE_THRESHOLD
|
||||
|
||||
# Ensure all models are registered with Base.metadata
|
||||
from app.models import user, vehicle, ocr_result # noqa: F401
|
||||
from app.models import user, vehicle as vehicle_model, ocr_result # noqa: F401
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
@@ -227,9 +225,7 @@ class TestOCRService:
|
||||
assert result.structured_data["vin"] == "WDB9066351L123456"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_process_ocr_low_confidence(
|
||||
self, db_session: AsyncSession, tmp_path
|
||||
):
|
||||
async def test_process_ocr_low_confidence(self, db_session: AsyncSession, tmp_path):
|
||||
"""Test OCR processing with low confidence sets status to manual_review."""
|
||||
with patch.object(ocr_service.settings, "UPLOAD_DIR", str(tmp_path)):
|
||||
ocr_result = await ocr_service.upload_file(
|
||||
@@ -359,6 +355,7 @@ class TestOCRRouter:
|
||||
@pytest_asyncio.fixture
|
||||
async def ocr_client(self, test_session_factory, admin_token):
|
||||
"""HTTP client with DB override and admin auth."""
|
||||
|
||||
async def _override_get_db():
|
||||
async with test_session_factory() as session:
|
||||
try:
|
||||
@@ -382,10 +379,12 @@ class TestOCRRouter:
|
||||
"""POST /api/v1/ocr/upload with valid image returns 202."""
|
||||
with patch.object(ocr_service.settings, "UPLOAD_DIR", str(tmp_path)):
|
||||
# Patch background task to avoid actual processing
|
||||
with patch("app.routers.ocr.run_ocr_processing") as mock_task:
|
||||
with patch("app.routers.ocr.run_ocr_processing"):
|
||||
response = await ocr_client.post(
|
||||
"/api/v1/ocr/upload",
|
||||
files={"file": ("scan.png", io.BytesIO(b"fake-image"), "image/png")},
|
||||
files={
|
||||
"file": ("scan.png", io.BytesIO(b"fake-image"), "image/png")
|
||||
},
|
||||
)
|
||||
assert response.status_code == 202
|
||||
data = response.json()
|
||||
@@ -418,7 +417,9 @@ class TestOCRRouter:
|
||||
with patch("app.routers.ocr.run_ocr_processing"):
|
||||
upload_resp = await ocr_client.post(
|
||||
"/api/v1/ocr/upload",
|
||||
files={"file": ("scan.png", io.BytesIO(b"fake-image"), "image/png")},
|
||||
files={
|
||||
"file": ("scan.png", io.BytesIO(b"fake-image"), "image/png")
|
||||
},
|
||||
)
|
||||
result_id = upload_resp.json()["ocr_result_id"]
|
||||
|
||||
@@ -443,7 +444,13 @@ class TestOCRRouter:
|
||||
for i in range(3):
|
||||
await ocr_client.post(
|
||||
"/api/v1/ocr/upload",
|
||||
files={"file": (f"scan{i}.png", io.BytesIO(b"fake-image"), "image/png")},
|
||||
files={
|
||||
"file": (
|
||||
f"scan{i}.png",
|
||||
io.BytesIO(b"fake-image"),
|
||||
"image/png",
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
response = await ocr_client.get("/api/v1/ocr/results")
|
||||
@@ -485,7 +492,9 @@ class TestOCRRouter:
|
||||
with patch("app.routers.ocr.run_ocr_processing"):
|
||||
upload_resp = await ocr_client.post(
|
||||
"/api/v1/ocr/upload",
|
||||
files={"file": ("scan.png", io.BytesIO(b"fake-image"), "image/png")},
|
||||
files={
|
||||
"file": ("scan.png", io.BytesIO(b"fake-image"), "image/png")
|
||||
},
|
||||
data={"vehicle_id": str(test_vehicle.id)},
|
||||
)
|
||||
result_id = upload_resp.json()["ocr_result_id"]
|
||||
@@ -493,8 +502,11 @@ class TestOCRRouter:
|
||||
# Manually set structured data via direct DB session
|
||||
from tests.conftest import TEST_DATABASE_URL
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
|
||||
|
||||
engine = create_async_engine(TEST_DATABASE_URL)
|
||||
factory = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
||||
factory = async_sessionmaker(
|
||||
engine, class_=AsyncSession, expire_on_commit=False
|
||||
)
|
||||
async with factory() as session:
|
||||
stmt = select(OCRResult).where(OCRResult.id == uuid.UUID(result_id))
|
||||
res = await session.execute(stmt)
|
||||
@@ -527,7 +539,9 @@ class TestOpenRouterClient:
|
||||
"""Test parsing a valid JSON response."""
|
||||
from app.utils.openrouter import _parse_response
|
||||
|
||||
raw = '{"brand": "BMW", "model": "X5", "vin": "ABC123", "confidence_score": 0.9}'
|
||||
raw = (
|
||||
'{"brand": "BMW", "model": "X5", "vin": "ABC123", "confidence_score": 0.9}'
|
||||
)
|
||||
result = _parse_response(raw)
|
||||
assert result["structured_data"]["brand"] == "BMW"
|
||||
assert result["confidence_score"] == 0.9
|
||||
|
||||
Reference in New Issue
Block a user