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,72 @@
|
||||
"""Быстрые тесты сервисов: без PostgreSQL, Valkey, S3, брокера, docker и сети.
|
||||
|
||||
`settings` читает `.env` и `config.toml` по абсолютному пути, поэтому поля, от
|
||||
которых зависят ветки сервисов, подставляются здесь: результат не зависит от
|
||||
локальных файлов и окружения (design change `unit-of-work`, решение 13).
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from fakes import (
|
||||
FakeCache,
|
||||
FakeDatabase,
|
||||
FakeJobPublisher,
|
||||
FakeObjectStorage,
|
||||
FakeUnitOfWork,
|
||||
)
|
||||
from src.config import settings
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def anyio_backend() -> str:
|
||||
return "asyncio"
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def fixed_settings(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
fixed = (
|
||||
(
|
||||
settings.worker,
|
||||
{
|
||||
"max_attempts": 3,
|
||||
"job_timeout_seconds": 60,
|
||||
"lease_margin_seconds": 10,
|
||||
"retry_base_delay_seconds": 5,
|
||||
"retry_max_delay_seconds": 300,
|
||||
"reconnect_seconds": 0.01,
|
||||
},
|
||||
),
|
||||
(settings.auth, {"session_ttl_seconds": 3600}),
|
||||
(settings.idempotency, {"ttl_seconds": 86400}),
|
||||
(settings.cache, {"chat_ttl_seconds": 600}),
|
||||
(settings.uploads, {"body_timeout_seconds": 1}),
|
||||
(settings.app, {"instance_id": "test-instance"}),
|
||||
)
|
||||
for section, fields in fixed:
|
||||
for name, value in fields.items():
|
||||
monkeypatch.setattr(section, name, value)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def database() -> FakeDatabase:
|
||||
return FakeDatabase()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def uow(database: FakeDatabase) -> FakeUnitOfWork:
|
||||
return FakeUnitOfWork(database)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def cache(database: FakeDatabase) -> FakeCache:
|
||||
return FakeCache(database)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def storage(database: FakeDatabase) -> FakeObjectStorage:
|
||||
return FakeObjectStorage(database)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def publisher(database: FakeDatabase) -> FakeJobPublisher:
|
||||
return FakeJobPublisher(database)
|
||||
Reference in New Issue
Block a user