правки

develop
Сергей Ванюшкин 2024-01-31 14:52:14 +03:00
parent e2185cc904
commit 995be04dcb
2 changed files with 256 additions and 56 deletions

View File

@ -1,12 +1,11 @@
import asyncio import asyncio
from typing import AsyncGenerator, Generator from typing import AsyncGenerator, Dict, Generator
import pytest import pytest
import pytest_asyncio import pytest_asyncio
from fastapi import FastAPI from fastapi import FastAPI
from httpx import AsyncClient from httpx import AsyncClient
from sqlalchemy.ext.asyncio import (AsyncSession, async_sessionmaker, from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
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
@ -66,3 +65,8 @@ async def client(app) -> AsyncGenerator[AsyncClient, None]:
async def asession(event_loop) -> AsyncGenerator[AsyncSession, None]: async def asession(event_loop) -> AsyncGenerator[AsyncSession, None]:
async with async_session_maker() as session: async with async_session_maker() as session:
yield session yield session
@pytest.fixture(scope="session")
def session_data() -> Dict:
return {}

View File

@ -1,4 +1,4 @@
from typing import Tuple from typing import Dict, Tuple
import pytest import pytest
from httpx import AsyncClient, Response from httpx import AsyncClient, Response
@ -145,21 +145,36 @@ class TestBaseCrud:
return response.status_code return response.status_code
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_menu_crud(self, client: AsyncClient) -> None: async def test_menu_crud_empty(self, client: AsyncClient) -> None:
"""Тестирование функций меню""" """Тестирование функций меню"""
code, rspn = await self.Menu.read_all(client) code, rspn = await self.Menu.read_all(client)
assert code == 200 assert code == 200
assert rspn == []
@pytest.mark.asyncio
async def test_menu_crud_add(self, client: AsyncClient) -> None:
"""Тестирование функций меню"""
data = {"title": "Menu", "description": None} data = {"title": "Menu", "description": None}
code, rspn = await self.Menu.write(client, data) code, rspn = await self.Menu.write(client, data)
assert code == 201 assert code == 201
assert rspn["title"] == "Menu" assert rspn["title"] == "Menu"
assert rspn["description"] is None assert rspn["description"] is None
@pytest.mark.asyncio
async def test_menu_crud_get(self, client: AsyncClient) -> None:
"""Тестирование функций меню"""
data = {"title": "Menu", "description": None}
code, rspn = await self.Menu.write(client, data)
code, menu = await self.Menu.get(client, {"id": rspn.get("id")}) code, menu = await self.Menu.get(client, {"id": rspn.get("id")})
assert code == 200 assert code == 200
assert menu["title"] == rspn["title"] assert menu["title"] == rspn["title"]
@pytest.mark.asyncio
async def test_menu_crud_update(self, client: AsyncClient) -> None:
"""Тестирование функций меню"""
data = {"title": "Menu", "description": None}
code, rspn = await self.Menu.write(client, data)
upd_data = { upd_data = {
"id": rspn.get("id"), "id": rspn.get("id"),
"title": "upd Menu", "title": "upd Menu",
@ -169,18 +184,37 @@ class TestBaseCrud:
assert code == 200 assert code == 200
assert upd_rspn["title"] == "upd Menu" assert upd_rspn["title"] == "upd Menu"
@pytest.mark.asyncio
async def test_menu_crud_delete(self, client: AsyncClient) -> None:
"""Тестирование функций меню"""
data = {"title": "Menu", "description": None}
code, rspn = await self.Menu.write(client, data)
code = await self.Menu.delete(client, rspn) code = await self.Menu.delete(client, rspn)
assert code == 200 assert code == 200
code, menu = await self.Menu.get(client, {"id": rspn.get("id")}) code, rspn = await self.Menu.get(client, {"id": rspn.get("id")})
assert code == 404 assert code == 404
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_submenus(self, client) -> None: async def test_menu_crud_get_all(self, client: AsyncClient) -> None:
"""Тестирование функций меню"""
code, rspn = await self.Menu.read_all(client)
assert code == 200
assert rspn == []
data = {"title": "Menu", "description": None}
code, rspn = await self.Menu.write(client, data)
code, upd_rspn = await self.Menu.read_all(client)
assert code == 200
assert upd_rspn == [rspn]
@pytest.mark.asyncio
async def test_submenus_get_all(self, client) -> None:
# Создаем меню и проверяем ответ # Создаем меню и проверяем ответ
menu = {"title": "Menu", "description": "main menu"} menu = {"title": "Menu", "description": "main menu"}
code, rspn = await self.Menu.write(client, menu) code, rspn = await self.Menu.write(client, menu)
assert code == 201
menu.update(rspn) menu.update(rspn)
# Проверяем наличие подменю # Проверяем наличие подменю
@ -195,46 +229,22 @@ class TestBaseCrud:
"parent_menu": menu["id"], "parent_menu": menu["id"],
} }
code, rspn = await self.Submenu.write(client, menu, submenu) code, rspn = await self.Submenu.write(client, menu, submenu)
assert code == 201
submenu.update(rspn) submenu.update(rspn)
# Проверяем меню на наличие подменю # Проверяем наличие подменю
code, rspn = await self.Menu.get(client, menu) code, upd_rspn = await self.Submenu.read_all(client, menu)
assert code == 200 assert code == 200
assert rspn["submenus_count"] == 1 assert upd_rspn == [rspn]
# Обновляем подменю и проверяем
submenu["title"] = "updated_submenu"
code, rspn = await self.Submenu.update(client, menu, submenu)
assert code == 200
assert submenu["title"] == rspn["title"]
submenu.update(rspn)
# Удаляем подменю
code = await self.Submenu.delete(client, menu, submenu)
assert code == 200
# Проверяем меню
code, rspn = await self.Menu.get(client, menu)
assert code == 200
assert rspn["submenus_count"] == 0
# Проверяем удаленное подменю
code, rspn = await self.Submenu.get(client, menu, submenu)
assert code == 404
# удаляем сопутствующее # удаляем сопутствующее
await self.Submenu.delete(client, menu, submenu)
await self.Menu.delete(client, menu) await self.Menu.delete(client, menu)
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_dishes(self, client: AsyncClient) -> None: async def test_submenus_add(self, client) -> None:
# Создаем меню и проверяем ответ # Создаем меню и проверяем ответ
menu = { menu = {"title": "Menu", "description": "main menu"}
"title": "Menu",
"description": "main menu",
}
code, rspn = await self.Menu.write(client, menu) code, rspn = await self.Menu.write(client, menu)
assert code == 201
menu.update(rspn) menu.update(rspn)
# Создаем и проверяем подменю # Создаем и проверяем подменю
@ -247,6 +257,83 @@ class TestBaseCrud:
assert code == 201 assert code == 201
submenu.update(rspn) submenu.update(rspn)
# удаляем сопутствующее
await self.Submenu.delete(client, menu, submenu)
await self.Menu.delete(client, menu)
@pytest.mark.asyncio
async def test_submenus_update(self, client) -> None:
# Создаем меню и проверяем ответ
menu = {"title": "Menu", "description": "main menu"}
code, rspn = await self.Menu.write(client, menu)
menu.update(rspn)
# Создаем и проверяем подменю
submenu = {
"title": "Submenu",
"description": "submenu",
"parent_menu": menu["id"],
}
code, rspn = await self.Submenu.write(client, menu, submenu)
submenu.update(rspn)
# Обновляем подменю и проверяем
submenu["title"] = "updated_submenu"
code, rspn = await self.Submenu.update(client, menu, submenu)
assert code == 200
assert submenu["title"] == rspn["title"]
submenu.update(rspn)
# удаляем сопутствующее
await self.Submenu.delete(client, menu, submenu)
await self.Menu.delete(client, menu)
@pytest.mark.asyncio
async def test_submenus_delete(self, client) -> None:
# Создаем меню и проверяем ответ
menu = {"title": "Menu", "description": "main menu"}
code, rspn = await self.Menu.write(client, menu)
menu.update(rspn)
# Создаем и проверяем подменю
submenu = {
"title": "Submenu",
"description": "submenu",
"parent_menu": menu["id"],
}
code, rspn = await self.Submenu.write(client, menu, submenu)
submenu.update(rspn)
# Удаляем подменю
code = await self.Submenu.delete(client, menu, submenu)
assert code == 200
# Проверяем удаленное подменю
code, rspn = await self.Submenu.get(client, menu, submenu)
assert code == 404
# удаляем сопутствующее
await self.Menu.delete(client, menu)
@pytest.mark.asyncio
async def test_dishes_get_all(self, client: AsyncClient) -> None:
# Создаем меню и проверяем ответ
menu = {
"title": "Menu",
"description": "main menu",
}
code, rspn = await self.Menu.write(client, menu)
menu.update(rspn)
# Создаем и проверяем подменю
submenu = {
"title": "Submenu",
"description": "submenu",
"parent_menu": menu["id"],
}
code, rspn = await self.Submenu.write(client, menu, submenu)
submenu.update(rspn)
# Проверяем все блюда в подменю # Проверяем все блюда в подменю
code, rspn = await self.Dish.read_all(client, menu, submenu) code, rspn = await self.Dish.read_all(client, menu, submenu)
assert code == 200 assert code == 200
@ -263,20 +350,83 @@ class TestBaseCrud:
assert code == 201 assert code == 201
dish.update(rspn) dish.update(rspn)
code, upd_rspn = await self.Dish.read_all(client, menu, submenu)
assert code == 200
# удаляем сопутствующее
await self.Dish.delete(client, menu, submenu, dish)
await self.Submenu.delete(client, menu, submenu)
await self.Menu.delete(client, menu)
@pytest.mark.asyncio
async def test_dishes_add(self, client: AsyncClient) -> None:
# Создаем меню и проверяем ответ
menu = {
"title": "Menu",
"description": "main menu",
}
code, rspn = await self.Menu.write(client, menu)
menu.update(rspn)
# Создаем и проверяем подменю
submenu = {
"title": "Submenu",
"description": "submenu",
"parent_menu": menu["id"],
}
code, rspn = await self.Submenu.write(client, menu, submenu)
submenu.update(rspn)
# Добавляем блюдо
dish = {
"title": "dish",
"description": "some dish",
"price": "12.5",
"parent_submenu": submenu["id"],
}
code, rspn = await self.Dish.write(client, menu, submenu, dish)
assert code == 201
dish.update(rspn)
# Получаем блюдо # Получаем блюдо
code, rspn = await self.Dish.get(client, menu, submenu, dish) code, rspn = await self.Dish.get(client, menu, submenu, dish)
assert code == 200 assert code == 200
assert rspn["title"] == dish["title"] assert rspn["title"] == dish["title"]
# Проверяем меню на количество блюд # удаляем сопутствующее
code, rspn = await self.Menu.get(client, menu) await self.Dish.delete(client, menu, submenu, dish)
assert code == 200 await self.Submenu.delete(client, menu, submenu)
assert rspn["dishes_count"] == 1 await self.Menu.delete(client, menu)
# Проверяем подменю на наличие блюд @pytest.mark.asyncio
code, rspn = await self.Submenu.get(client, menu, submenu) async def test_dishes_update(self, client: AsyncClient) -> None:
assert code == 200 # Создаем меню и проверяем ответ
assert rspn["dishes_count"] == 1 menu = {
"title": "Menu",
"description": "main menu",
}
code, rspn = await self.Menu.write(client, menu)
menu.update(rspn)
# Создаем и проверяем подменю
submenu = {
"title": "Submenu",
"description": "submenu",
"parent_menu": menu["id"],
}
code, rspn = await self.Submenu.write(client, menu, submenu)
submenu.update(rspn)
# Добавляем блюдо
dish = {
"title": "dish",
"description": "some dish",
"price": "12.5",
"parent_submenu": submenu["id"],
}
code, rspn = await self.Dish.write(client, menu, submenu, dish)
dish.update(rspn)
# Обновляем блюдо и проверяем # Обновляем блюдо и проверяем
dish["title"] = "updated_dish" dish["title"] = "updated_dish"
@ -285,20 +435,44 @@ class TestBaseCrud:
assert dish["title"] == rspn["title"] assert dish["title"] == rspn["title"]
dish.update(rspn) dish.update(rspn)
# удаляем сопутствующее
await self.Dish.delete(client, menu, submenu, dish)
await self.Submenu.delete(client, menu, submenu)
await self.Menu.delete(client, menu)
@pytest.mark.asyncio
async def test_dishes_delete(self, client: AsyncClient) -> None:
# Создаем меню и проверяем ответ
menu = {
"title": "Menu",
"description": "main menu",
}
code, rspn = await self.Menu.write(client, menu)
menu.update(rspn)
# Создаем и проверяем подменю
submenu = {
"title": "Submenu",
"description": "submenu",
"parent_menu": menu["id"],
}
code, rspn = await self.Submenu.write(client, menu, submenu)
submenu.update(rspn)
# Добавляем блюдо
dish = {
"title": "dish",
"description": "some dish",
"price": "12.5",
"parent_submenu": submenu["id"],
}
code, rspn = await self.Dish.write(client, menu, submenu, dish)
dish.update(rspn)
# Удаляем подменю # Удаляем подменю
code = await self.Dish.delete(client, menu, submenu, dish) code = await self.Dish.delete(client, menu, submenu, dish)
assert code == 200 assert code == 200
# Проверяем меню
code, rspn = await self.Menu.get(client, menu)
assert code == 200
assert rspn["dishes_count"] == 0
# Проверяем подменю на наличие блюд
code, rspn = await self.Submenu.get(client, menu, submenu)
assert code == 200
assert rspn["dishes_count"] == 0
# Проверяем удаленное блюдо # Проверяем удаленное блюдо
code, rspn = await self.Dish.get(client, menu, submenu, dish) code, rspn = await self.Dish.get(client, menu, submenu, dish)
assert code == 404 assert code == 404
@ -309,6 +483,28 @@ class TestBaseCrud:
class TestСontinuity: class TestСontinuity:
@pytest.mark.asyncio
async def test_01(self, client, session_data: Dict):
"""Проверяет создание меню"""
data = {"title": "Menu", "description": "some"}
code, rspn = await TestBaseCrud.Menu.write(client, data)
assert code == 201
code, rspn = await TestBaseCrud.Menu.get(client, rspn)
session_data["target_menu_id"] = rspn.get("id")
session_data["target_menu_title"] = rspn.get("title")
session_data["target_menu_description"] = rspn.get("description")
assert code == 200
assert "id" in rspn
assert "title" in rspn
assert "description" in rspn
assert "submenus_count" in rspn
assert "dishes_count" in rspn
assert rspn["title"] == "Menu"
assert rspn.get("description") == "some"
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_postman_continuity(self, client): async def test_postman_continuity(self, client):
# Создаем меню # Создаем меню