|
16 | 16 | from sqlalchemy.ext.asyncio import AsyncConnection, AsyncEngine |
17 | 17 |
|
18 | 18 |
|
| 19 | +async def test_sa_transactions(asyncpg_engine: AsyncEngine): |
| 20 | + # |
| 21 | + # SEE https://docs.sqlalchemy.org/en/20/orm/extensions/asyncio.html#synopsis-core |
| 22 | + # |
| 23 | + |
| 24 | + total_count_query = sa.select(sa.func.count()).select_from(tags) |
| 25 | + |
| 26 | + # WRITE queries |
| 27 | + query1 = tags.insert().values(name="query1", color="blue") |
| 28 | + query11 = tags.insert().values(name="query11", color="blue") |
| 29 | + query111 = tags.insert().values(name="query111", color="blue") |
| 30 | + query1111 = tags.insert().values(name="query1111", color="blue") |
| 31 | + query112 = tags.insert().values(name="query112", color="blue") |
| 32 | + query12 = tags.insert().values(name="query12", color="blue") |
| 33 | + query2 = tags.insert().values(name="query2", color="blue") |
| 34 | + |
| 35 | + # to make it fail, just repeat query since `id` is unique |
| 36 | + # TODO: query1111 = tags.insert().values(id=1, name="query1111", color="blue") |
| 37 | + # TODO: await conn.commit() # explicit commit ! |
| 38 | + |
| 39 | + # TODO: if this is true, then the order of execution is NOT preserved? |
| 40 | + async with asyncpg_engine.connect() as conn: |
| 41 | + |
| 42 | + await conn.execute(query1) |
| 43 | + |
| 44 | + async with conn.begin(): # savepoint |
| 45 | + await conn.execute(query11) |
| 46 | + |
| 47 | + async with conn.begin_nested(): # savepoint |
| 48 | + await conn.execute(query111) |
| 49 | + |
| 50 | + async with conn.begin_nested(): # savepoint |
| 51 | + await conn.execute(query1111) |
| 52 | + |
| 53 | + await conn.execute(query112) |
| 54 | + |
| 55 | + total_count = (await conn.execute(total_count_query)).scalar() |
| 56 | + assert total_count == 1 # (query1111) |
| 57 | + |
| 58 | + await conn.execute(query12) |
| 59 | + |
| 60 | + total_count = (await conn.execute(total_count_query)).scalar() |
| 61 | + assert total_count == 3 # query111, (query1111), query112 |
| 62 | + |
| 63 | + await conn.execute(query2) |
| 64 | + |
| 65 | + total_count = (await conn.execute(total_count_query)).scalar() |
| 66 | + assert total_count == 5 # query11, (query111, (query1111), query112), query2 |
| 67 | + |
| 68 | + total_count = (await conn.execute(total_count_query)).scalar() |
| 69 | + assert total_count == 7 # includes query1, query2 |
| 70 | + |
| 71 | + |
19 | 72 | class _PageDict(TypedDict): |
20 | 73 | total_count: int |
21 | 74 | rows: list[dict[str, Any]] |
@@ -97,17 +150,7 @@ async def delete( |
97 | 150 | return result.rowcount > 0 |
98 | 151 |
|
99 | 152 |
|
100 | | -# async def test_it(asyncpg_engine: AsyncEngine): |
101 | | - |
102 | | -# async with asyncpg_engine.connect() as conn: |
103 | | -# async with conn.begin(): |
104 | | -# conn.execute() |
105 | | - |
106 | | - |
107 | 153 | async def test_transaction_context(asyncpg_engine: AsyncEngine): |
108 | | - # |
109 | | - # Similar to example in https://docs.sqlalchemy.org/en/20/orm/extensions/asyncio.html#synopsis-core |
110 | | - # using tags |
111 | 154 |
|
112 | 155 | tags_repo = OneResourceRepoDemo(engine=asyncpg_engine, table=tags) |
113 | 156 |
|
|
0 commit comments