2024-01-26 00:23:38 +03:00
|
|
|
import asyncio
|
2024-02-03 01:08:04 +03:00
|
|
|
from typing import AsyncGenerator, Generator
|
2024-01-30 13:05:15 +03:00
|
|
|
|
2024-01-26 00:23:38 +03:00
|
|
|
import pytest
|
|
|
|
import pytest_asyncio
|
2024-01-30 13:05:15 +03:00
|
|
|
from fastapi import FastAPI
|
|
|
|
from httpx import AsyncClient
|
2024-02-02 23:38:12 +03:00
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
2024-01-26 00:23:38 +03:00
|
|
|
|
2024-01-30 13:05:15 +03:00
|
|
|
from fastfood.app import create_app
|
2024-01-26 00:23:38 +03:00
|
|
|
from fastfood.config import settings
|
|
|
|
from fastfood.dbase import get_async_session
|
|
|
|
from fastfood.models import Base
|
|
|
|
|
|
|
|
async_engine = create_async_engine(settings.TESTDATABASE_URL_asyncpg)
|
|
|
|
async_session_maker = async_sessionmaker(
|
|
|
|
async_engine,
|
|
|
|
class_=AsyncSession,
|
|
|
|
expire_on_commit=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-02-03 01:08:04 +03:00
|
|
|
@pytest.fixture(scope='session', autouse=True)
|
2024-01-26 00:23:38 +03:00
|
|
|
def event_loop():
|
2024-01-29 04:35:37 +03:00
|
|
|
try:
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
except RuntimeError:
|
|
|
|
loop = asyncio.new_event_loop()
|
2024-01-26 00:23:38 +03:00
|
|
|
yield loop
|
|
|
|
loop.close()
|
|
|
|
|
|
|
|
|
2024-02-03 01:08:04 +03:00
|
|
|
@pytest_asyncio.fixture(scope='session', autouse=True)
|
2024-01-30 23:11:40 +03:00
|
|
|
async def db_init(event_loop):
|
2024-01-26 00:23:38 +03:00
|
|
|
async with async_engine.begin() as conn:
|
|
|
|
await conn.run_sync(Base.metadata.drop_all)
|
|
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
|
|
yield
|
|
|
|
async with async_engine.begin() as conn:
|
|
|
|
await conn.run_sync(Base.metadata.drop_all)
|
|
|
|
|
2024-01-29 22:22:36 +03:00
|
|
|
|
2024-01-26 00:23:38 +03:00
|
|
|
async def get_test_session() -> AsyncGenerator[AsyncSession, None]:
|
|
|
|
async with async_session_maker() as session:
|
|
|
|
yield session
|
|
|
|
|
|
|
|
|
2024-02-03 01:08:04 +03:00
|
|
|
@pytest.fixture(scope='session')
|
2024-01-30 23:11:40 +03:00
|
|
|
def app(event_loop) -> Generator[FastAPI, None, None]:
|
2024-01-30 13:05:15 +03:00
|
|
|
app: FastAPI = create_app()
|
2024-01-26 00:23:38 +03:00
|
|
|
app.dependency_overrides[get_async_session] = get_test_session
|
|
|
|
yield app
|
2024-01-27 14:43:46 +03:00
|
|
|
|
|
|
|
|
2024-02-04 00:24:09 +03:00
|
|
|
@pytest_asyncio.fixture(scope='session')
|
2024-01-30 13:05:15 +03:00
|
|
|
async def client(app) -> AsyncGenerator[AsyncClient, None]:
|
2024-01-27 14:43:46 +03:00
|
|
|
async with AsyncClient(
|
2024-01-30 13:05:15 +03:00
|
|
|
app=app,
|
2024-02-03 01:08:04 +03:00
|
|
|
base_url='http://localhost:8000/api/v1/menus',
|
2024-01-27 14:43:46 +03:00
|
|
|
) as async_client:
|
|
|
|
yield async_client
|
2024-02-02 23:38:12 +03:00
|
|
|
|
|
|
|
|
2024-02-03 01:08:04 +03:00
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def session_data() -> dict:
|
2024-02-02 23:38:12 +03:00
|
|
|
return {}
|