|
| 1 | +import contextlib |
| 2 | +import typing |
| 3 | +import uuid |
| 4 | + |
| 5 | +import modern_di |
| 6 | +import pytest |
| 7 | +import sqlalchemy as sa |
| 8 | +from advanced_alchemy.exceptions import NotFoundError |
| 9 | +from sqlalchemy import event |
| 10 | + |
| 11 | +from app import ioc, models, schemas |
| 12 | +from app.resources.db import create_primary_sa_engine |
| 13 | + |
| 14 | + |
| 15 | +if typing.TYPE_CHECKING: |
| 16 | + from sqlalchemy.ext.asyncio import AsyncEngine |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +async def primary_engine(di_container: modern_di.Container) -> typing.AsyncIterator[AsyncEngine]: |
| 21 | + engine = create_primary_sa_engine() |
| 22 | + di_container.override(ioc.Database.dynamic_engine, engine) |
| 23 | + try: |
| 24 | + yield engine |
| 25 | + finally: |
| 26 | + di_container.reset_override(ioc.Database.dynamic_engine) |
| 27 | + await engine.dispose() |
| 28 | + |
| 29 | + |
| 30 | +@contextlib.contextmanager |
| 31 | +def record_in_transaction(engine: AsyncEngine) -> typing.Iterator[list[bool]]: |
| 32 | + in_transaction: typing.Final[list[bool]] = [] |
| 33 | + |
| 34 | + def record(connection: sa.Connection, *_: object) -> None: |
| 35 | + driver_connection = connection.connection.driver_connection |
| 36 | + assert driver_connection is not None |
| 37 | + in_transaction.append(driver_connection.is_in_transaction()) |
| 38 | + |
| 39 | + event.listen(engine.sync_engine, "after_cursor_execute", record) |
| 40 | + try: |
| 41 | + yield in_transaction |
| 42 | + finally: |
| 43 | + event.remove(engine.sync_engine, "after_cursor_execute", record) |
| 44 | + |
| 45 | + |
| 46 | +async def test_create_deck_sends_no_begin_or_commit( |
| 47 | + di_container: modern_di.Container, primary_engine: AsyncEngine |
| 48 | +) -> None: |
| 49 | + key: typing.Final = uuid.uuid4() |
| 50 | + try: |
| 51 | + with record_in_transaction(primary_engine) as in_transaction: |
| 52 | + async with di_container.build_child_container(scope=modern_di.Scope.REQUEST) as container: |
| 53 | + use_case = container.resolve_provider(ioc.UseCases.create_deck_use_case) |
| 54 | + await use_case(data=schemas.DeckCreate(name="autocommit", idempotency_key=key)) |
| 55 | + finally: |
| 56 | + async with primary_engine.begin() as connection: |
| 57 | + await connection.execute(sa.delete(models.Deck).where(models.Deck.idempotency_key == key)) |
| 58 | + |
| 59 | + assert in_transaction == [False] |
| 60 | + |
| 61 | + |
| 62 | +async def test_update_deck_sends_no_begin_or_commit( |
| 63 | + di_container: modern_di.Container, primary_engine: AsyncEngine |
| 64 | +) -> None: |
| 65 | + with record_in_transaction(primary_engine) as in_transaction: |
| 66 | + async with di_container.build_child_container(scope=modern_di.Scope.REQUEST) as container: |
| 67 | + use_case = container.resolve_provider(ioc.UseCases.update_deck_use_case) |
| 68 | + with pytest.raises(NotFoundError): |
| 69 | + await use_case(deck_id=-1, data=schemas.DeckUpdate(name="autocommit")) |
| 70 | + |
| 71 | + assert in_transaction == [False] |
0 commit comments