fixtures&easytest
This commit is contained in:
48
tests/conftest.py
Normal file
48
tests/conftest.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import asyncio
|
||||
from typing import AsyncGenerator
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||
from fastfood.app import create_app
|
||||
|
||||
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,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def event_loop():
|
||||
loop = asyncio.get_event_loop_policy().new_event_loop()
|
||||
yield loop
|
||||
loop.close()
|
||||
|
||||
|
||||
@pytest_asyncio.fixture(scope="function", autouse=True)
|
||||
async def db_init():
|
||||
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)
|
||||
|
||||
|
||||
async def get_test_session() -> AsyncGenerator[AsyncSession, None]:
|
||||
async with async_session_maker() as session:
|
||||
yield session
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def app():
|
||||
app = create_app()
|
||||
app.dependency_overrides[get_async_session] = get_test_session
|
||||
yield app
|
21
tests/test_api.py
Normal file
21
tests/test_api.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import pytest
|
||||
from httpx import AsyncClient
|
||||
|
||||
|
||||
url = "http://localhost:8000/api/v1/menus"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_read_menus(app):
|
||||
async with AsyncClient(app=app, base_url=url) as ac:
|
||||
response = await ac.get("/")
|
||||
assert response.status_code == 200
|
||||
assert response.json() == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_write_menu(app):
|
||||
async with AsyncClient(app=app, base_url=url) as ac:
|
||||
response = await ac.post("/", json={"title": "ddd", "description": "hh"})
|
||||
assert response.status_code == 201
|
||||
assert response.json()["title"] == "ddd"
|
Reference in New Issue
Block a user