2022-11-06 20:09:44 +08:00
|
|
|
from typing import Any, Generator
|
2022-03-30 08:04:44 +08:00
|
|
|
|
|
|
|
import pytest_asyncio
|
|
|
|
from fastapi import FastAPI
|
|
|
|
from felicity.core.config import settings
|
|
|
|
from felicity.main import flims
|
2022-11-06 20:09:44 +08:00
|
|
|
from httpx import AsyncClient
|
|
|
|
from sqlalchemy import create_engine
|
2022-03-30 08:04:44 +08:00
|
|
|
|
|
|
|
engine = create_engine(settings.SQLALCHEMY_ASYNC_DATABASE_URI)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest_asyncio.fixture(scope="function")
|
|
|
|
async def app() -> Generator[FastAPI, Any, None]:
|
|
|
|
"""
|
|
|
|
Create a fresh database on each test case.
|
|
|
|
"""
|
|
|
|
# async with async_engine.begin() as conn:
|
|
|
|
# await conn.run_sync(DBModel.metadata.create_all)
|
|
|
|
|
|
|
|
yield flims
|
|
|
|
|
|
|
|
# async with async_engine.begin() as conn:
|
|
|
|
# await conn.run_sync(DBModel.metadata.drop_all)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest_asyncio.fixture(scope="function")
|
|
|
|
async def appX() -> Generator[FastAPI, Any, None]:
|
|
|
|
from alembic.config import Config
|
|
|
|
from alembic import command
|
|
|
|
import os, inspect
|
|
|
|
|
|
|
|
this_file_directory = os.path.dirname(os.path.abspath(inspect.stack()[0][1]))
|
2022-11-06 20:09:44 +08:00
|
|
|
root_directory = os.path.join(this_file_directory, "../..")
|
|
|
|
alembic_directory = os.path.join(root_directory, "migrations")
|
|
|
|
ini_path = os.path.join(root_directory, "alembic.ini")
|
2022-03-30 08:04:44 +08:00
|
|
|
|
|
|
|
config = Config(ini_path)
|
2022-11-06 20:09:44 +08:00
|
|
|
config.set_main_option("script_location", alembic_directory)
|
2022-03-30 08:04:44 +08:00
|
|
|
|
2022-11-06 20:09:44 +08:00
|
|
|
command.upgrade(config, "head")
|
2022-03-30 08:04:44 +08:00
|
|
|
|
|
|
|
yield flims
|
|
|
|
|
|
|
|
|
|
|
|
@pytest_asyncio.fixture(scope="function")
|
|
|
|
async def client(app: FastAPI) -> Generator[AsyncClient, Any, None]:
|
|
|
|
async with AsyncClient(app=app, base_url="http://localhost:8080/api/v1") as client:
|
|
|
|
yield client
|
|
|
|
|
|
|
|
|
|
|
|
# pytest tests --asyncio-mode=strict
|