Skip to content

Commit 22c1e10

Browse files
committed
fix in is_context_initiated, pop_db_session_from_context, get_db_session_from_context
1 parent db838bb commit 22c1e10

File tree

13 files changed

+152
-27
lines changed

13 files changed

+152
-27
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ lint:
55
flake8 .
66

77
test:
8-
pytest --cov context_async_sqlalchemy examples/fastapi_example/tests examples/starlette_example/tests examples/fastapi_with_pure_asgi_example/tests --cov-report=term-missing
8+
pytest --cov context_async_sqlalchemy tests examples/fastapi_example/tests examples/starlette_example/tests examples/fastapi_with_pure_asgi_example/tests --cov-report=term-missing
99

1010
test_fastapi:
1111
pytest examples/fastapi_example/tests

context_async_sqlalchemy/context.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ def is_context_initiated() -> bool:
2222
"""
2323
Checks whether the context is initiated
2424
"""
25-
return bool(_db_session_ctx.get())
25+
return _db_session_ctx.get() is not None
2626

2727

28-
def pop_db_session_from_context(context_key: str) -> AsyncSession | None:
28+
def pop_db_session_from_context(connect: DBConnect) -> AsyncSession | None:
2929
"""
3030
Removes a session from the context
3131
"""
3232
session_ctx = _db_session_ctx.get()
3333
if not session_ctx:
3434
return None
3535

36-
session: AsyncSession | None = session_ctx.pop(context_key, None)
36+
session: AsyncSession | None = session_ctx.pop(connect.context_key, None)
3737
return session
3838

3939

@@ -50,12 +50,12 @@ async def reset_db_session_ctx(
5050
_db_session_ctx.reset(token)
5151

5252

53-
def get_db_session_from_context(context_key: str) -> AsyncSession | None:
53+
def get_db_session_from_context(connect: DBConnect) -> AsyncSession | None:
5454
"""
5555
Extracts the session from the context
5656
"""
5757
session_ctx = _get_initiated_context()
58-
return session_ctx.get(context_key)
58+
return session_ctx.get(connect.context_key)
5959

6060

6161
def put_db_session_to_context(

context_async_sqlalchemy/session.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async def db_session(connect: DBConnect) -> AsyncSession:
1919
session = await db_session(connect)
2020
...
2121
"""
22-
session = get_db_session_from_context(connect.context_key)
22+
session = get_db_session_from_context(connect)
2323
if not session:
2424
session = await connect.create_session()
2525
put_db_session_to_context(connect, session)
@@ -86,7 +86,7 @@ async def commit_db_session(connect: DBConnect) -> None:
8686
await your_function_with_db_session()
8787
await commit_db_session(connect)
8888
"""
89-
session = get_db_session_from_context(connect.context_key)
89+
session = get_db_session_from_context(connect)
9090
if session and session.in_transaction():
9191
await session.commit()
9292

@@ -99,7 +99,7 @@ async def rollback_db_session(connect: DBConnect) -> None:
9999
await your_function_with_db_session()
100100
await rollback_db_session(connect)
101101
"""
102-
session = get_db_session_from_context(connect.context_key)
102+
session = get_db_session_from_context(connect)
103103
if session and session.in_transaction():
104104
await session.rollback()
105105

examples/fastapi_example/routes/atomic_prev_transaction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ async def atomic_and_previous_transaction() -> None:
5454

5555
async def _insert_1() -> None:
5656
session = await db_session(connection)
57-
stmt = insert(ExampleTable).values()
57+
stmt = insert(ExampleTable)
5858
await session.execute(stmt)

examples/fastapi_example/routes/auto_rollback_by_status_code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async def auto_rollback_by_status_code() -> None:
1212
let's imagine that an error code was returned.
1313
"""
1414
session = await db_session(connection)
15-
stmt = insert(ExampleTable).values(text="example_with_db_session")
15+
stmt = insert(ExampleTable)
1616
await session.execute(stmt)
1717

1818
raise HTTPException(status_code=500)

examples/fastapi_example/routes/councurrent_queries.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ async def concurrent_queries() -> None:
3131
),
3232
_insert_non_ctx(), # new non context session
3333
_insert_non_ctx_manual(), # new non context session
34+
run_in_new_ctx( # new context and session with autorollback
35+
_insert_with_exception
36+
),
37+
return_exceptions=True,
3438
)
3539

3640

3741
async def _insert() -> None:
3842
session = await db_session(connection)
39-
stmt = insert(ExampleTable).values(text="example_multiple_sessions")
43+
stmt = insert(ExampleTable)
4044
await session.execute(stmt)
4145

4246

