refactor: move services to a unit of work over repository ports
Services depend only on src/core/ports: async with self.uow is one transaction, commit is explicit, leaving without it rolls back. A connection is held per step, the JSON routers keep a shared request deadline, and rollback runs under the block deadline. Adds service-tests with fakes, a repo contract run on both the fake and SQL, and make service_test / service_test_db. API, schema and settings are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,450 @@
|
||||
"""`AttachmentService`: порядок проверок и записи, компенсация, чтение."""
|
||||
|
||||
import asyncio
|
||||
from collections.abc import AsyncIterator
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
import pytest
|
||||
|
||||
from builders import NOW, attachment_dto, chat_dto, user_dto, variant_dto
|
||||
from fakes import FakeCache, FakeDatabase, FakeObjectStorage, FakeUnitOfWork
|
||||
from src.core import exceptions
|
||||
from src.core.services import AttachmentService
|
||||
from src.infra.upload_slots import UploadSlots
|
||||
from src.schemas.enums import (
|
||||
AttachmentErrorCode,
|
||||
AttachmentJobState,
|
||||
AttachmentStatus,
|
||||
AttachmentVariantName,
|
||||
)
|
||||
from src.utils.constants import ATTACHMENT_MAX_BYTES
|
||||
|
||||
pytestmark = pytest.mark.anyio
|
||||
|
||||
SAMPLE = (
|
||||
Path(__file__).resolve().parents[2] / "tests" / "fixtures" / "sample.png"
|
||||
).read_bytes()
|
||||
UNAVAILABLE = exceptions.ServiceUnavailable
|
||||
|
||||
|
||||
def _slots(*, max_concurrent: int = 1, max_waiting: int = 0) -> UploadSlots:
|
||||
return UploadSlots(
|
||||
max_concurrent=max_concurrent,
|
||||
max_waiting=max_waiting,
|
||||
wait_timeout_seconds=0.01,
|
||||
)
|
||||
|
||||
|
||||
async def _body(*chunks: bytes) -> AsyncIterator[bytes]:
|
||||
for chunk in chunks:
|
||||
yield chunk
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def service(
|
||||
uow: FakeUnitOfWork,
|
||||
cache: FakeCache,
|
||||
storage: FakeObjectStorage,
|
||||
) -> AttachmentService:
|
||||
return AttachmentService(uow, cache, storage, _slots())
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def world(
|
||||
uow: FakeUnitOfWork,
|
||||
database: FakeDatabase,
|
||||
) -> tuple[UUID, UUID, UUID]:
|
||||
alice = user_dto()
|
||||
bob = user_dto()
|
||||
chat = chat_dto(member_ids=(alice.id,))
|
||||
async with uow:
|
||||
await uow.users.insert_user(user=alice, password_hash="h")
|
||||
await uow.users.insert_user(user=bob, password_hash="h")
|
||||
await uow.chats.insert_chat(chat=chat)
|
||||
await uow.chats.insert_chat_members(chat=chat)
|
||||
await uow.commit()
|
||||
database.events.clear()
|
||||
return alice.id, bob.id, chat.id
|
||||
|
||||
|
||||
async def _upload(
|
||||
service: AttachmentService,
|
||||
world: tuple[UUID, UUID, UUID],
|
||||
*,
|
||||
owner: UUID | None = None,
|
||||
media_type: str | None = "image/png",
|
||||
content_length: int | None = None,
|
||||
body: AsyncIterator[bytes] | None = None,
|
||||
):
|
||||
alice, _, chat_id = world
|
||||
return await service.upload(
|
||||
chat_id=chat_id,
|
||||
owner_id=owner or alice,
|
||||
filename="a.png",
|
||||
media_type=media_type,
|
||||
content_length=content_length,
|
||||
body=body if body is not None else _body(SAMPLE),
|
||||
)
|
||||
|
||||
|
||||
class _Untouched:
|
||||
"""Тело, которое нельзя начать читать."""
|
||||
|
||||
def __aiter__(self):
|
||||
raise AssertionError("body must not be read")
|
||||
|
||||
|
||||
async def test_upload_order_and_events(
|
||||
service: AttachmentService,
|
||||
database: FakeDatabase,
|
||||
storage: FakeObjectStorage,
|
||||
world: tuple[UUID, UUID, UUID],
|
||||
) -> None:
|
||||
attachment = await _upload(service, world)
|
||||
assert attachment.status == AttachmentStatus.QUEUED
|
||||
assert database.attachments[attachment.id].status == (
|
||||
AttachmentStatus.QUEUED
|
||||
)
|
||||
job = next(iter(database.jobs.values()))
|
||||
assert job.attachment_id == attachment.id
|
||||
assert job.state == AttachmentJobState.QUEUED
|
||||
assert storage.objects[attachment.staging_key][0] == SAMPLE
|
||||
events = [item for item in database.events if not item.startswith("cache")]
|
||||
assert events == [
|
||||
"enter",
|
||||
"chats.get_chat",
|
||||
"exit",
|
||||
"enter",
|
||||
"attachments.insert_attachment",
|
||||
"commit",
|
||||
"exit",
|
||||
"storage.put",
|
||||
"enter",
|
||||
"attachments.mark_queued",
|
||||
"jobs.insert_job",
|
||||
"commit",
|
||||
"exit",
|
||||
]
|
||||
assert not database.inside_block("storage.put")
|
||||
|
||||
|
||||
async def test_not_member_404_before_everything(
|
||||
service: AttachmentService,
|
||||
database: FakeDatabase,
|
||||
world: tuple[UUID, UUID, UUID],
|
||||
) -> None:
|
||||
_, bob, _ = world
|
||||
with pytest.raises(exceptions.ChatNotFound):
|
||||
await _upload(
|
||||
service,
|
||||
world,
|
||||
owner=bob,
|
||||
media_type="text/plain",
|
||||
body=_Untouched(),
|
||||
)
|
||||
assert database.attachments == {}
|
||||
|
||||
|
||||
async def test_type_415_before_size_413(
|
||||
service: AttachmentService,
|
||||
world: tuple[UUID, UUID, UUID],
|
||||
) -> None:
|
||||
with pytest.raises(exceptions.UnsupportedMediaType):
|
||||
await _upload(
|
||||
service,
|
||||
world,
|
||||
media_type="text/plain",
|
||||
content_length=ATTACHMENT_MAX_BYTES + 1,
|
||||
body=_Untouched(),
|
||||
)
|
||||
|
||||
|
||||
async def test_declared_size_413_before_slot(
|
||||
uow: FakeUnitOfWork,
|
||||
cache: FakeCache,
|
||||
storage: FakeObjectStorage,
|
||||
world: tuple[UUID, UUID, UUID],
|
||||
) -> None:
|
||||
slots = _slots()
|
||||
service = AttachmentService(uow, cache, storage, slots)
|
||||
async with slots.slot():
|
||||
# места нет, а отказ всё равно 413: место не ждали
|
||||
with pytest.raises(exceptions.PayloadTooLarge):
|
||||
await _upload(
|
||||
service,
|
||||
world,
|
||||
content_length=ATTACHMENT_MAX_BYTES + 1,
|
||||
body=_Untouched(),
|
||||
)
|
||||
|
||||
|
||||
async def test_busy_slots_429_without_reading_body(
|
||||
uow: FakeUnitOfWork,
|
||||
cache: FakeCache,
|
||||
storage: FakeObjectStorage,
|
||||
database: FakeDatabase,
|
||||
world: tuple[UUID, UUID, UUID],
|
||||
) -> None:
|
||||
slots = _slots()
|
||||
service = AttachmentService(uow, cache, storage, slots)
|
||||
async with slots.slot():
|
||||
with pytest.raises(exceptions.UploadsBusy):
|
||||
await _upload(service, world, body=_Untouched())
|
||||
assert database.attachments == {}
|
||||
|
||||
|
||||
async def test_counter_413_interrupt_and_body_timeout(
|
||||
service: AttachmentService,
|
||||
database: FakeDatabase,
|
||||
world: tuple[UUID, UUID, UUID],
|
||||
) -> None:
|
||||
with pytest.raises(exceptions.PayloadTooLarge):
|
||||
await _upload(
|
||||
service,
|
||||
world,
|
||||
body=_body(b"x" * ATTACHMENT_MAX_BYTES, b"x"),
|
||||
)
|
||||
|
||||
async def interrupted() -> AsyncIterator[bytes]:
|
||||
yield b"x"
|
||||
raise exceptions.UploadInterrupted
|
||||
|
||||
with pytest.raises(exceptions.UploadInterrupted):
|
||||
await _upload(service, world, body=interrupted())
|
||||
|
||||
async def slow() -> AsyncIterator[bytes]:
|
||||
yield b"x"
|
||||
await asyncio.sleep(30)
|
||||
|
||||
with pytest.raises(exceptions.UploadTimedOut):
|
||||
await _upload(service, world, body=slow())
|
||||
assert database.attachments == {}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("data", "media_type"),
|
||||
[
|
||||
(b"not an image", "image/png"),
|
||||
(SAMPLE, "image/jpeg"),
|
||||
(b"", "image/png"),
|
||||
],
|
||||
ids=["content", "mismatch", "empty"],
|
||||
)
|
||||
async def test_bad_content_415_no_row(
|
||||
service: AttachmentService,
|
||||
database: FakeDatabase,
|
||||
world: tuple[UUID, UUID, UUID],
|
||||
data: bytes,
|
||||
media_type: str,
|
||||
) -> None:
|
||||
with pytest.raises(exceptions.UnsupportedMediaType):
|
||||
await _upload(
|
||||
service,
|
||||
world,
|
||||
media_type=media_type,
|
||||
body=_body(data),
|
||||
)
|
||||
assert database.attachments == {}
|
||||
|
||||
|
||||
async def test_put_failure_leaves_pending_without_compensation(
|
||||
service: AttachmentService,
|
||||
database: FakeDatabase,
|
||||
world: tuple[UUID, UUID, UUID],
|
||||
) -> None:
|
||||
database.fail_on["storage.put"] = exceptions.ObjectStorageUnavailable()
|
||||
with pytest.raises(exceptions.ObjectStorageUnavailable):
|
||||
await _upload(service, world)
|
||||
row = next(iter(database.attachments.values()))
|
||||
assert row.status == AttachmentStatus.PENDING
|
||||
assert "storage.delete" not in database.events
|
||||
|
||||
|
||||
async def test_failure_on_statement_compensates(
|
||||
service: AttachmentService,
|
||||
database: FakeDatabase,
|
||||
storage: FakeObjectStorage,
|
||||
world: tuple[UUID, UUID, UUID],
|
||||
) -> None:
|
||||
"""Сценарий «Компенсация различает исход фиксации»: упало выражение."""
|
||||
database.fail_on["jobs.insert_job"] = UNAVAILABLE()
|
||||
with pytest.raises(UNAVAILABLE):
|
||||
await _upload(service, world)
|
||||
row = next(iter(database.attachments.values()))
|
||||
assert row.status == AttachmentStatus.PENDING
|
||||
assert storage.objects == {}
|
||||
assert "storage.delete" in database.events
|
||||
assert database.jobs == {}
|
||||
|
||||
|
||||
async def test_failure_on_commit_keeps_object(
|
||||
service: AttachmentService,
|
||||
database: FakeDatabase,
|
||||
storage: FakeObjectStorage,
|
||||
world: tuple[UUID, UUID, UUID],
|
||||
) -> None:
|
||||
"""Тот же сценарий: упала фиксация, исход неизвестен, объект на месте."""
|
||||
database.fail_commit = UNAVAILABLE()
|
||||
# первая фиксация — строка pending
|
||||
database.commits_before_fail = 1
|
||||
with pytest.raises(UNAVAILABLE):
|
||||
await _upload(service, world)
|
||||
row = next(iter(database.attachments.values()))
|
||||
assert row.staging_key in storage.objects
|
||||
assert "storage.delete" not in database.events
|
||||
|
||||
|
||||
async def test_removed_row_compensates_and_503(
|
||||
uow: FakeUnitOfWork,
|
||||
cache: FakeCache,
|
||||
database: FakeDatabase,
|
||||
storage: FakeObjectStorage,
|
||||
world: tuple[UUID, UUID, UUID],
|
||||
) -> None:
|
||||
"""`mark_queued` → `None`: уборщик пометил строку."""
|
||||
|
||||
class Marking(FakeObjectStorage):
|
||||
async def put(self, key, body, *, size, content_type) -> None:
|
||||
await super().put(key, body, size=size, content_type=content_type)
|
||||
row = next(iter(database.attachments.values()))
|
||||
database.attachments[row.id] = replace(
|
||||
row,
|
||||
updated_at=row.created_at.replace(year=2100),
|
||||
)
|
||||
|
||||
marking = Marking(database)
|
||||
service = AttachmentService(uow, cache, marking, _slots())
|
||||
with pytest.raises(UNAVAILABLE):
|
||||
await _upload(service, world)
|
||||
assert marking.objects == {}
|
||||
assert database.jobs == {}
|
||||
assert storage.objects == {}
|
||||
|
||||
|
||||
async def test_compensation_failure_keeps_original_error(
|
||||
service: AttachmentService,
|
||||
database: FakeDatabase,
|
||||
world: tuple[UUID, UUID, UUID],
|
||||
) -> None:
|
||||
database.fail_on["jobs.insert_job"] = UNAVAILABLE("db")
|
||||
database.fail_on["storage.delete"] = exceptions.ObjectStorageUnavailable()
|
||||
with pytest.raises(UNAVAILABLE) as error:
|
||||
await _upload(service, world)
|
||||
assert not isinstance(error.value, exceptions.ObjectStorageUnavailable)
|
||||
|
||||
|
||||
async def _row(
|
||||
uow: FakeUnitOfWork,
|
||||
world: tuple[UUID, UUID, UUID],
|
||||
*,
|
||||
status: AttachmentStatus,
|
||||
):
|
||||
alice, _, chat_id = world
|
||||
row = attachment_dto(chat_id=chat_id, owner_id=alice, status=status)
|
||||
async with uow:
|
||||
await uow.attachments.insert_attachment(attachment=row)
|
||||
await uow.commit()
|
||||
return row
|
||||
|
||||
|
||||
async def test_metadata_hidden_and_variants_only_ready(
|
||||
service: AttachmentService,
|
||||
uow: FakeUnitOfWork,
|
||||
world: tuple[UUID, UUID, UUID],
|
||||
) -> None:
|
||||
alice, bob, _ = world
|
||||
pending = await _row(uow, world, status=AttachmentStatus.PENDING)
|
||||
queued = await _row(uow, world, status=AttachmentStatus.QUEUED)
|
||||
ready = await _row(uow, world, status=AttachmentStatus.READY)
|
||||
variant = variant_dto(attachment_id=ready.id)
|
||||
async with uow:
|
||||
await uow.variants.insert_variant(
|
||||
attachment_id=ready.id,
|
||||
variant=variant,
|
||||
)
|
||||
await uow.variants.insert_variant(
|
||||
attachment_id=queued.id,
|
||||
variant=variant_dto(attachment_id=queued.id),
|
||||
)
|
||||
await uow.commit()
|
||||
with pytest.raises(exceptions.AttachmentNotFound):
|
||||
await service.get_attachment(attachment_id=pending.id, user_id=alice)
|
||||
with pytest.raises(exceptions.AttachmentNotFound):
|
||||
await service.get_attachment(attachment_id=uuid4(), user_id=alice)
|
||||
with pytest.raises(exceptions.AttachmentNotFound):
|
||||
await service.get_attachment(attachment_id=ready.id, user_id=bob)
|
||||
shown = await service.get_attachment(attachment_id=ready.id, user_id=alice)
|
||||
assert shown.variants == (variant,)
|
||||
waiting = await service.get_attachment(
|
||||
attachment_id=queued.id,
|
||||
user_id=alice,
|
||||
)
|
||||
assert waiting.variants == ()
|
||||
|
||||
|
||||
async def test_open_content_branches(
|
||||
service: AttachmentService,
|
||||
uow: FakeUnitOfWork,
|
||||
database: FakeDatabase,
|
||||
storage: FakeObjectStorage,
|
||||
world: tuple[UUID, UUID, UUID],
|
||||
) -> None:
|
||||
alice, _, _ = world
|
||||
queued = await _row(uow, world, status=AttachmentStatus.QUEUED)
|
||||
with pytest.raises(exceptions.AttachmentNotReady):
|
||||
await service.open_content(attachment_id=queued.id, user_id=alice)
|
||||
|
||||
ready = await _row(uow, world, status=AttachmentStatus.READY)
|
||||
# нет варианта
|
||||
with pytest.raises(exceptions.AttachmentNotFound):
|
||||
await service.open_content(
|
||||
attachment_id=ready.id,
|
||||
user_id=alice,
|
||||
variant=AttachmentVariantName.CROP,
|
||||
)
|
||||
# нет объекта
|
||||
with pytest.raises(exceptions.AttachmentContentMissing):
|
||||
await service.open_content(attachment_id=ready.id, user_id=alice)
|
||||
# другой размер
|
||||
storage.seed(ready.object_key, b"different size")
|
||||
with pytest.raises(exceptions.AttachmentContentMissing):
|
||||
await service.open_content(attachment_id=ready.id, user_id=alice)
|
||||
# целый объект
|
||||
storage.seed(ready.object_key, b"content")
|
||||
content = await service.open_content(attachment_id=ready.id, user_id=alice)
|
||||
assert b"".join([chunk async for chunk in content.chunks]) == b"content"
|
||||
await content.close()
|
||||
# потерянный — 404 без обращения к S3
|
||||
async with uow:
|
||||
await uow.attachments.mark_lost(
|
||||
attachment_id=ready.id,
|
||||
object_key=ready.object_key,
|
||||
lost_at=NOW,
|
||||
)
|
||||
await uow.commit()
|
||||
database.events.clear()
|
||||
with pytest.raises(exceptions.AttachmentNotFound):
|
||||
await service.open_content(attachment_id=ready.id, user_id=alice)
|
||||
assert "storage.get" not in database.events
|
||||
|
||||
|
||||
async def test_failed_attachment_metadata_has_code(
|
||||
service: AttachmentService,
|
||||
uow: FakeUnitOfWork,
|
||||
world: tuple[UUID, UUID, UUID],
|
||||
) -> None:
|
||||
alice, _, _ = world
|
||||
row = await _row(uow, world, status=AttachmentStatus.QUEUED)
|
||||
async with uow:
|
||||
await uow.attachments.commit_failed(
|
||||
attachment_id=row.id,
|
||||
error_code=AttachmentErrorCode.INVALID_IMAGE,
|
||||
updated_at=NOW,
|
||||
)
|
||||
await uow.commit()
|
||||
shown = await service.get_attachment(attachment_id=row.id, user_id=alice)
|
||||
assert shown.error_code == AttachmentErrorCode.INVALID_IMAGE
|
||||
with pytest.raises(exceptions.AttachmentNotReady):
|
||||
await service.open_content(attachment_id=row.id, user_id=alice)
|
||||
Reference in New Issue
Block a user