Repository navigation
Expand file tree
/
Copy pathtest_create_deck.py
More file actions
52 lines (44 loc) · 2.17 KB
/
Copy pathtest_create_deck.py
File metadata and controls
52 lines (44 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import asyncio
import typing
import uuid
import sqlalchemy as sa
from db_retry import Transaction
from app import models, schemas
from app.repositories import DecksRepository
from app.resources.db import create_sa_engine, create_session
from app.settings import settings
from app.use_cases.create_deck import CreateDeckUseCase
if typing.TYPE_CHECKING:
from sqlalchemy.ext.asyncio import AsyncEngine
async def _wait_until_blocked_on_a_lock(engine: AsyncEngine, pid: int) -> None:
wait_event_type = None
while wait_event_type != "Lock":
await asyncio.sleep(0.01)
async with engine.connect() as connection:
wait_event_type = await connection.scalar(
sa.text("SELECT wait_event_type FROM pg_stat_activity WHERE pid = :pid"), {"pid": pid}
)
async def test_create_deck_returns_the_deck_a_concurrent_request_created_with_the_same_key() -> None:
engine: typing.Final = create_sa_engine(settings=settings, url=settings.db_dsn_parsed)
idempotency_key: typing.Final = uuid.uuid4()
try:
async with engine.connect() as concurrent_request:
await concurrent_request.execute(
sa.insert(models.Deck).values(idempotency_key=idempotency_key, name="first")
)
session: typing.Final = create_session(engine)
pid: typing.Final = await (await session.connection()).scalar(sa.text("SELECT pg_backend_pid()"))
use_case: typing.Final = CreateDeckUseCase(
transaction=Transaction(session=session), decks_repository=DecksRepository(session=session)
)
pending: typing.Final = asyncio.create_task(
use_case(data=schemas.DeckCreate(idempotency_key=idempotency_key, name="second"))
)
await asyncio.wait_for(_wait_until_blocked_on_a_lock(engine, pid), timeout=5)
await concurrent_request.commit()
deck: typing.Final = await pending
assert deck.name == "first"
finally:
async with engine.begin() as connection:
await connection.execute(sa.delete(models.Deck).where(models.Deck.idempotency_key == idempotency_key))
await engine.dispose()