@@ -58,7 +62,7 @@ async def _insert_non_ctx() -> None:
5862
You don't have to use the context to work with sessions at all
5963
"""
6064
async with new_non_ctx_atomic_session(connection) as session:
61-
stmt = insert(ExampleTable).values(text="example_multiple_sessions")
65+
stmt = insert(ExampleTable)
6266
await session.execute(stmt)
6367

6468

@@ -67,6 +71,13 @@ async def _insert_non_ctx_manual() -> None:
6771
You don't have to use the context to work with sessions at all
6872
"""
6973
async with new_non_ctx_session(connection) as session:
70-
stmt = insert(ExampleTable).values(text="example_multiple_sessions")
74+
stmt = insert(ExampleTable)
7175
await session.execute(stmt)
7276
await session.commit()
77+
78+
79+
async def _insert_with_exception() -> None:
80+
session = await db_session(connection)
81+
stmt = insert(ExampleTable)
82+
await session.execute(stmt)
83+
raise Exception()

examples/fastapi_example/routes/early_connection_close.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,5 @@ async def _insert() -> None:
3434
request.
3535
"""
3636
session = await db_session(connection)
37-
stmt = insert(ExampleTable).values(
38-
text="example_with_early_connection_close"
39-
)
37+
stmt = insert(ExampleTable)
4038
await session.execute(stmt)

examples/fastapi_with_pure_asgi_example/routes/councurrent_queries.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ async def concurrent_queries() -> None:
3131
),
3232
_insert_non_ctx(), # new non context session
3333
_insert_non_ctx_manual(), # new non context session
34+
run_in_new_ctx( # new context and session with autorollback
35+
_insert_with_exception
36+
),
37+
return_exceptions=True,
3438
)
3539

3640

3741
async def _insert() -> None:
3842
session = await db_session(connection)
39-
stmt = insert(ExampleTable).values(text="example_multiple_sessions")
43+
stmt = insert(ExampleTable)
4044
await session.execute(stmt)
4145

4246

@@ -58,7 +62,7 @@ async def _insert_non_ctx() -> None:
5862
You don't have to use the context to work with sessions at all
5963
"""
6064
async with new_non_ctx_atomic_session(connection) as session:
61-
stmt = insert(ExampleTable).values(text="example_multiple_sessions")
65+
stmt = insert(ExampleTable)
6266
await session.execute(stmt)
6367

6468

@@ -67,6 +71,13 @@ async def _insert_non_ctx_manual() -> None:
6771
You don't have to use the context to work with sessions at all
6872
"""
6973
async with new_non_ctx_session(connection) as session:
70-
stmt = insert(ExampleTable).values(text="example_multiple_sessions")
74+
stmt = insert(ExampleTable)
7175
await session.execute(stmt)
7276
await session.commit()
77+
78+
79+
async def _insert_with_exception() -> None:
80+
session = await db_session(connection)
81+
stmt = insert(ExampleTable)
82+
await session.execute(stmt)
83+
raise Exception()

examples/starlette_example/routes/councurrent_queries.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ async def concurrent_queries(_: Request) -> JSONResponse:
3232
),
3333
_insert_non_ctx(), # new non context session
3434
_insert_non_ctx_manual(), # new non context session
35+
run_in_new_ctx( # new context and session with autorollback
36+
_insert_with_exception
37+
),
38+
return_exceptions=True,
3539
)
3640
return JSONResponse({})
3741

3842

3943
async def _insert() -> None:
4044
session = await db_session(connection)
41-
stmt = insert(ExampleTable).values(text="example_multiple_sessions")
45+
stmt = insert(ExampleTable)
4246
await session.execute(stmt)
4347

4448

@@ -60,7 +64,7 @@ async def _insert_non_ctx() -> None:
6064
You don't have to use the context to work with sessions at all
6165
"""
6266
async with new_non_ctx_atomic_session(connection) as session:
63-
stmt = insert(ExampleTable).values(text="example_multiple_sessions")
67+
stmt = insert(ExampleTable)
6468
await session.execute(stmt)
6569

6670

@@ -69,6 +73,13 @@ async def _insert_non_ctx_manual() -> None:
6973
You don't have to use the context to work with sessions at all
7074
"""
7175
async with new_non_ctx_session(connection) as session:
72-
stmt = insert(ExampleTable).values(text="example_multiple_sessions")
76+
stmt = insert(ExampleTable)
7377
await session.execute(stmt)
7478
await session.commit()
79+
80+
81+
async def _insert_with_exception() -> None:
82+
session = await db_session(connection)
83+
stmt = insert(ExampleTable)
84+
await session.execute(stmt)
85+
raise Exception()

tests/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# Tests
22

33

4-
The library is tested in the context of real asynchronous applications.
5-
For this reason, the library’s tests are essentially the same as the
6-
example tests - each example includes a complete test suite that
7-
verifies all of the library’s capabilities.
4+
The library is also tested in the context of real asynchronous applications.
5+
Each example includes a complete test suite that verifies all of the
6+
library’s capabilities.
87

98
- [Tests with FastAPI](../examples/fastapi_example/tests)
109
- [Tests with Starlette](../examples/starlette_example/tests)

0 commit comments

Comments
 (0)