From ed3d7d935201da5d2c9aa458fd745041fea3a41a Mon Sep 17 00:00:00 2001 From: pi3c Date: Sun, 11 Feb 2024 23:17:57 +0300 Subject: [PATCH] =?UTF-8?q?upd=20=D0=A0=D0=B0=D0=B7=D0=BD=D0=B5=D1=81=20?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D1=8B,=20=D1=83=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D1=8C=D1=88=D0=B8=D0=B2=20=D0=BF=D0=BE=D1=80=D1=82=D1=8F=D0=BD?= =?UTF-8?q?=D0=BA=D1=83=20upd=20=D0=A2=D0=B5=D1=81=D1=82=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20summary=20=D1=80=D0=BE=D1=83=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastfood/routers/dish.py | 4 +- fastfood/routers/menu.py | 4 +- fastfood/routers/submenu.py | 4 +- tests/repository.py | 8 + tests/test_api.py | 360 ------------------------------------ tests/test_dish.py | 174 +++++++++++++++++ tests/test_menu.py | 80 ++++++++ tests/test_submenu.py | 113 +++++++++++ tests/test_summary.py | 117 ++++++++++++ 9 files changed, 498 insertions(+), 366 deletions(-) delete mode 100644 tests/test_api.py create mode 100644 tests/test_dish.py create mode 100644 tests/test_menu.py create mode 100644 tests/test_submenu.py create mode 100644 tests/test_summary.py diff --git a/fastfood/routers/dish.py b/fastfood/routers/dish.py index 22b443f..e8d0594 100644 --- a/fastfood/routers/dish.py +++ b/fastfood/routers/dish.py @@ -63,7 +63,7 @@ async def get_dish( if not result: raise HTTPException( status_code=404, - detail=f'Блюдо c UUID={dish_id} не существует, доступ невозможен', + detail='dish not found', ) return result @@ -89,7 +89,7 @@ async def update_dish( if not result: raise HTTPException( status_code=404, - detail=f'Блюдо c UUID={dish_id} не существует, обновление невозможно', + detail='dish not found', ) return result diff --git a/fastfood/routers/menu.py b/fastfood/routers/menu.py index e1a82be..963f2a0 100644 --- a/fastfood/routers/menu.py +++ b/fastfood/routers/menu.py @@ -50,7 +50,7 @@ async def get_menu( if not result: raise HTTPException( status_code=404, - detail=f'Меню c UUID={menu_id} не существует, доступ невозможен', + detail='menu not found', ) return result @@ -72,7 +72,7 @@ async def update_menu( if not result: raise HTTPException( status_code=404, - detail=f'Меню c UUID={menu_id} не существует, Обновление невозможно', + detail='menu not found', ) return result diff --git a/fastfood/routers/submenu.py b/fastfood/routers/submenu.py index 67649dc..b52e523 100644 --- a/fastfood/routers/submenu.py +++ b/fastfood/routers/submenu.py @@ -59,7 +59,7 @@ async def get_submenu( if not result: raise HTTPException( status_code=404, - detail=f'Подменю c UUID={submenu_id} не существует, доступ невозможен', + detail='submenu not found', ) return result @@ -83,7 +83,7 @@ async def update_submenu( if not result: raise HTTPException( status_code=404, - detail=f'Gjlvеню c UUID={submenu_id} не существует, обновление невозможно', + detail='submenu not found', ) return result diff --git a/tests/repository.py b/tests/repository.py index e563f2a..5d8b929 100644 --- a/tests/repository.py +++ b/tests/repository.py @@ -186,3 +186,11 @@ class Repository: ), ) return response.status_code + + class Summary: + @staticmethod + async def read_summary(ac: AsyncClient) -> tuple[int, dict]: + """чтение summary""" + + response: Response = await ac.get(reverse('get_summary')) + return response.status_code, response.json() diff --git a/tests/test_api.py b/tests/test_api.py deleted file mode 100644 index d57f408..0000000 --- a/tests/test_api.py +++ /dev/null @@ -1,360 +0,0 @@ -import pytest -from httpx import AsyncClient - -from .repository import Repository as Repo - - -@pytest.mark.asyncio -async def test_menu_crud_empty(client: AsyncClient) -> None: - """Тестирование функций меню""" - code, rspn = await Repo.Menu.read_all(client) - assert code == 200 - assert rspn == [] - - -@pytest.mark.asyncio -async def test_menu_crud_add(client: AsyncClient) -> None: - """Тестирование функций меню""" - data = {'title': 'Menu', 'description': None} - code, rspn = await Repo.Menu.write(client, data) - assert code == 201 - assert rspn['title'] == 'Menu' - assert rspn['description'] is None - await Repo.Menu.delete(client, rspn) - - -@pytest.mark.asyncio -async def test_menu_crud_get(client: AsyncClient) -> None: - """Тестирование функций меню""" - data = {'title': 'Menu', 'description': None} - code, rspn = await Repo.Menu.write(client, data) - code, menu = await Repo.Menu.get(client, {'id': rspn.get('id')}) - assert code == 200 - assert menu['title'] == rspn['title'] - await Repo.Menu.delete(client, menu) - - -@pytest.mark.asyncio -async def test_menu_crud_update(client: AsyncClient) -> None: - """Тестирование функций меню""" - data = {'title': 'Menu', 'description': None} - code, rspn = await Repo.Menu.write(client, data) - - upd_data = { - 'id': rspn.get('id'), - 'title': 'upd Menu', - 'description': '', - } - code, upd_rspn = await Repo.Menu.update(client, upd_data) - assert code == 200 - assert upd_rspn['title'] == 'upd Menu' - await Repo.Menu.delete(client, upd_rspn) - - -@pytest.mark.asyncio -async def test_menu_crud_delete(client: AsyncClient) -> None: - """Тестирование функций меню""" - data = {'title': 'Menu', 'description': None} - code, rspn = await Repo.Menu.write(client, data) - - code = await Repo.Menu.delete(client, rspn) - assert code == 200 - - code, rspn = await Repo.Menu.get(client, {'id': rspn.get('id')}) - assert code == 404 - - -@pytest.mark.asyncio -async def test_menu_crud_get_all(client: AsyncClient) -> None: - """Тестирование функций меню""" - code, rspn = await Repo.Menu.read_all(client) - assert code == 200 - assert rspn == [] - - data = {'title': 'Menu', 'description': None} - code, rspn = await Repo.Menu.write(client, data) - - code, upd_rspn = await Repo.Menu.read_all(client) - assert code == 200 - assert upd_rspn == [rspn] - await Repo.Menu.delete(client, rspn) - - -@pytest.mark.asyncio -async def test_submenus_get_all(client) -> None: - # Создаем меню и проверяем ответ - menu = {'title': 'Menu', 'description': 'main menu'} - code, rspn = await Repo.Menu.write(client, menu) - assert code == 201 - menu.update(rspn) - - # Проверяем наличие подменю - code, rspn = await Repo.Submenu.read_all(client, menu) - assert code == 200 - assert rspn == [] - - # Создаем и проверяем подменю - submenu = { - 'title': 'Submenu', - 'description': 'submenu', - 'parent_menu': menu['id'], - } - code, rspn = await Repo.Submenu.write(client, menu, submenu) - submenu.update(rspn) - - # Проверяем наличие подменю - code, upd_rspn = await Repo.Submenu.read_all(client, menu) - assert code == 200 - assert upd_rspn == [rspn] - - # удаляем сопутствующее - await Repo.Submenu.delete(client, menu, submenu) - await Repo.Menu.delete(client, menu) - - -@pytest.mark.asyncio -async def test_submenus_add(client) -> None: - # Создаем меню и проверяем ответ - menu = {'title': 'Menu', 'description': 'main menu'} - code, rspn = await Repo.Menu.write(client, menu) - menu.update(rspn) - - # Создаем и проверяем подменю - submenu = { - 'title': 'Submenu', - 'description': 'submenu', - 'parent_menu': menu['id'], - } - code, rspn = await Repo.Submenu.write(client, menu, submenu) - assert code == 201 - submenu.update(rspn) - - # удаляем сопутствующее - await Repo.Submenu.delete(client, menu, submenu) - await Repo.Menu.delete(client, menu) - - -@pytest.mark.asyncio -async def test_submenus_update(client) -> None: - # Создаем меню и проверяем ответ - menu = {'title': 'Menu', 'description': 'main menu'} - code, rspn = await Repo.Menu.write(client, menu) - menu.update(rspn) - - # Создаем и проверяем подменю - submenu = { - 'title': 'Submenu', - 'description': 'submenu', - 'parent_menu': menu['id'], - } - code, rspn = await Repo.Submenu.write(client, menu, submenu) - submenu.update(rspn) - - # Обновляем подменю и проверяем - submenu['title'] = 'updated_submenu' - code, rspn = await Repo.Submenu.update(client, menu, submenu) - assert code == 200 - assert submenu['title'] == rspn['title'] - submenu.update(rspn) - - # удаляем сопутствующее - await Repo.Submenu.delete(client, menu, submenu) - await Repo.Menu.delete(client, menu) - - -@pytest.mark.asyncio -async def test_submenus_delete(client) -> None: - # Создаем меню и проверяем ответ - menu = {'title': 'Menu', 'description': 'main menu'} - code, rspn = await Repo.Menu.write(client, menu) - menu.update(rspn) - - # Создаем и проверяем подменю - submenu = { - 'title': 'Submenu', - 'description': 'submenu', - 'parent_menu': menu['id'], - } - code, rspn = await Repo.Submenu.write(client, menu, submenu) - submenu.update(rspn) - - # Удаляем подменю - code = await Repo.Submenu.delete(client, menu, submenu) - assert code == 200 - - # Проверяем удаленное подменю - code, rspn = await Repo.Submenu.get(client, menu, submenu) - assert code == 404 - - # удаляем сопутствующее - await Repo.Menu.delete(client, menu) - - -@pytest.mark.asyncio -async def test_dishes_get_all(client: AsyncClient) -> None: - # Создаем меню и проверяем ответ - menu = { - 'title': 'Menu', - 'description': 'main menu', - } - code, rspn = await Repo.Menu.write(client, menu) - menu.update(rspn) - - # Создаем и проверяем подменю - submenu = { - 'title': 'Submenu', - 'description': 'submenu', - 'parent_menu': menu['id'], - } - code, rspn = await Repo.Submenu.write(client, menu, submenu) - submenu.update(rspn) - - # Проверяем все блюда в подменю - code, rspn = await Repo.Dish.read_all(client, menu, submenu) - assert code == 200 - assert rspn == [] - - # Добавляем блюдо - dish = { - 'title': 'dish', - 'description': 'some dish', - 'price': '12.5', - 'parent_submenu': submenu['id'], - } - code, rspn = await Repo.Dish.write(client, menu, submenu, dish) - assert code == 201 - dish.update(rspn) - - code, upd_rspn = await Repo.Dish.read_all(client, menu, submenu) - - assert code == 200 - - # удаляем сопутствующее - await Repo.Dish.delete(client, menu, submenu, dish) - await Repo.Submenu.delete(client, menu, submenu) - await Repo.Menu.delete(client, menu) - - -@pytest.mark.asyncio -async def test_dishes_add(client: AsyncClient) -> None: - # Создаем меню и проверяем ответ - menu = { - 'title': 'Menu', - 'description': 'main menu', - } - code, rspn = await Repo.Menu.write(client, menu) - menu.update(rspn) - - # Создаем и проверяем подменю - submenu = { - 'title': 'Submenu', - 'description': 'submenu', - 'parent_menu': menu['id'], - } - code, rspn = await Repo.Submenu.write(client, menu, submenu) - submenu.update(rspn) - - # Добавляем блюдо - dish = { - 'title': 'dish', - 'description': 'some dish', - 'price': '12.5', - 'parent_submenu': submenu['id'], - } - code, rspn = await Repo.Dish.write(client, menu, submenu, dish) - assert code == 201 - dish.update(rspn) - - # Получаем блюдо - code, rspn = await Repo.Dish.get(client, menu, submenu, dish) - assert code == 200 - assert rspn['title'] == dish['title'] - - # удаляем сопутствующее - await Repo.Dish.delete(client, menu, submenu, dish) - await Repo.Submenu.delete(client, menu, submenu) - await Repo.Menu.delete(client, menu) - - -@pytest.mark.asyncio -async def test_dishes_update(client: AsyncClient) -> None: - # Создаем меню и проверяем ответ - menu = { - 'title': 'Menu', - 'description': 'main menu', - } - code, rspn = await Repo.Menu.write(client, menu) - menu.update(rspn) - - # Создаем и проверяем подменю - submenu = { - 'title': 'Submenu', - 'description': 'submenu', - 'parent_menu': menu['id'], - } - code, rspn = await Repo.Submenu.write(client, menu, submenu) - submenu.update(rspn) - - # Добавляем блюдо - dish = { - 'title': 'dish', - 'description': 'some dish', - 'price': '12.5', - 'parent_submenu': submenu['id'], - } - code, rspn = await Repo.Dish.write(client, menu, submenu, dish) - dish.update(rspn) - - # Обновляем блюдо и проверяем - dish['title'] = 'updated_dish' - code, rspn = await Repo.Dish.update(client, menu, submenu, dish) - assert code == 200 - assert dish['title'] == rspn['title'] - dish.update(rspn) - - # удаляем сопутствующее - await Repo.Dish.delete(client, menu, submenu, dish) - await Repo.Submenu.delete(client, menu, submenu) - await Repo.Menu.delete(client, menu) - - -@pytest.mark.asyncio -async def test_dishes_delete(client: AsyncClient) -> None: - # Создаем меню и проверяем ответ - menu = { - 'title': 'Menu', - 'description': 'main menu', - } - code, rspn = await Repo.Menu.write(client, menu) - menu.update(rspn) - - # Создаем и проверяем подменю - submenu = { - 'title': 'Submenu', - 'description': 'submenu', - 'parent_menu': menu['id'], - } - code, rspn = await Repo.Submenu.write(client, menu, submenu) - submenu.update(rspn) - - # Добавляем блюдо - dish = { - 'title': 'dish', - 'description': 'some dish', - 'price': '12.5', - 'parent_submenu': submenu['id'], - } - code, rspn = await Repo.Dish.write(client, menu, submenu, dish) - dish.update(rspn) - - # Удаляем подменю - code = await Repo.Dish.delete(client, menu, submenu, dish) - assert code == 200 - - # Проверяем удаленное блюдо - code, rspn = await Repo.Dish.get(client, menu, submenu, dish) - assert code == 404 - - # удаляем сопутствующее - await Repo.Submenu.delete(client, menu, submenu) - await Repo.Menu.delete(client, menu) diff --git a/tests/test_dish.py b/tests/test_dish.py new file mode 100644 index 0000000..c1bfabe --- /dev/null +++ b/tests/test_dish.py @@ -0,0 +1,174 @@ +import pytest +from httpx import AsyncClient + +from .repository import Repository as Repo + + +@pytest.mark.asyncio +async def test_dishes_get_all(client: AsyncClient) -> None: + # Создаем меню и проверяем ответ + menu = { + 'title': 'Menu', + 'description': 'main menu', + } + code, rspn = await Repo.Menu.write(client, menu) + menu.update(rspn) + + # Создаем и проверяем подменю + submenu = { + 'title': 'Submenu', + 'description': 'submenu', + 'parent_menu': menu['id'], + } + code, rspn = await Repo.Submenu.write(client, menu, submenu) + submenu.update(rspn) + + # Проверяем все блюда в подменю + code, rspn = await Repo.Dish.read_all(client, menu, submenu) + assert code == 200 + assert rspn == [] + + # Добавляем блюдо + dish = { + 'title': 'dish', + 'description': 'some dish', + 'price': '12.5', + 'parent_submenu': submenu['id'], + } + code, rspn = await Repo.Dish.write(client, menu, submenu, dish) + assert code == 201 + dish.update(rspn) + + code, upd_rspn = await Repo.Dish.read_all(client, menu, submenu) + + assert code == 200 + + # удаляем сопутствующее + await Repo.Dish.delete(client, menu, submenu, dish) + await Repo.Submenu.delete(client, menu, submenu) + await Repo.Menu.delete(client, menu) + + +@pytest.mark.asyncio +async def test_dishes_add(client: AsyncClient) -> None: + # Создаем меню и проверяем ответ + menu = { + 'title': 'Menu', + 'description': 'main menu', + } + code, rspn = await Repo.Menu.write(client, menu) + menu.update(rspn) + + # Создаем и проверяем подменю + submenu = { + 'title': 'Submenu', + 'description': 'submenu', + 'parent_menu': menu['id'], + } + code, rspn = await Repo.Submenu.write(client, menu, submenu) + submenu.update(rspn) + + # Добавляем блюдо + dish = { + 'title': 'dish', + 'description': 'some dish', + 'price': '12.5', + 'parent_submenu': submenu['id'], + } + code, rspn = await Repo.Dish.write(client, menu, submenu, dish) + assert code == 201 + dish.update(rspn) + + # Получаем блюдо + code, rspn = await Repo.Dish.get(client, menu, submenu, dish) + assert code == 200 + assert rspn['title'] == dish['title'] + + # удаляем сопутствующее + await Repo.Dish.delete(client, menu, submenu, dish) + await Repo.Submenu.delete(client, menu, submenu) + await Repo.Menu.delete(client, menu) + + +@pytest.mark.asyncio +async def test_dishes_update(client: AsyncClient) -> None: + # Создаем меню и проверяем ответ + menu = { + 'title': 'Menu', + 'description': 'main menu', + } + code, rspn = await Repo.Menu.write(client, menu) + menu.update(rspn) + + # Создаем и проверяем подменю + submenu = { + 'title': 'Submenu', + 'description': 'submenu', + 'parent_menu': menu['id'], + } + code, rspn = await Repo.Submenu.write(client, menu, submenu) + submenu.update(rspn) + + # Добавляем блюдо + dish = { + 'title': 'dish', + 'description': 'some dish', + 'price': '12.5', + 'parent_submenu': submenu['id'], + } + code, rspn = await Repo.Dish.write(client, menu, submenu, dish) + dish.update(rspn) + + # Обновляем блюдо и проверяем + dish['title'] = 'updated_dish' + code, rspn = await Repo.Dish.update(client, menu, submenu, dish) + assert code == 200 + assert dish['title'] == rspn['title'] + dish.update(rspn) + + # удаляем сопутствующее + await Repo.Dish.delete(client, menu, submenu, dish) + await Repo.Submenu.delete(client, menu, submenu) + await Repo.Menu.delete(client, menu) + + +@pytest.mark.asyncio +async def test_dishes_delete(client: AsyncClient) -> None: + # Создаем меню и проверяем ответ + menu = { + 'title': 'Menu', + 'description': 'main menu', + } + code, rspn = await Repo.Menu.write(client, menu) + menu.update(rspn) + + # Создаем и проверяем подменю + submenu = { + 'title': 'Submenu', + 'description': 'submenu', + 'parent_menu': menu['id'], + } + code, rspn = await Repo.Submenu.write(client, menu, submenu) + submenu.update(rspn) + + # Добавляем блюдо + dish = { + 'title': 'dish', + 'description': 'some dish', + 'price': '12.5', + 'parent_submenu': submenu['id'], + } + code, rspn = await Repo.Dish.write(client, menu, submenu, dish) + dish.update(rspn) + + # Удаляем подменю + code = await Repo.Dish.delete(client, menu, submenu, dish) + assert code == 200 + + # Проверяем удаленное блюдо + code, rspn = await Repo.Dish.get(client, menu, submenu, dish) + assert code == 404 + + # удаляем сопутствующее + await Repo.Submenu.delete(client, menu, submenu) + await Repo.Menu.delete(client, menu) diff --git a/tests/test_menu.py b/tests/test_menu.py new file mode 100644 index 0000000..4668944 --- /dev/null +++ b/tests/test_menu.py @@ -0,0 +1,80 @@ +import pytest +from httpx import AsyncClient + +from .repository import Repository as Repo + + +@pytest.mark.asyncio +async def test_menu_crud_empty(client: AsyncClient) -> None: + """Тестирование функций меню""" + code, rspn = await Repo.Menu.read_all(client) + assert code == 200 + assert rspn == [] + + +@pytest.mark.asyncio +async def test_menu_crud_add(client: AsyncClient) -> None: + """Тестирование функций меню""" + data = {'title': 'Menu', 'description': None} + code, rspn = await Repo.Menu.write(client, data) + assert code == 201 + assert rspn['title'] == 'Menu' + assert rspn['description'] is None + await Repo.Menu.delete(client, rspn) + + +@pytest.mark.asyncio +async def test_menu_crud_get(client: AsyncClient) -> None: + """Тестирование функций меню""" + data = {'title': 'Menu', 'description': None} + code, rspn = await Repo.Menu.write(client, data) + code, menu = await Repo.Menu.get(client, {'id': rspn.get('id')}) + assert code == 200 + assert menu['title'] == rspn['title'] + await Repo.Menu.delete(client, menu) + + +@pytest.mark.asyncio +async def test_menu_crud_update(client: AsyncClient) -> None: + """Тестирование функций меню""" + data = {'title': 'Menu', 'description': None} + code, rspn = await Repo.Menu.write(client, data) + + upd_data = { + 'id': rspn.get('id'), + 'title': 'upd Menu', + 'description': '', + } + code, upd_rspn = await Repo.Menu.update(client, upd_data) + assert code == 200 + assert upd_rspn['title'] == 'upd Menu' + await Repo.Menu.delete(client, upd_rspn) + + +@pytest.mark.asyncio +async def test_menu_crud_delete(client: AsyncClient) -> None: + """Тестирование функций меню""" + data = {'title': 'Menu', 'description': None} + code, rspn = await Repo.Menu.write(client, data) + + code = await Repo.Menu.delete(client, rspn) + assert code == 200 + + code, rspn = await Repo.Menu.get(client, {'id': rspn.get('id')}) + assert code == 404 + + +@pytest.mark.asyncio +async def test_menu_crud_get_all(client: AsyncClient) -> None: + """Тестирование функций меню""" + code, rspn = await Repo.Menu.read_all(client) + assert code == 200 + assert rspn == [] + + data = {'title': 'Menu', 'description': None} + code, rspn = await Repo.Menu.write(client, data) + + code, upd_rspn = await Repo.Menu.read_all(client) + assert code == 200 + assert upd_rspn == [rspn] + await Repo.Menu.delete(client, rspn) diff --git a/tests/test_submenu.py b/tests/test_submenu.py new file mode 100644 index 0000000..d3c8d56 --- /dev/null +++ b/tests/test_submenu.py @@ -0,0 +1,113 @@ +import pytest + +from .repository import Repository as Repo + + +@pytest.mark.asyncio +async def test_submenus_get_all(client) -> None: + # Создаем меню и проверяем ответ + menu = {'title': 'Menu', 'description': 'main menu'} + code, rspn = await Repo.Menu.write(client, menu) + assert code == 201 + menu.update(rspn) + + # Проверяем наличие подменю + code, rspn = await Repo.Submenu.read_all(client, menu) + assert code == 200 + assert rspn == [] + + # Создаем и проверяем подменю + submenu = { + 'title': 'Submenu', + 'description': 'submenu', + 'parent_menu': menu['id'], + } + code, rspn = await Repo.Submenu.write(client, menu, submenu) + submenu.update(rspn) + + # Проверяем наличие подменю + code, upd_rspn = await Repo.Submenu.read_all(client, menu) + assert code == 200 + assert upd_rspn == [rspn] + + # удаляем сопутствующее + await Repo.Submenu.delete(client, menu, submenu) + await Repo.Menu.delete(client, menu) + + +@pytest.mark.asyncio +async def test_submenus_add(client) -> None: + # Создаем меню и проверяем ответ + menu = {'title': 'Menu', 'description': 'main menu'} + code, rspn = await Repo.Menu.write(client, menu) + menu.update(rspn) + + # Создаем и проверяем подменю + submenu = { + 'title': 'Submenu', + 'description': 'submenu', + 'parent_menu': menu['id'], + } + code, rspn = await Repo.Submenu.write(client, menu, submenu) + assert code == 201 + submenu.update(rspn) + + # удаляем сопутствующее + await Repo.Submenu.delete(client, menu, submenu) + await Repo.Menu.delete(client, menu) + + +@pytest.mark.asyncio +async def test_submenus_update(client) -> None: + # Создаем меню и проверяем ответ + menu = {'title': 'Menu', 'description': 'main menu'} + code, rspn = await Repo.Menu.write(client, menu) + menu.update(rspn) + + # Создаем и проверяем подменю + submenu = { + 'title': 'Submenu', + 'description': 'submenu', + 'parent_menu': menu['id'], + } + code, rspn = await Repo.Submenu.write(client, menu, submenu) + submenu.update(rspn) + + # Обновляем подменю и проверяем + submenu['title'] = 'updated_submenu' + code, rspn = await Repo.Submenu.update(client, menu, submenu) + assert code == 200 + assert submenu['title'] == rspn['title'] + submenu.update(rspn) + + # удаляем сопутствующее + await Repo.Submenu.delete(client, menu, submenu) + await Repo.Menu.delete(client, menu) + + +@pytest.mark.asyncio +async def test_submenus_delete(client) -> None: + # Создаем меню и проверяем ответ + menu = {'title': 'Menu', 'description': 'main menu'} + code, rspn = await Repo.Menu.write(client, menu) + menu.update(rspn) + + # Создаем и проверяем подменю + submenu = { + 'title': 'Submenu', + 'description': 'submenu', + 'parent_menu': menu['id'], + } + code, rspn = await Repo.Submenu.write(client, menu, submenu) + submenu.update(rspn) + + # Удаляем подменю + code = await Repo.Submenu.delete(client, menu, submenu) + assert code == 200 + + # Проверяем удаленное подменю + code, rspn = await Repo.Submenu.get(client, menu, submenu) + assert code == 404 + + # удаляем сопутствующее + await Repo.Menu.delete(client, menu) diff --git a/tests/test_summary.py b/tests/test_summary.py new file mode 100644 index 0000000..f37ddd6 --- /dev/null +++ b/tests/test_summary.py @@ -0,0 +1,117 @@ +import pytest +from httpx import AsyncClient + +from .repository import Repository as Repo + + +@pytest.mark.asyncio +async def test_summary_with_menu(client: AsyncClient) -> None: + # Проверяем пустое summary + code, rspn = await Repo.Summary.read_summary(client) + assert code == 200 + assert rspn == [] + + # Создаем меню и проверяем ответ + menu = { + 'title': 'Menu', + 'description': 'main menu', + } + code, rspn = await Repo.Menu.write(client, menu) + menu.update(rspn) + + # Удалим ненужные ключи, тк в модели они не используются + del menu['submenus_count'] + del menu['dishes_count'] + menu.__setattr__('submenus', list()) + + # Проверяем summary c меню + code, rspn = await Repo.Summary.read_summary(client) + assert code == 200 + assert rspn == [menu] + + # удаляем сопутствующее + await Repo.Menu.delete(client, menu) + + +@pytest.mark.asyncio +async def test_summary_with_submenus(client: AsyncClient) -> None: + # Создаем меню и проверяем ответ + menu = { + 'title': 'Menu', + 'description': 'main menu', + } + code, rspn = await Repo.Menu.write(client, menu) + menu.update(rspn) + + del menu['submenus_count'] + del menu['dishes_count'] + menu.__setattr__('submenus', list()) + + # Создаем и проверяем подменю + submenu = { + 'title': 'Submenu', + 'description': 'submenu', + 'parent_menu': menu['id'], + } + code, rspn = await Repo.Submenu.write(client, menu, submenu) + submenu.update(rspn) + submenu.__setattr__('dishes', list()) + del submenu['dishes_count'] + del submenu['parent_menu'] + + menu.__setattr__('submenus', [submenu]) + + # Получаем блюдо + code, rspn = await Repo.Summary.read_summary(client) + assert code == 200 + assert rspn == [menu] + + await Repo.Menu.delete(client, menu) + + +@pytest.mark.asyncio +async def test_summary_with_dishes(client: AsyncClient) -> None: + # Создаем меню и проверяем ответ + menu = { + 'title': 'Menu', + 'description': 'main menu', + } + code, rspn = await Repo.Menu.write(client, menu) + menu.update(rspn) + + del menu['submenus_count'] + del menu['dishes_count'] + menu.__setattr__('submenus', list()) + + # Создаем и проверяем подменю + submenu = { + 'title': 'Submenu', + 'description': 'submenu', + 'parent_menu': menu['id'], + } + code, rspn = await Repo.Submenu.write(client, menu, submenu) + submenu.update(rspn) + submenu.__setattr__('dishes', list()) + del submenu['dishes_count'] + del submenu['parent_menu'] + + # Добавляем блюдо + dish = { + 'title': 'dish', + 'description': 'some dish', + 'price': '12.5', + 'parent_submenu': submenu['id'], + } + code, rspn = await Repo.Dish.write(client, menu, submenu, dish) + dish.update(rspn) + del dish['parent_submenu'] + del dish['id'] + + submenu.__setattr__('dishes', dish) + menu.__setattr__('submenus', submenu) + + code, rspn = await Repo.Summary.read_summary(client) + assert code == 200 + assert rspn == [menu] + + await Repo.Menu.delete(client, menu)