2023-04-03 03:50:59 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import pytest
|
|
|
|
import pytest_asyncio
|
|
|
|
from typing import Generator
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
|
|
from app.main import app
|
|
|
|
from app.database import Base
|
|
|
|
from app.database import async_engine
|
|
|
|
from app.database import async_session
|
|
|
|
from app.database import engine
|
|
|
|
from app.database import SessionLocal
|
|
|
|
|
|
|
|
from sqlalchemy import orm
|
|
|
|
|
|
|
|
|
2023-07-30 14:50:08 +02:00
|
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
|
|
async def async_db_session():
|
|
|
|
async with async_session() as session: # type: ignore
|
|
|
|
async with async_engine.begin() as conn:
|
|
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
|
|
yield session
|
|
|
|
async with async_engine.begin() as conn:
|
|
|
|
await conn.run_sync(Base.metadata.drop_all)
|
|
|
|
|
2023-04-03 03:50:59 +02:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def db():
|
2023-07-30 14:50:08 +02:00
|
|
|
_Session = orm.scoped_session(SessionLocal)
|
2023-04-03 03:50:59 +02:00
|
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
with _Session() as db_session:
|
|
|
|
try:
|
|
|
|
yield db_session
|
|
|
|
finally:
|
|
|
|
db_session.close()
|
|
|
|
Base.metadata.drop_all(bind=engine)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def client(db) -> Generator:
|
|
|
|
with TestClient(app) as c:
|
|
|
|
yield c
|