reverse_url в тестах

develop
Сергей Ванюшкин 2024-02-04 18:40:58 +00:00
parent 09d0627d70
commit 291c61f873
2 changed files with 76 additions and 18 deletions

View File

@ -1,5 +1,7 @@
from httpx import AsyncClient, Response from httpx import AsyncClient, Response
from .urls import reverse_url
class Repository: class Repository:
class Menu: class Menu:
@ -7,26 +9,28 @@ class Repository:
async def read_all(ac: AsyncClient) -> tuple[int, dict]: async def read_all(ac: AsyncClient) -> tuple[int, dict]:
"""чтение всех меню""" """чтение всех меню"""
response: Response = await ac.get('/') response: Response = await ac.get(reverse_url('menus'))
return response.status_code, response.json() return response.status_code, response.json()
@staticmethod @staticmethod
async def get(ac: AsyncClient, data: dict) -> tuple[int, dict]: async def get(ac: AsyncClient, data: dict) -> tuple[int, dict]:
"""Получение меню по id""" """Получение меню по id"""
response: Response = await ac.get(f"/{data.get('id')}") response: Response = await ac.get(
reverse_url('menu', menu_id=data.get('id'))
)
return response.status_code, response.json() return response.status_code, response.json()
@staticmethod @staticmethod
async def write(ac: AsyncClient, data: dict) -> tuple[int, dict]: async def write(ac: AsyncClient, data: dict) -> tuple[int, dict]:
"""создания меню""" """создания меню"""
response: Response = await ac.post('/', json=data) response: Response = await ac.post(reverse_url('menus'), json=data)
return response.status_code, response.json() return response.status_code, response.json()
@staticmethod @staticmethod
async def update(ac: AsyncClient, data: dict) -> tuple[int, dict]: async def update(ac: AsyncClient, data: dict) -> tuple[int, dict]:
"""Обновление меню по id""" """Обновление меню по id"""
response: Response = await ac.patch( response: Response = await ac.patch(
f"/{data.get('id')}", reverse_url('menu', menu_id=data.get('id')),
json=data, json=data,
) )
return response.status_code, response.json() return response.status_code, response.json()
@ -34,14 +38,18 @@ class Repository:
@staticmethod @staticmethod
async def delete(ac: AsyncClient, data: dict) -> int: async def delete(ac: AsyncClient, data: dict) -> int:
"""Удаление меню по id""" """Удаление меню по id"""
response: Response = await ac.delete(f"/{data.get('id')}") response: Response = await ac.delete(
reverse_url('menu', menu_id=data.get('id')),
)
return response.status_code return response.status_code
class Submenu: class Submenu:
@staticmethod @staticmethod
async def read_all(ac: AsyncClient, menu: dict) -> tuple[int, dict]: async def read_all(ac: AsyncClient, menu: dict) -> tuple[int, dict]:
"""чтение всех меню""" """чтение всех меню"""
response: Response = await ac.get(f"/{menu.get('id')}/submenus/") response: Response = await ac.get(
reverse_url('submenus', menu_id=menu.get('id')),
)
return response.status_code, response.json() return response.status_code, response.json()
@staticmethod @staticmethod
@ -52,7 +60,11 @@ class Repository:
) -> tuple[int, dict]: ) -> tuple[int, dict]:
"""Получение меню по id""" """Получение меню по id"""
response: Response = await ac.get( response: Response = await ac.get(
f"/{menu.get('id')}/submenus/{submenu.get('id')}", reverse_url(
'submenu',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
),
) )
return response.status_code, response.json() return response.status_code, response.json()
@ -64,7 +76,7 @@ class Repository:
) -> tuple[int, dict]: ) -> tuple[int, dict]:
"""создания меню""" """создания меню"""
response: Response = await ac.post( response: Response = await ac.post(
f"/{menu.get('id')}/submenus/", reverse_url('submenu', menu_id=menu.get('id')),
json=submenu, json=submenu,
) )
return response.status_code, response.json() return response.status_code, response.json()
@ -75,7 +87,11 @@ class Repository:
) -> tuple[int, dict]: ) -> tuple[int, dict]:
"""Обновление меню по id""" """Обновление меню по id"""
response: Response = await ac.patch( response: Response = await ac.patch(
f"/{menu.get('id')}/submenus/{submenu.get('id')}", reverse_url(
'submenu',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
),
json=submenu, json=submenu,
) )
return response.status_code, response.json() return response.status_code, response.json()
@ -84,7 +100,11 @@ class Repository:
async def delete(ac: AsyncClient, menu: dict, submenu: dict) -> int: async def delete(ac: AsyncClient, menu: dict, submenu: dict) -> int:
"""Удаление меню по id""" """Удаление меню по id"""
response: Response = await ac.delete( response: Response = await ac.delete(
f"/{menu.get('id')}/submenus/{submenu.get('id')}" reverse_url(
'submenu',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
),
) )
return response.status_code return response.status_code
@ -95,7 +115,11 @@ class Repository:
) -> tuple[int, dict]: ) -> tuple[int, dict]:
"""чтение всех блюд""" """чтение всех блюд"""
response: Response = await ac.get( response: Response = await ac.get(
f"/{menu.get('id')}/submenus/{submenu.get('id')}/dishes/", reverse_url(
'dishes',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
),
) )
return response.status_code, response.json() return response.status_code, response.json()
@ -105,8 +129,12 @@ class Repository:
) -> tuple[int, dict]: ) -> tuple[int, dict]:
"""Получение блюда по id""" """Получение блюда по id"""
response: Response = await ac.get( response: Response = await ac.get(
f"/{menu.get('id')}/submenus/{submenu.get('id')}" reverse_url(
f"/dishes/{dish.get('id')}", 'dish',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
dish_id=dish.get('id'),
),
) )
return response.status_code, response.json() return response.status_code, response.json()
@ -116,7 +144,11 @@ class Repository:
) -> tuple[int, dict]: ) -> tuple[int, dict]:
"""создания блюда""" """создания блюда"""
response: Response = await ac.post( response: Response = await ac.post(
f"/{menu.get('id')}/submenus/{submenu.get('id')}/dishes/", reverse_url(
'dishes',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
),
json=dish, json=dish,
) )
return response.status_code, response.json() return response.status_code, response.json()
@ -127,8 +159,12 @@ class Repository:
) -> tuple[int, dict]: ) -> tuple[int, dict]:
"""Обновление блюда по id""" """Обновление блюда по id"""
response: Response = await ac.patch( response: Response = await ac.patch(
f"/{menu.get('id')}/submenus/{submenu.get('id')}" reverse_url(
f"/dishes/{dish.get('id')}", 'dish',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
dish_id=dish.get('id'),
),
json=dish, json=dish,
) )
return response.status_code, response.json() return response.status_code, response.json()
@ -142,7 +178,11 @@ class Repository:
) -> int: ) -> int:
"""Удаление блюда по id""" """Удаление блюда по id"""
response: Response = await ac.delete( response: Response = await ac.delete(
f"/{menu.get('id')}/submenus/{submenu.get('id')}" reverse_url(
f"/dishes/{dish.get('id')}" 'dish',
menu_id=menu.get('id'),
submenu_id=submenu.get('id'),
dish_id=dish.get('id'),
),
) )
return response.status_code return response.status_code

18
tests/urls.py Normal file
View File

@ -0,0 +1,18 @@
def reverse_url(loc: str, **kwargs) -> str:
menu_pref = '/'
submenu_pref = menu_pref + str(kwargs.get('menu_id', '')) + '/submenus/'
dish_pref = submenu_pref + str(kwargs.get('submenu_id', '')) + '/dishes/'
match loc:
case 'menus':
return menu_pref
case 'menu':
return menu_pref + str(kwargs.get('menu_id', ''))
case 'submenus':
return submenu_pref
case 'submenu':
return submenu_pref + str(kwargs.get('submenu_id', ''))
case 'dishes':
return dish_pref
case 'dish':
return dish_pref + str(kwargs.get('dish_id', ''))
return menu_pref