From 291c61f8736ca39b7beb6afb310d0455e4eb6774 Mon Sep 17 00:00:00 2001 From: Sergey Vanyushkin Date: Sun, 4 Feb 2024 18:40:58 +0000 Subject: [PATCH] =?UTF-8?q?reverse=5Furl=20=D0=B2=20=D1=82=D0=B5=D1=81?= =?UTF-8?q?=D1=82=D0=B0=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/repository.py | 76 ++++++++++++++++++++++++++++++++++----------- tests/urls.py | 18 +++++++++++ 2 files changed, 76 insertions(+), 18 deletions(-) create mode 100644 tests/urls.py diff --git a/tests/repository.py b/tests/repository.py index b1c9ce1..f4aca24 100644 --- a/tests/repository.py +++ b/tests/repository.py @@ -1,5 +1,7 @@ from httpx import AsyncClient, Response +from .urls import reverse_url + class Repository: class Menu: @@ -7,26 +9,28 @@ class Repository: 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() @staticmethod async def get(ac: AsyncClient, data: dict) -> tuple[int, dict]: """Получение меню по 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() @staticmethod 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() @staticmethod async def update(ac: AsyncClient, data: dict) -> tuple[int, dict]: """Обновление меню по id""" response: Response = await ac.patch( - f"/{data.get('id')}", + reverse_url('menu', menu_id=data.get('id')), json=data, ) return response.status_code, response.json() @@ -34,14 +38,18 @@ class Repository: @staticmethod async def delete(ac: AsyncClient, data: dict) -> int: """Удаление меню по 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 class Submenu: @staticmethod 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() @staticmethod @@ -52,7 +60,11 @@ class Repository: ) -> tuple[int, dict]: """Получение меню по id""" 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() @@ -64,7 +76,7 @@ class Repository: ) -> tuple[int, dict]: """создания меню""" response: Response = await ac.post( - f"/{menu.get('id')}/submenus/", + reverse_url('submenu', menu_id=menu.get('id')), json=submenu, ) return response.status_code, response.json() @@ -75,7 +87,11 @@ class Repository: ) -> tuple[int, dict]: """Обновление меню по id""" 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, ) return response.status_code, response.json() @@ -84,7 +100,11 @@ class Repository: async def delete(ac: AsyncClient, menu: dict, submenu: dict) -> int: """Удаление меню по id""" 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 @@ -95,7 +115,11 @@ class Repository: ) -> tuple[int, dict]: """чтение всех блюд""" 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() @@ -105,8 +129,12 @@ class Repository: ) -> tuple[int, dict]: """Получение блюда по id""" response: Response = await ac.get( - f"/{menu.get('id')}/submenus/{submenu.get('id')}" - f"/dishes/{dish.get('id')}", + reverse_url( + 'dish', + menu_id=menu.get('id'), + submenu_id=submenu.get('id'), + dish_id=dish.get('id'), + ), ) return response.status_code, response.json() @@ -116,7 +144,11 @@ class Repository: ) -> tuple[int, dict]: """создания блюда""" 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, ) return response.status_code, response.json() @@ -127,8 +159,12 @@ class Repository: ) -> tuple[int, dict]: """Обновление блюда по id""" response: Response = await ac.patch( - f"/{menu.get('id')}/submenus/{submenu.get('id')}" - f"/dishes/{dish.get('id')}", + 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() @@ -142,7 +178,11 @@ class Repository: ) -> int: """Удаление блюда по id""" response: Response = await ac.delete( - f"/{menu.get('id')}/submenus/{submenu.get('id')}" - f"/dishes/{dish.get('id')}" + reverse_url( + 'dish', + menu_id=menu.get('id'), + submenu_id=submenu.get('id'), + dish_id=dish.get('id'), + ), ) return response.status_code diff --git a/tests/urls.py b/tests/urls.py new file mode 100644 index 0000000..bc16268 --- /dev/null +++ b/tests/urls.py @@ -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