Compare commits

..

No commits in common. "b474e21f0ffb2db2fd8a45ec573ff7fa23257288" and "f09b5b57b2f0cfd6889a5a38324a7310f3ef8ca8" have entirely different histories.

2 changed files with 17 additions and 129 deletions

View File

@ -1,20 +1,14 @@
import asyncio import asyncio
from typing import AsyncGenerator from typing import AsyncGenerator
from httpx import AsyncClient
import pytest import pytest
import pytest_asyncio import pytest_asyncio
from sqlalchemy.ext.asyncio import ( from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
AsyncSession,
async_sessionmaker,
create_async_engine,
)
from fastfood.app import create_app from fastfood.app import create_app
from fastfood.config import settings from fastfood.config import settings
from fastfood.dbase import get_async_session from fastfood.dbase import get_async_session
from fastfood.models import Base from fastfood.models import Base
async_engine = create_async_engine(settings.TESTDATABASE_URL_asyncpg) async_engine = create_async_engine(settings.TESTDATABASE_URL_asyncpg)
async_session_maker = async_sessionmaker( async_session_maker = async_sessionmaker(
async_engine, async_engine,
@ -35,7 +29,9 @@ async def db_init():
async with async_engine.begin() as conn: async with async_engine.begin() as conn:
await conn.run_sync(Base.metadata.drop_all) await conn.run_sync(Base.metadata.drop_all)
await conn.run_sync(Base.metadata.create_all) await conn.run_sync(Base.metadata.create_all)
yield yield
async with async_engine.begin() as conn: async with async_engine.begin() as conn:
await conn.run_sync(Base.metadata.drop_all) await conn.run_sync(Base.metadata.drop_all)
@ -50,11 +46,3 @@ def app():
app = create_app() app = create_app()
app.dependency_overrides[get_async_session] = get_test_session app.dependency_overrides[get_async_session] = get_test_session
yield app yield app
@pytest_asyncio.fixture(scope="session")
async def client(app):
async with AsyncClient(
app=app, base_url="http://localhost:8000/api/v1/menus",
) as async_client:
yield async_client

View File

@ -5,122 +5,22 @@ url = "http://localhost:8000/api/v1/menus"
class TestCrud: class TestCrud:
class Menu: @pytest.mark.asyncio
@staticmethod async def test_read_menus(self, app):
async def read_all(app): """тест пустой бд"""
"""чтение всех меню"""
async with AsyncClient(app=app, base_url=url) as ac: async with AsyncClient(app=app, base_url=url) as ac:
response = await ac.get("/") response = await ac.get("/")
return response.status_code, response.json() assert response.status_code == 200
assert response.json() == []
@staticmethod
async def get(app, data):
"""Получение меню по id"""
async with AsyncClient(app=app, base_url=url) as ac:
response = await ac.get(f"/{data.get('id')}")
return response.status_code, response.json()
@staticmethod
async def write(app, data):
"""создания меню"""
async with AsyncClient(app=app, base_url=url) as ac:
response = await ac.post("/", json=data)
return response.status_code, response.json()
@staticmethod
async def update(app, data):
"""Обновление меню по id"""
async with AsyncClient(app=app, base_url=url) as ac:
response = await ac.patch(f"/{data.get('id')}", json=data)
return response.status_code, response.json()
@staticmethod
async def delete(app, data):
"""Удаление меню по id"""
async with AsyncClient(app=app, base_url=url) as ac:
response = await ac.delete(f"/{data.get('id')}")
class Submenu:
@staticmethod
async def read_all(app, menu):
"""чтение всех меню"""
async with AsyncClient(app=app, base_url=url) as ac:
response = await ac.get(f"/{menu.get('id')}/submenus/")
print(response)
return response.status_code, response.json()
@staticmethod
async def get(app, menu, submenu):
"""Получение меню по id"""
async with AsyncClient(app=app, base_url=url) as ac:
response = await ac.get(
f"/{menu.get('id')}/submenus/{submenu.get('id')}",
)
return response.status_code, response.json()
@staticmethod
async def write(app, menu, submenu):
"""создания меню"""
async with AsyncClient(app=app, base_url=url) as ac:
response = await ac.post(
f"/{menu.get('id')}/submenus/", json=submenu,
)
return response.status_code, response.json()
@staticmethod
async def update(app, menu, submenu):
"""Обновление меню по id"""
async with AsyncClient(app=app, base_url=url) as ac:
response = await ac.patch(
f"/{menu.get('id')}/submenus/{submenu.get('id')}",
json=submenu,
)
return response.status_code, response.json()
@staticmethod
async def delete(app, menu, submenu):
"""Удаление меню по id"""
async with AsyncClient(app=app, base_url=url) as ac:
response = await ac.delete(f"/{menu.get('id')}/submenus/{submenu.get('id')}")
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_menu_crud(self, app): async def test_write_menu(self, app):
"""Тестирование функций меню""" """"""
code, rspn = await self.Menu.read_all(app) async with AsyncClient(app=app, base_url=url) as ac:
assert code == 200 response = await ac.post("/", json={"title": "menu 1", "description": None})
assert response.status_code == 201
data = {"title": "Menu", "description": None} assert response.json()["title"] == "menu 1"
code, rspn = await self.Menu.write(app, data) assert response.json()["description"] == None
assert code == 201
assert rspn["title"] == "Menu"
assert rspn["description"] is None
code, menu = await self.Menu.get(app, {"id": rspn.get("id")})
assert code == 200
assert menu["title"] == rspn["title"]
upd_data = {
"id": rspn.get("id"), "title": "upd Menu", "description": "",
}
code, upd_rspn = await self.Menu.update(app, upd_data)
assert code == 200
assert upd_rspn["title"] == "upd Menu"
code = await self.Menu.delete(app, rspn)
code, menu = await self.Menu.get(app, {"id": rspn.get("id")})
assert code == 404
@pytest.mark.asyncio
async def test_submenu(self, app):
menu = {"title": "Menu", "description": "main menu"}
code, rspn = await self.Menu.write(app, menu)
assert code == 201
menu.update(rspn)
print(menu)
code, rspn = await self.Submenu.read_all(app, menu)
assert code == 200
assert rspn == []
class TestСontinuity: class TestСontinuity: