fastfood/tests/repository.py

189 lines
6.3 KiB
Python
Raw Normal View History

from httpx import AsyncClient, Response
2024-02-04 21:40:58 +03:00
from .urls import reverse_url
class Repository:
class Menu:
@staticmethod
async def read_all(ac: AsyncClient) -> tuple[int, dict]:
"""чтение всех меню"""
2024-02-04 21:40:58 +03:00
response: Response = await ac.get(reverse_url('menus'))
return response.status_code, response.json()
@staticmethod
async def get(ac: AsyncClient, data: dict) -> tuple[int, dict]:
"""Получение меню по id"""
2024-02-04 21:40:58 +03:00
response: Response = await ac.get(
reverse_url('menu', menu_id=data.get('id'))
)
return response.status_code, response.json()
@staticmethod
async def write(ac: AsyncClient, data: dict) -> tuple[int, dict]:
"""создания меню"""
2024-02-04 21:40:58 +03:00
response: Response = await ac.post(reverse_url('menus'), json=data)
return response.status_code, response.json()
@staticmethod
async def update(ac: AsyncClient, data: dict) -> tuple[int, dict]:
"""Обновление меню по id"""
response: Response = await ac.patch(
2024-02-04 21:40:58 +03:00
reverse_url('menu', menu_id=data.get('id')),
json=data,
)
return response.status_code, response.json()
@staticmethod
async def delete(ac: AsyncClient, data: dict) -> int:
"""Удаление меню по id"""
2024-02-04 21:40:58 +03:00
response: Response = await ac.delete(
reverse_url('menu', menu_id=data.get('id')),
)
return response.status_code
class Submenu:
@staticmethod
async def read_all(ac: AsyncClient, menu: dict) -> tuple[int, dict]:
"""чтение всех меню"""
2024-02-04 21:40:58 +03:00
response: Response = await ac.get(
reverse_url('submenus', menu_id=menu.get('id')),
)
return response.status_code, response.json()
@staticmethod
async def get(
ac: AsyncClient,
menu: dict,
submenu: dict,
) -> tuple[int, dict]:
"""Получение меню по id"""
response: Response = await ac.get(
2024-02-04 21:40:58 +03:00
reverse_url(
'submenu',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
),
)
return response.status_code, response.json()
@staticmethod
async def write(
ac: AsyncClient,
menu: dict,
submenu: dict,
) -> tuple[int, dict]:
"""создания меню"""
response: Response = await ac.post(
2024-02-04 21:40:58 +03:00
reverse_url('submenu', menu_id=menu.get('id')),
json=submenu,
)
return response.status_code, response.json()
@staticmethod
async def update(
ac: AsyncClient, menu: dict, submenu: dict
) -> tuple[int, dict]:
"""Обновление меню по id"""
response: Response = await ac.patch(
2024-02-04 21:40:58 +03:00
reverse_url(
'submenu',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
),
json=submenu,
)
return response.status_code, response.json()
@staticmethod
async def delete(ac: AsyncClient, menu: dict, submenu: dict) -> int:
"""Удаление меню по id"""
response: Response = await ac.delete(
2024-02-04 21:40:58 +03:00
reverse_url(
'submenu',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
),
)
return response.status_code
class Dish:
@staticmethod
async def read_all(
ac: AsyncClient, menu: dict, submenu: dict
) -> tuple[int, dict]:
"""чтение всех блюд"""
response: Response = await ac.get(
2024-02-04 21:40:58 +03:00
reverse_url(
'dishes',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
),
)
return response.status_code, response.json()
@staticmethod
async def get(
ac: AsyncClient, menu: dict, submenu: dict, dish: dict
) -> tuple[int, dict]:
"""Получение блюда по id"""
response: Response = await ac.get(
2024-02-04 21:40:58 +03:00
reverse_url(
'dish',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
dish_id=dish.get('id'),
),
)
return response.status_code, response.json()
@staticmethod
async def write(
ac: AsyncClient, menu: dict, submenu: dict, dish: dict
) -> tuple[int, dict]:
"""создания блюда"""
response: Response = await ac.post(
2024-02-04 21:40:58 +03:00
reverse_url(
'dishes',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
),
json=dish,
)
return response.status_code, response.json()
@staticmethod
async def update(
ac: AsyncClient, menu: dict, submenu: dict, dish: dict
) -> tuple[int, dict]:
"""Обновление блюда по id"""
response: Response = await ac.patch(
2024-02-04 21:40:58 +03:00
reverse_url(
'dish',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
dish_id=dish.get('id'),
),
json=dish,
)
return response.status_code, response.json()
@staticmethod
async def delete(
ac: AsyncClient,
menu: dict,
submenu: dict,
dish: dict,
) -> int:
"""Удаление блюда по id"""
response: Response = await ac.delete(
2024-02-04 21:40:58 +03:00
reverse_url(
'dish',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
dish_id=dish.get('id'),
),
)
return response.status_code