diff --git a/app/main.py b/app/main.py index 03448a2..331db96 100644 --- a/app/main.py +++ b/app/main.py @@ -1,15 +1,16 @@ from contextlib import asynccontextmanager +from typing import Generator from fastapi import FastAPI from app.api import main_router -from app.core import create_admin, settings +from app.core import create_admin, get_async_session, settings @asynccontextmanager -async def lifespan(app: FastAPI): - await create_admin() - yield +async def lifespan(app: FastAPI, async_generator: Generator = get_async_session): + yield await create_admin(async_generator) + app = FastAPI(lifespan=lifespan, title=settings.app_title, diff --git a/tests/unit_tests/test_user.py b/tests/unit_tests/test_user.py index dc9e254..9a19a4e 100644 --- a/tests/unit_tests/test_user.py +++ b/tests/unit_tests/test_user.py @@ -1,24 +1,33 @@ import pytest from app.core.user import create_admin, create_user +from app.main import lifespan from tests.conftest import User, override_get_async_session, settings +def check_user(user: User, is_superuser: bool = True) -> None: + assert user.email == settings.admin_email + assert user.hashed_password + assert user.is_superuser == is_superuser + + @pytest.mark.asyncio @pytest.mark.parametrize('is_superuser', (True, False)) async def test_create_user(is_superuser) -> None: - user: User = await create_user(override_get_async_session, + user = await create_user(override_get_async_session, settings.admin_email, settings.admin_password, is_superuser=is_superuser) - assert user.email == settings.admin_email - assert user.hashed_password - assert user.is_superuser == is_superuser + check_user(user, is_superuser) @pytest.mark.asyncio async def test_create_admin() -> None: - user: User = await create_admin(override_get_async_session) - assert user.email == settings.admin_email - assert user.hashed_password - assert user.is_superuser + user = await create_admin(override_get_async_session) + check_user(user) + + +@pytest.mark.asyncio +async def test_lifespan() -> None: + async with lifespan('', override_get_async_session) as user: + check_user(user)