microblog.pub/tests/conftest.py

40 lines
993 B
Python
Raw Normal View History

2022-06-22 20:11:22 +02:00
from typing import Generator
import pytest
2022-07-29 09:24:51 +02:00
import pytest_asyncio
2022-06-22 20:11:22 +02:00
from fastapi.testclient import TestClient
from app.database import Base
2022-06-29 20:43:17 +02:00
from app.database import async_engine
from app.database import async_session
2022-06-22 20:11:22 +02:00
from app.database import engine
from app.main import app
2022-07-24 20:27:58 +02:00
from tests.factories import _Session
2022-06-22 20:11:22 +02:00
2022-07-29 09:24:51 +02:00
@pytest_asyncio.fixture
2022-06-29 20:43:17 +02:00
async def async_db_session():
async with async_session() as session:
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)
2022-06-22 20:11:22 +02:00
@pytest.fixture
def db() -> Generator:
Base.metadata.create_all(bind=engine)
2022-07-25 08:14:34 +02:00
with _Session() as db_session:
try:
yield db_session
finally:
db_session.close()
Base.metadata.drop_all(bind=engine)
2022-06-22 20:11:22 +02:00
@pytest.fixture
2022-07-24 20:27:58 +02:00
def client(db) -> Generator:
2022-06-22 20:11:22 +02:00
with TestClient(app) as c:
yield c