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,132 @@
|
||||
"""DTO для тестов: явное время, ничего случайного, кроме id."""
|
||||
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from hashlib import sha256
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from src.schemas import dataclasses
|
||||
from src.schemas.enums import (
|
||||
AttachmentJobState,
|
||||
AttachmentStatus,
|
||||
AttachmentVariantName,
|
||||
ImageType,
|
||||
)
|
||||
|
||||
NOW = datetime(2026, 3, 1, 12, 0, 0, tzinfo=UTC)
|
||||
# вид значения держит CHECK ck_users_password_hash_argon2id
|
||||
PASSWORD_HASH = "$argon2id$v=19$m=65536,t=3,p=4$c2FsdA$aGFzaA"
|
||||
|
||||
|
||||
def at(seconds: float) -> datetime:
|
||||
"""Момент относительно `NOW`."""
|
||||
return NOW + timedelta(seconds=seconds)
|
||||
|
||||
|
||||
def user_dto(*, username: str | None = None) -> dataclasses.UserFullDTO:
|
||||
return dataclasses.UserFullDTO(
|
||||
id=uuid4(),
|
||||
username=username or f"u{uuid4().hex[:12]}",
|
||||
display_name="User",
|
||||
created_at=NOW,
|
||||
)
|
||||
|
||||
|
||||
def chat_dto(
|
||||
*,
|
||||
member_ids: tuple[UUID, ...],
|
||||
created_at: datetime = NOW,
|
||||
) -> dataclasses.ChatFullDTO:
|
||||
return dataclasses.ChatFullDTO(
|
||||
id=uuid4(),
|
||||
title="Chat",
|
||||
member_ids=member_ids,
|
||||
created_at=created_at,
|
||||
)
|
||||
|
||||
|
||||
def message_dto(
|
||||
*,
|
||||
chat_id: UUID,
|
||||
sender_id: UUID,
|
||||
created_at: datetime = NOW,
|
||||
attachment_ids: tuple[UUID, ...] = (),
|
||||
) -> dataclasses.MessageFullDTO:
|
||||
return dataclasses.MessageFullDTO(
|
||||
id=uuid4(),
|
||||
chat_id=chat_id,
|
||||
sender_id=sender_id,
|
||||
text="hello",
|
||||
created_at=created_at,
|
||||
attachment_ids=attachment_ids,
|
||||
)
|
||||
|
||||
|
||||
def attachment_dto(
|
||||
*,
|
||||
chat_id: UUID,
|
||||
owner_id: UUID,
|
||||
status: AttachmentStatus = AttachmentStatus.PENDING,
|
||||
created_at: datetime = NOW,
|
||||
updated_at: datetime | None = None,
|
||||
staging: bool = True,
|
||||
namespace: str = "testdb",
|
||||
data: bytes = b"content",
|
||||
object_key: str | None = None,
|
||||
) -> dataclasses.AttachmentFullDTO:
|
||||
attachment_id = uuid4()
|
||||
return dataclasses.AttachmentFullDTO(
|
||||
id=attachment_id,
|
||||
chat_id=chat_id,
|
||||
owner_id=owner_id,
|
||||
filename="a.png",
|
||||
content_type=ImageType.PNG,
|
||||
size_bytes=len(data),
|
||||
sha256=sha256(data).digest(),
|
||||
object_key=object_key
|
||||
or f"{namespace}/attachments/{attachment_id}/original",
|
||||
staging_key=f"{namespace}/staging/{attachment_id}/original"
|
||||
if staging
|
||||
else None,
|
||||
status=status,
|
||||
created_at=created_at,
|
||||
updated_at=updated_at or created_at,
|
||||
)
|
||||
|
||||
|
||||
def job_dto(
|
||||
*,
|
||||
attachment_id: UUID,
|
||||
created_at: datetime = NOW,
|
||||
pipeline_version: int = 1,
|
||||
) -> dataclasses.AttachmentJobDTO:
|
||||
return dataclasses.AttachmentJobDTO(
|
||||
id=uuid4(),
|
||||
attachment_id=attachment_id,
|
||||
pipeline_version=pipeline_version,
|
||||
state=AttachmentJobState.QUEUED,
|
||||
attempt=0,
|
||||
created_at=created_at,
|
||||
updated_at=created_at,
|
||||
)
|
||||
|
||||
|
||||
def variant_dto(
|
||||
*,
|
||||
attachment_id: UUID,
|
||||
name: AttachmentVariantName = AttachmentVariantName.THUMBNAIL,
|
||||
pipeline_version: int = 1,
|
||||
) -> dataclasses.AttachmentVariantDTO:
|
||||
return dataclasses.AttachmentVariantDTO(
|
||||
name=name,
|
||||
pipeline_version=pipeline_version,
|
||||
object_key=(
|
||||
f"testdb/attachments/{attachment_id}/"
|
||||
f"v{pipeline_version}-{name.value}"
|
||||
),
|
||||
content_type="image/jpeg",
|
||||
size_bytes=3,
|
||||
width=10,
|
||||
height=10,
|
||||
sha256=sha256(b"var").digest(),
|
||||
created_at=NOW,
|
||||
)
|
||||
Reference in New Issue
Block a user