fastfood/tests/test_crud.py

180 lines
5.5 KiB
Python
Raw Normal View History

2024-01-29 02:19:02 +03:00
from uuid import UUID
2024-01-29 12:34:11 +03:00
import pytest
2024-01-29 02:19:02 +03:00
from sqlalchemy.ext.asyncio import AsyncSession
2024-01-29 12:34:11 +03:00
2024-01-31 01:05:28 +03:00
from fastfood.cruds.dish import DishCrud
2024-01-29 02:19:02 +03:00
from fastfood.cruds.menu import MenuCrud
2024-01-29 12:34:11 +03:00
from fastfood.cruds.submenu import SubMenuCrud
2024-01-31 01:05:28 +03:00
from fastfood.models import Dish, Menu, SubMenu
from fastfood.schemas import DishBase as dishschema
2024-01-29 02:19:02 +03:00
from fastfood.schemas import Menu as menuschema
from fastfood.schemas import MenuBase as menubaseschema
@pytest.mark.asyncio
async def test_menu(asession: AsyncSession) -> None:
async with asession:
2024-01-29 22:22:36 +03:00
# Создаем меню
2024-01-29 02:19:02 +03:00
menu: Menu = Menu(title="SomeMenu", description="SomeDescription")
2024-01-29 20:11:39 +03:00
menu: Menu = await MenuCrud.create_menu_item(
menubaseschema.model_validate(menu),
asession,
)
2024-01-29 02:19:02 +03:00
menu_id: UUID = menu.id
2024-01-29 22:22:36 +03:00
# Получаем его же
2024-01-29 02:19:02 +03:00
req_menu: Menu | None = await MenuCrud.get_menu_item(menu_id, asession)
assert menu == req_menu
2024-01-29 22:22:36 +03:00
# Получаем все меню и проверяем
2024-01-29 02:19:02 +03:00
req_menus = await MenuCrud.get_menus(asession)
2024-01-29 20:11:39 +03:00
assert menu == req_menus.scalars().all()[0]
2024-01-29 02:19:02 +03:00
2024-01-29 22:22:36 +03:00
# Обновляем
2024-01-29 02:19:02 +03:00
menu.title = "updatedMenu"
await MenuCrud.update_menu_item(
menu.id, menuschema.model_validate(menu), asession
)
2024-01-29 22:22:36 +03:00
# И сверяем
2024-01-29 02:19:02 +03:00
req_menu = await MenuCrud.get_menu_item(menu_id, asession)
assert menu == req_menu
2024-01-29 22:22:36 +03:00
# Удаляем и проверяем
2024-01-29 02:19:02 +03:00
await MenuCrud.delete_menu_item(menu_id, asession)
req_menus = await MenuCrud.get_menus(asession)
assert req_menus.all() == []
@pytest.mark.asyncio
async def test_submenu(asession: AsyncSession) -> None:
async with asession:
2024-01-29 20:11:39 +03:00
# Создаем меню напрямую
2024-01-31 01:05:28 +03:00
menu: Menu = Menu(title="SomeMenu", description="SomeDescription")
2024-01-29 02:19:02 +03:00
asession.add(menu)
await asession.commit()
await asession.refresh(menu)
menu_id: UUID = menu.id
2024-01-29 20:11:39 +03:00
2024-01-29 22:22:36 +03:00
# Создаем подменю
2024-01-29 02:19:02 +03:00
submenu: SubMenu = SubMenu(
2024-01-29 12:34:11 +03:00
title="submenu",
description="",
parent_menu=menu_id,
2024-01-29 02:19:02 +03:00
)
submenu = await SubMenuCrud.create_submenu_item(
2024-01-29 12:34:11 +03:00
menu_id,
menubaseschema.model_validate(submenu),
asession,
2024-01-29 02:19:02 +03:00
)
2024-01-29 22:22:36 +03:00
submenu_id = submenu.id
# Проверяем подменю
req_submenu = await SubMenuCrud.get_submenu_item(
2024-01-30 23:11:40 +03:00
menu_id,
submenu.id,
asession,
2024-01-29 22:22:36 +03:00
)
assert submenu == req_submenu
2024-01-31 01:05:28 +03:00
assert submenu.dishes_count == 0
2024-01-29 22:22:36 +03:00
# Обновляем меню
submenu.title = "UpdatedSubmenu"
req_submenu = await SubMenuCrud.update_submenu_item(
2024-01-30 23:11:40 +03:00
submenu_id,
menubaseschema.model_validate(submenu),
asession,
2024-01-29 22:22:36 +03:00
)
assert submenu == req_submenu.scalar_one_or_none()
menu = await MenuCrud.get_menu_item(menu_id, asession)
assert 1 == menu.submenus_count
# Удаляем полменю
await SubMenuCrud.delete_submenu_item(submenu_id, asession)
menu = await MenuCrud.get_menu_item(menu_id, asession)
assert 0 == menu.submenus_count
await MenuCrud.delete_menu_item(menu_id, asession)
2024-01-29 20:11:39 +03:00
@pytest.mark.asyncio
async def test_dish(asession: AsyncSession):
2024-01-29 22:22:36 +03:00
"""Not Implemented yet"""
2024-01-29 20:11:39 +03:00
async with asession:
2024-01-31 01:05:28 +03:00
# Создаем меню напрямую
menu = Menu(title="SomeMenu", description="SomeDescription")
asession.add(menu)
await asession.commit()
await asession.refresh(menu)
menu_id: UUID = menu.id
# Создаем подменю
submenu: SubMenu = SubMenu(
title="submenu",
description="",
parent_menu=menu_id,
)
asession.add(submenu)
await asession.commit()
await asession.refresh(submenu)
submenu_id = submenu.id
# Создаем блюдо
dish: Dish = Dish(
title="dish1",
description="dish number 1",
price="12.5",
parent_submenu=submenu_id,
)
dish = await DishCrud.create_dish_item(
submenu_id,
dishschema.model_validate(dish),
asession,
)
dish_id = dish.id
# Проверяем блюдо
req_dish = await DishCrud.get_dish_item(
dish_id,
asession,
)
assert dish == req_dish
menu = await MenuCrud.get_menu_item(menu_id, asession)
submenu = await SubMenuCrud.get_submenu_item(
menu_id,
submenu.id,
asession,
)
assert menu.submenus_count == 1
assert menu.dishes_count == 1
assert submenu.dishes_count == 1
# Обновляем блюдо
dish.price = 177
req_dish = await DishCrud.update_dish_item(
dish_id,
dishschema.model_validate(dish),
asession,
)
assert dish == req_dish
# Удаляем длюдо
await DishCrud.delete_dish_item(dish_id, asession)
menu = await MenuCrud.get_menu_item(menu_id, asession)
submenu = await SubMenuCrud.get_submenu_item(
menu_id,
submenu.id,
asession,
)
assert menu.dishes_count == 0
assert submenu.dishes_count == 0
await SubMenuCrud.delete_submenu_item(submenu_id, asession)
await MenuCrud.delete_menu_item(menu_id, asession)