Skip to content

Commit db838bb

Browse files
committed
update tests
1 parent 3bfd0be commit db838bb

36 files changed

+1829
-6
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 --cov-report=term-missing
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
99

1010
test_fastapi:
1111
pytest examples/fastapi_example/tests

context_async_sqlalchemy/starlette_utils/http_middleware.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717

1818
def add_starlette_http_db_session_middleware(app: Starlette) -> None:
1919
"""Adds middleware to the application"""
20-
app.add_middleware(
21-
BaseHTTPMiddleware, dispatch=starlette_http_db_session_middleware
22-
)
20+
app.add_middleware(StarletteHTTPDBSessionMiddleware)
2321

2422

2523
class StarletteHTTPDBSessionMiddleware(BaseHTTPMiddleware):

examples/fastapi_example/routes/atomic_prev_transaction.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ async def atomic_and_previous_transaction() -> None:
3636
# Open transaction
3737
await _insert_1()
3838

39+
# use current transaction and rollback
40+
try:
41+
async with atomic_db_session(connection, "append"):
42+
await _insert_1()
43+
raise Exception()
44+
except Exception:
45+
...
46+
47+
# Open transaction
48+
await _insert_1()
49+
3950
# raise InvalidRequestError error
4051
async with atomic_db_session(connection, "raise"):
4152
await _insert_1()

examples/fastapi_example/tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
from context_async_sqlalchemy.test_utils import rollback_session
1414
from examples.database import connection
15-
from examples.fastapi_example.setup_app import lifespan, setup_app
1615
from examples.models import ExampleTable
16+
from ..setup_app import lifespan, setup_app
1717

1818

1919
@pytest_asyncio.fixture
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
With set_test_context you can call funcs straight
3+
"""
4+
5+
import pytest
6+
from sqlalchemy.ext.asyncio import AsyncSession
7+
8+
from context_async_sqlalchemy.test_utils import set_test_context
9+
10+
from examples.fastapi_example.routes.atomic import atomic_base_example
11+
from ..conftest import count_rows_example_table
12+
13+
14+
@pytest.mark.asyncio
15+
async def test_atomic_base_example(
16+
db_session_test: AsyncSession,
17+
) -> None:
18+
async with set_test_context(auto_close=True):
19+
await atomic_base_example()
20+
21+
assert await count_rows_example_table(db_session_test) == 2
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# FastAPI example
2+
3+
An example of how to use the library with FastAPI
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""
2+
An example of how to use the library with FastAPI
3+
"""
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""Application entry point"""
2+
3+
from .setup_app import setup_app
4+
5+
6+
app = setup_app()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[project]
2+
name = "fastapi-example"
3+
version = "0.1.0"
4+
description = "fastapi-example"
5+
requires-python = ">=3.9"
6+
dependencies = [
7+
"context-async-sqlalchemy>=1.0.1",
8+
]
9+
10+
[dependency-groups]
11+
dev = [
12+
"asyncpg>=0.30.0",
13+
"fastapi>=0.121.1",
14+
"greenlet>=3.2.4",
15+
"httpx>=0.28.1",
16+
"psycopg2-binary>=2.9.11",
17+
"pydantic>=2.12.4",
18+
"pytest>=8.4.2",
19+
"pytest-asyncio>=1.2.0",
20+
]

examples/fastapi_with_pure_asgi_example/routes/README.md

Whitespace-only changes.

0 commit comments

Comments
 (0)