2024-01-26 00:23:38 +03:00
|
|
|
|
import pytest
|
|
|
|
|
from httpx import AsyncClient
|
|
|
|
|
|
|
|
|
|
url = "http://localhost:8000/api/v1/menus"
|
|
|
|
|
|
|
|
|
|
|
2024-01-26 12:52:35 +03:00
|
|
|
|
class TestCrud:
|
2024-01-27 08:55:09 +03:00
|
|
|
|
class Menu:
|
|
|
|
|
@staticmethod
|
2024-01-27 15:48:20 +03:00
|
|
|
|
async def read_all(ac):
|
2024-01-27 08:55:09 +03:00
|
|
|
|
"""чтение всех меню"""
|
2024-01-27 15:48:20 +03:00
|
|
|
|
response = await ac.get("/")
|
|
|
|
|
return response.status_code, response.json()
|
2024-01-27 08:55:09 +03:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
2024-01-27 15:48:20 +03:00
|
|
|
|
async def get(ac, data):
|
2024-01-27 08:55:09 +03:00
|
|
|
|
"""Получение меню по id"""
|
2024-01-27 15:48:20 +03:00
|
|
|
|
response = await ac.get(f"/{data.get('id')}")
|
|
|
|
|
return response.status_code, response.json()
|
|
|
|
|
|
2024-01-27 08:55:09 +03:00
|
|
|
|
@staticmethod
|
2024-01-27 15:48:20 +03:00
|
|
|
|
async def write(ac, data):
|
2024-01-27 08:55:09 +03:00
|
|
|
|
"""создания меню"""
|
2024-01-27 15:48:20 +03:00
|
|
|
|
response = await ac.post("/", json=data)
|
|
|
|
|
return response.status_code, response.json()
|
|
|
|
|
|
2024-01-27 08:55:09 +03:00
|
|
|
|
@staticmethod
|
2024-01-27 15:48:20 +03:00
|
|
|
|
async def update(ac, data):
|
2024-01-27 08:55:09 +03:00
|
|
|
|
"""Обновление меню по id"""
|
2024-01-27 15:48:20 +03:00
|
|
|
|
response = await ac.patch(f"/{data.get('id')}", json=data)
|
|
|
|
|
return response.status_code, response.json()
|
2024-01-27 08:55:09 +03:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
2024-01-27 15:48:20 +03:00
|
|
|
|
async def delete(ac, data):
|
2024-01-27 08:55:09 +03:00
|
|
|
|
"""Удаление меню по id"""
|
2024-01-27 15:48:20 +03:00
|
|
|
|
response = await ac.delete(f"/{data.get('id')}")
|
2024-01-27 08:55:09 +03:00
|
|
|
|
|
|
|
|
|
class Submenu:
|
|
|
|
|
@staticmethod
|
2024-01-27 15:48:20 +03:00
|
|
|
|
async def read_all(ac, menu):
|
2024-01-27 08:55:09 +03:00
|
|
|
|
"""чтение всех меню"""
|
2024-01-27 15:48:20 +03:00
|
|
|
|
response = await ac.get(f"/{menu.get('id')}/submenus/")
|
|
|
|
|
return response.status_code, response.json()
|
2024-01-27 08:55:09 +03:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
2024-01-27 15:48:20 +03:00
|
|
|
|
async def get(ac, menu, submenu):
|
2024-01-27 08:55:09 +03:00
|
|
|
|
"""Получение меню по id"""
|
2024-01-27 15:48:20 +03:00
|
|
|
|
response = await ac.get(
|
|
|
|
|
f"/{menu.get('id')}/submenus/{submenu.get('id')}",
|
|
|
|
|
)
|
|
|
|
|
return response.status_code, response.json()
|
2024-01-27 08:55:09 +03:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
2024-01-27 15:48:20 +03:00
|
|
|
|
async def write(ac, menu, submenu):
|
2024-01-27 08:55:09 +03:00
|
|
|
|
"""создания меню"""
|
2024-01-27 15:48:20 +03:00
|
|
|
|
response = await ac.post(
|
|
|
|
|
f"/{menu.get('id')}/submenus/",
|
|
|
|
|
json=submenu,
|
|
|
|
|
)
|
|
|
|
|
return response.status_code, response.json()
|
2024-01-27 08:55:09 +03:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
2024-01-27 15:48:20 +03:00
|
|
|
|
async def update(ac, menu, submenu):
|
2024-01-27 08:55:09 +03:00
|
|
|
|
"""Обновление меню по id"""
|
2024-01-27 15:48:20 +03:00
|
|
|
|
response = await ac.patch(
|
|
|
|
|
f"/{menu.get('id')}/submenus/{submenu.get('id')}",
|
|
|
|
|
json=submenu,
|
|
|
|
|
)
|
|
|
|
|
return response.status_code, response.json()
|
2024-01-27 08:55:09 +03:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
2024-01-27 15:48:20 +03:00
|
|
|
|
async def delete(ac, menu, submenu):
|
2024-01-27 08:55:09 +03:00
|
|
|
|
"""Удаление меню по id"""
|
2024-01-27 15:48:20 +03:00
|
|
|
|
response = await ac.delete(
|
|
|
|
|
f"/{menu.get('id')}/submenus/{submenu.get('id')}"
|
|
|
|
|
)
|
2024-01-27 08:55:09 +03:00
|
|
|
|
|
2024-01-26 12:52:35 +03:00
|
|
|
|
@pytest.mark.asyncio
|
2024-01-27 15:48:20 +03:00
|
|
|
|
async def test_menu_crud(self, client):
|
2024-01-27 08:55:09 +03:00
|
|
|
|
"""Тестирование функций меню"""
|
2024-01-27 15:48:20 +03:00
|
|
|
|
code, rspn = await self.Menu.read_all(client)
|
2024-01-27 08:55:09 +03:00
|
|
|
|
assert code == 200
|
|
|
|
|
|
|
|
|
|
data = {"title": "Menu", "description": None}
|
2024-01-27 15:48:20 +03:00
|
|
|
|
code, rspn = await self.Menu.write(client, data)
|
2024-01-27 08:55:09 +03:00
|
|
|
|
assert code == 201
|
|
|
|
|
assert rspn["title"] == "Menu"
|
|
|
|
|
assert rspn["description"] is None
|
|
|
|
|
|
2024-01-27 15:48:20 +03:00
|
|
|
|
code, menu = await self.Menu.get(client, {"id": rspn.get("id")})
|
2024-01-27 08:55:09 +03:00
|
|
|
|
assert code == 200
|
|
|
|
|
assert menu["title"] == rspn["title"]
|
|
|
|
|
|
|
|
|
|
upd_data = {
|
2024-01-27 15:48:20 +03:00
|
|
|
|
"id": rspn.get("id"),
|
|
|
|
|
"title": "upd Menu",
|
|
|
|
|
"description": "",
|
2024-01-27 08:55:09 +03:00
|
|
|
|
}
|
2024-01-27 15:48:20 +03:00
|
|
|
|
code, upd_rspn = await self.Menu.update(client, upd_data)
|
2024-01-27 08:55:09 +03:00
|
|
|
|
assert code == 200
|
|
|
|
|
assert upd_rspn["title"] == "upd Menu"
|
|
|
|
|
|
2024-01-27 15:48:20 +03:00
|
|
|
|
code = await self.Menu.delete(client, rspn)
|
2024-01-27 08:55:09 +03:00
|
|
|
|
|
2024-01-27 15:48:20 +03:00
|
|
|
|
code, menu = await self.Menu.get(client, {"id": rspn.get("id")})
|
2024-01-27 08:55:09 +03:00
|
|
|
|
assert code == 404
|
2024-01-26 12:52:35 +03:00
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2024-01-27 15:48:20 +03:00
|
|
|
|
async def test_submenu(self, client):
|
|
|
|
|
# Создаем меню и проверяем ответ
|
2024-01-27 08:55:09 +03:00
|
|
|
|
menu = {"title": "Menu", "description": "main menu"}
|
2024-01-27 15:48:20 +03:00
|
|
|
|
code, rspn = await self.Menu.write(client, menu)
|
2024-01-27 08:55:09 +03:00
|
|
|
|
assert code == 201
|
|
|
|
|
menu.update(rspn)
|
2024-01-27 15:48:20 +03:00
|
|
|
|
|
|
|
|
|
# Проверяем наличие подменю
|
|
|
|
|
code, rspn = await self.Submenu.read_all(client, menu)
|
2024-01-27 08:55:09 +03:00
|
|
|
|
assert code == 200
|
|
|
|
|
assert rspn == []
|
2024-01-26 00:23:38 +03:00
|
|
|
|
|
2024-01-27 15:48:20 +03:00
|
|
|
|
# Создаем и проверяем подменю
|
|
|
|
|
submenu = {
|
|
|
|
|
"title": "Submenu",
|
|
|
|
|
"description": "submenu",
|
|
|
|
|
"parent_menu": menu["id"],
|
|
|
|
|
}
|
|
|
|
|
code, rspn = await self.Submenu.write(client, menu, submenu)
|
|
|
|
|
assert code == 201
|
|
|
|
|
submenu.update(rspn)
|
2024-01-26 00:23:38 +03:00
|
|
|
|
|
2024-01-27 15:48:20 +03:00
|
|
|
|
# Проверяем меню на наличие подменю
|
|
|
|
|
code, rspn = await self.Menu.get(client, menu)
|
|
|
|
|
assert code == 200
|
|
|
|
|
assert rspn["submenus_count"] == 1
|
|
|
|
|
|
|
|
|
|
# Обновляем подменю и проверяем
|
|
|
|
|
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 is None
|
|
|
|
|
|
|
|
|
|
# Проверяем меню
|
|
|
|
|
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.Menu.delete(client, menu)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# class TestСontinuity:
|
|
|
|
|
# @pytest.mark.asyncio
|
|
|
|
|
# async def test_postman_continuity(self, app):
|
|
|
|
|
# async with AsyncClient(app=app, base_url=url) as ac:
|
|
|
|
|
# pass
|