|
| 1 | +from unittest.mock import patch |
| 2 | + |
| 3 | +from fastapi import Depends, FastAPI, HTTPException |
| 4 | +from pydantic import BaseModel |
| 5 | +from pytest import fixture, mark |
| 6 | +from sqlalchemy import text |
| 7 | +from structlog.testing import capture_logs |
| 8 | + |
| 9 | +pytestmark = [mark.sqlalchemy("1.4"), mark.require_asyncpg] |
| 10 | + |
| 11 | + |
| 12 | +@fixture |
| 13 | +def app(User): |
| 14 | + from fastapi_sqla import AsyncSession, setup |
| 15 | + |
| 16 | + app = FastAPI() |
| 17 | + setup(app) |
| 18 | + |
| 19 | + class UserIn(BaseModel): |
| 20 | + id: int |
| 21 | + first_name: str |
| 22 | + last_name: str |
| 23 | + |
| 24 | + @app.post("/users") |
| 25 | + def create_user(user: UserIn, session: AsyncSession = Depends()): |
| 26 | + session.add(User(**dict(user))) |
| 27 | + |
| 28 | + @app.get("/404") |
| 29 | + def get_users(session: AsyncSession = Depends(AsyncSession)): |
| 30 | + raise HTTPException(status_code=404, detail="YOLO") |
| 31 | + |
| 32 | + return app |
| 33 | + |
| 34 | + |
| 35 | +async def test_async_session_dependency(client, faker, async_session): |
| 36 | + userid = faker.unique.random_int() |
| 37 | + first_name = faker.first_name() |
| 38 | + last_name = faker.last_name() |
| 39 | + res = await client.post( |
| 40 | + "/users", json={"id": userid, "first_name": first_name, "last_name": last_name} |
| 41 | + ) |
| 42 | + assert res.status_code == 200, res.json() |
| 43 | + row = ( |
| 44 | + await async_session.execute( |
| 45 | + text(f"select * from public.user where id = {userid}") |
| 46 | + ) |
| 47 | + ).fetchone() |
| 48 | + assert row == (userid, first_name, last_name) |
| 49 | + |
| 50 | + |
| 51 | +@fixture |
| 52 | +async def user_1(async_sqla_connection): |
| 53 | + async with async_sqla_connection.begin(): |
| 54 | + await async_sqla_connection.execute( |
| 55 | + text("INSERT INTO public.user VALUES (1, 'bob', 'morane') ") |
| 56 | + ) |
| 57 | + yield |
| 58 | + async with async_sqla_connection.begin(): |
| 59 | + await async_sqla_connection.execute( |
| 60 | + text("DELETE FROM public.user WHERE id = 1") |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +async def test_commit_error_returns_500(client, user_1, mock_middleware): |
| 65 | + with capture_logs() as caplog: |
| 66 | + res = await client.post( |
| 67 | + "/users", |
| 68 | + json={"id": 1, "first_name": "Bob", "last_name": "Morane"}, |
| 69 | + headers={"origin": "localhost"}, |
| 70 | + ) |
| 71 | + |
| 72 | + assert res.status_code == 500 |
| 73 | + |
| 74 | + assert { |
| 75 | + "event": "commit failed, returning http error", |
| 76 | + "exc_info": True, |
| 77 | + "log_level": "error", |
| 78 | + } in caplog |
| 79 | + |
| 80 | + assert { |
| 81 | + "event": "http error, rolling back possibly uncommitted changes", |
| 82 | + "log_level": "warning", |
| 83 | + "status_code": 500, |
| 84 | + } in caplog |
| 85 | + |
| 86 | + mock_middleware.assert_called_once() |
| 87 | + |
| 88 | + |
| 89 | +async def test_rollback_on_http_exception(client, mock_middleware): |
| 90 | + with patch("fastapi_sqla.async_sqla.open_session") as open_session: |
| 91 | + session = open_session.return_value.__aenter__.return_value |
| 92 | + |
| 93 | + await client.get("/404") |
| 94 | + |
| 95 | + session.rollback.assert_awaited_once_with() |
| 96 | + mock_middleware.assert_called_once() |
| 97 | + |
| 98 | + |
| 99 | +async def test_rollback_on_http_exception_silent(client, mock_middleware): |
| 100 | + with capture_logs() as caplog: |
| 101 | + await client.get("/404") |
| 102 | + |
| 103 | + mock_middleware.assert_called_once() |
| 104 | + |
| 105 | + assert { |
| 106 | + "event": "http error, rolling back possibly uncommitted changes", |
| 107 | + "log_level": "warning", |
| 108 | + "status_code": 404, |
| 109 | + } not in caplog |
0 commit comments