|
| 1 | +"""Tests for service composition primitives.""" |
| 2 | + |
| 3 | +from collections.abc import AsyncGenerator |
| 4 | +from typing import Any |
| 5 | +from unittest.mock import MagicMock |
| 6 | + |
| 7 | +import pytest |
| 8 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 9 | + |
| 10 | +from advanced_alchemy.service import ServiceComposition, ServiceProvider, ServiceWithSession |
| 11 | + |
| 12 | +pytestmark = pytest.mark.unit |
| 13 | + |
| 14 | + |
| 15 | +class FakeServiceA: |
| 16 | + def __init__(self, *, session: AsyncSession) -> None: |
| 17 | + self.session = session |
| 18 | + |
| 19 | + |
| 20 | +class FakeServiceB: |
| 21 | + def __init__(self, *, session: AsyncSession) -> None: |
| 22 | + self.session = session |
| 23 | + |
| 24 | + |
| 25 | +class FakeServiceC: |
| 26 | + def __init__(self, *, session: AsyncSession) -> None: |
| 27 | + self.session = session |
| 28 | + |
| 29 | + |
| 30 | +async def provider_a(session: AsyncSession) -> AsyncGenerator[FakeServiceA, None]: |
| 31 | + yield FakeServiceA(session=session) |
| 32 | + |
| 33 | + |
| 34 | +async def provider_b(session: AsyncSession) -> AsyncGenerator[FakeServiceB, None]: |
| 35 | + yield FakeServiceB(session=session) |
| 36 | + |
| 37 | + |
| 38 | +async def provider_c(session: AsyncSession) -> AsyncGenerator[FakeServiceC, None]: |
| 39 | + yield FakeServiceC(session=session) |
| 40 | + |
| 41 | + |
| 42 | +def test_service_provider_alias_accepts_async_generator_provider() -> None: |
| 43 | + provider: ServiceProvider[FakeServiceA] = provider_a |
| 44 | + |
| 45 | + assert provider is provider_a |
| 46 | + |
| 47 | + |
| 48 | +@pytest.mark.asyncio |
| 49 | +async def test_starting_with_returns_typed_composition() -> None: |
| 50 | + session = MagicMock(spec=AsyncSession) |
| 51 | + composer = ServiceComposition.starting_with(provider_a) |
| 52 | + |
| 53 | + async with composer.open(session=session) as services: |
| 54 | + (a,) = services |
| 55 | + |
| 56 | + assert isinstance(a, FakeServiceA) |
| 57 | + assert a.session is session |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.asyncio |
| 61 | +async def test_chain_three_providers_share_session() -> None: |
| 62 | + session = MagicMock(spec=AsyncSession) |
| 63 | + composer = ServiceComposition.starting_with(provider_a).add(provider_b).add(provider_c) |
| 64 | + |
| 65 | + async with composer.open(session=session) as services: |
| 66 | + a, b, c = services |
| 67 | + |
| 68 | + assert isinstance(a, FakeServiceA) |
| 69 | + assert isinstance(b, FakeServiceB) |
| 70 | + assert isinstance(c, FakeServiceC) |
| 71 | + assert a.session is b.session is c.session is session |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.asyncio |
| 75 | +async def test_partial_failure_closes_entered_providers() -> None: |
| 76 | + cleanup_ran = False |
| 77 | + |
| 78 | + async def cleanup_provider(session: AsyncSession) -> AsyncGenerator[FakeServiceA, None]: |
| 79 | + try: |
| 80 | + yield FakeServiceA(session=session) |
| 81 | + finally: |
| 82 | + nonlocal cleanup_ran |
| 83 | + cleanup_ran = True |
| 84 | + |
| 85 | + async def failing_provider(session: AsyncSession) -> AsyncGenerator[Any, None]: |
| 86 | + if repr(session) == "unused": |
| 87 | + yield None |
| 88 | + raise RuntimeError("fail at entry") |
| 89 | + |
| 90 | + session = MagicMock(spec=AsyncSession) |
| 91 | + composer = ServiceComposition.starting_with(cleanup_provider).add(failing_provider) |
| 92 | + |
| 93 | + with pytest.raises(RuntimeError, match="fail at entry"): |
| 94 | + async with composer.open(session=session): |
| 95 | + pass |
| 96 | + |
| 97 | + assert cleanup_ran |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.asyncio |
| 101 | +async def test_user_exception_inside_block_closes_entered_providers() -> None: |
| 102 | + cleanup_ran = False |
| 103 | + |
| 104 | + async def cleanup_provider(session: AsyncSession) -> AsyncGenerator[FakeServiceA, None]: |
| 105 | + try: |
| 106 | + yield FakeServiceA(session=session) |
| 107 | + finally: |
| 108 | + nonlocal cleanup_ran |
| 109 | + cleanup_ran = True |
| 110 | + |
| 111 | + session = MagicMock(spec=AsyncSession) |
| 112 | + |
| 113 | + async def raise_user_error() -> None: |
| 114 | + async with ServiceComposition.starting_with(cleanup_provider).open(session=session): |
| 115 | + raise ValueError("user error") |
| 116 | + |
| 117 | + with pytest.raises(ValueError, match="user error"): |
| 118 | + await raise_user_error() |
| 119 | + |
| 120 | + assert cleanup_ran |
| 121 | + |
| 122 | + |
| 123 | +def test_service_with_session_protocol_recognizes_session_keyword_services() -> None: |
| 124 | + class GoodService: |
| 125 | + def __init__(self, *, session: AsyncSession) -> None: |
| 126 | + self.session = session |
| 127 | + |
| 128 | + service = GoodService(session=MagicMock(spec=AsyncSession)) |
| 129 | + |
| 130 | + assert isinstance(service, ServiceWithSession) |
0 commit comments