Skip to content

Commit 62bd46e

Browse files
committed
extending test
1 parent 46d6dbc commit 62bd46e

File tree

2 files changed

+56
-11
lines changed

2 files changed

+56
-11
lines changed

packages/postgres-database/src/simcore_postgres_database/base_repo.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ async def transaction_context(
2727
):
2828
async with get_or_create_connection(engine, connection) as conn:
2929
if conn.in_transaction():
30-
# async with conn.begin_nested():
30+
# FIXME: should not extend nested? async with conn.begin_nested():
31+
# depends on the analysis of test_sa_transactions
32+
# might need another function that produces a transaction ONLY
3133
yield conn
3234
else:
3335
try:

packages/postgres-database/tests/test_base_repo.py

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,59 @@
1616
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncEngine
1717

1818

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+
1972
class _PageDict(TypedDict):
2073
total_count: int
2174
rows: list[dict[str, Any]]
@@ -97,17 +150,7 @@ async def delete(
97150
return result.rowcount > 0
98151

99152

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-
107153
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
111154

112155
tags_repo = OneResourceRepoDemo(engine=asyncpg_engine, table=tags)
113156

0 commit comments

Comments
 (0)