backend/tests/test_bulk.py

90 lines
3.3 KiB
Python

"""Batch upload prep + bulk annotation import (COCO/YOLO path)."""
import pytest
import pytest_asyncio
from sqlalchemy import text
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from shared.schemas import AnnotationUpsert
from src.services import annotations as ann_svc
from src.services import images as img_svc
@pytest_asyncio.fixture
async def db():
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
from src.core.db import Base
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
yield async_sessionmaker(engine, expire_on_commit=False)
await engine.dispose()
async def _seed(db):
async with db() as s:
await s.execute(text(
"INSERT INTO projects (id, slug, name, task_type, status) "
"VALUES ('p1', 'p1', 'P', 'object_detection', 'annotating')"))
await s.execute(text(
"INSERT INTO images (id, project_id, file_name, r2_key, status, flags) "
"VALUES ('img1', 'p1', 'a.jpg', 'k1', 'in_progress', '{}'),"
"('img2', 'p1', 'b.jpg', 'k2', 'in_progress', '{}')"))
await s.commit()
@pytest.fixture(autouse=True)
def fake_storage(monkeypatch):
monkeypatch.setattr(img_svc.r2, "presign_put",
lambda key, content_type: f"http://storage.test/{key}")
def _body(n=1):
from shared.schemas import AnnotationShape
return AnnotationUpsert(shapes=[
AnnotationShape(id=f"s{i}", index=i + 1, type="bbox", labelId="car",
bbox={"x": 1, "y": 2, "width": 10, "height": 10})
for i in range(n)])
@pytest.mark.asyncio
async def test_bulk_upsert_saves_and_reports_missing(db):
await _seed(db)
async with db() as s:
out = await ann_svc.bulk_upsert("p1", {"img1": _body(2), "ghost": _body(1)}, s)
assert out == {"saved": 1, "missing": ["ghost"]}
n = (await s.execute(text("SELECT object_count FROM images WHERE id='img1'"))).scalar()
assert n == 2
got = await ann_svc.get_annotation("p1", "img1", s)
assert len(got["shapes"]) == 2
st = (await s.execute(text("SELECT status FROM images WHERE id='img1'"))).scalar()
assert st == "pending_review"
@pytest.mark.asyncio
async def test_bulk_upsert_unknown_project_404(db):
from fastapi import HTTPException
await _seed(db)
async with db() as s:
with pytest.raises(HTTPException) as e:
await ann_svc.bulk_upsert("nope", {"img1": _body()}, s)
assert e.value.status_code == 404
@pytest.mark.asyncio
async def test_batch_presign_unique_keys_and_cap(db):
await _seed(db)
async with db() as s:
files = [{"fileName": "same.jpg", "contentType": "image/jpeg"} for _ in range(3)]
out = await img_svc.batch_presign("p1", files, s)
keys = [o["r2Key"] for o in out]
assert len(set(keys)) == 3 and all("same.jpg" in k for k in keys)
assert all(o["putUrl"] for o in out)
@pytest.mark.asyncio
async def test_batch_presign_unknown_project_404(db):
from fastapi import HTTPException
await _seed(db)
async with db() as s:
with pytest.raises(HTTPException) as e:
await img_svc.batch_presign("nope", [{"fileName": "a.jpg"}], s)
assert e.value.status_code == 404