dishcrud test
parent
ead24d9f28
commit
64bc03b7fa
17
README.md
17
README.md
|
@ -97,12 +97,21 @@ Fastapi веб приложение реализующее api для общеп
|
||||||
`CREATE DATABASE <db_name>_test WITH OWNER <db_user>;`
|
`CREATE DATABASE <db_name>_test WITH OWNER <db_user>;`
|
||||||
где <db_name> и <db_user> соответвтовали POSTGRES_DB и POSTGRES_USER в файле `.env`
|
где <db_name> и <db_user> соответвтовали POSTGRES_DB и POSTGRES_USER в файле `.env`
|
||||||
|
|
||||||
Создайте и запустите образы
|
Создайте контейнеры
|
||||||
Запуск FAstAPI приложения
|
> `$ docker-compose -f compose_app.yml build && docker-compose -f compose_test.yml build`
|
||||||
> `$ docker-compose -f compose_app.yml up -d`
|
|
||||||
|
|
||||||
Запуск тестов
|
И запустите образы:
|
||||||
|
|
||||||
|
- Запуск FAstAPI приложения
|
||||||
|
> `$ docker-compose -f compose_app.yml up -d`
|
||||||
|
По завершении работы остановите контейнеры
|
||||||
|
> `$ docker-compose -f compose_app.yml down`
|
||||||
|
|
||||||
|
|
||||||
|
- Запуск тестов
|
||||||
> `$ docker-compose -f compose_test.yml up`
|
> `$ docker-compose -f compose_test.yml up`
|
||||||
|
По завершении работы остановите контейнеры
|
||||||
|
> `$ docker-compose -f compose_test.yml down`
|
||||||
|
|
||||||
После успешного запуска образов документация по API будет доступна по адресу <a href="http://localhost:8000/docs">http://localhost:8000</a>
|
После успешного запуска образов документация по API будет доступна по адресу <a href="http://localhost:8000/docs">http://localhost:8000</a>
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ services:
|
||||||
- .env
|
- .env
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_DB: "${POSTGRES_DB}_test"
|
POSTGRES_DB: ${POSTGRES_DB_TEST}
|
||||||
POSTGRES_USER: ${POSTGRES_USER}
|
POSTGRES_USER: ${POSTGRES_USER}
|
||||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
|
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ services:
|
||||||
- 6432:5432
|
- 6432:5432
|
||||||
|
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}_test"]
|
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB_TEST}"]
|
||||||
interval: 10s
|
interval: 10s
|
||||||
timeout: 5s
|
timeout: 5s
|
||||||
retries: 5
|
retries: 5
|
||||||
|
|
|
@ -2,12 +2,12 @@ from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||||
|
|
||||||
|
|
||||||
class Settings(BaseSettings):
|
class Settings(BaseSettings):
|
||||||
DB_HOST: str = "db"
|
DB_HOST: str = ""
|
||||||
DB_PORT: int = 5432
|
DB_PORT: int = 5432
|
||||||
POSTGRES_DB: str = "fastfood_db"
|
POSTGRES_DB: str = ""
|
||||||
POSTGRES_PASSWORD: str = "test"
|
POSTGRES_PASSWORD: str = ""
|
||||||
POSTGRES_USER: str = "testuser"
|
POSTGRES_USER: str = ""
|
||||||
POSTGRES_DB_TEST: str = "fastfood_db_test"
|
POSTGRES_DB_TEST: str = ""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def DATABASE_URL_asyncpg(self):
|
def DATABASE_URL_asyncpg(self):
|
||||||
|
|
|
@ -3,9 +3,11 @@ from uuid import UUID
|
||||||
import pytest
|
import pytest
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
|
from fastfood.cruds.dish import DishCrud
|
||||||
from fastfood.cruds.menu import MenuCrud
|
from fastfood.cruds.menu import MenuCrud
|
||||||
from fastfood.cruds.submenu import SubMenuCrud
|
from fastfood.cruds.submenu import SubMenuCrud
|
||||||
from fastfood.models import Menu, SubMenu
|
from fastfood.models import Dish, Menu, SubMenu
|
||||||
|
from fastfood.schemas import DishBase as dishschema
|
||||||
from fastfood.schemas import Menu as menuschema
|
from fastfood.schemas import Menu as menuschema
|
||||||
from fastfood.schemas import MenuBase as menubaseschema
|
from fastfood.schemas import MenuBase as menubaseschema
|
||||||
|
|
||||||
|
@ -48,7 +50,7 @@ async def test_menu(asession: AsyncSession) -> None:
|
||||||
async def test_submenu(asession: AsyncSession) -> None:
|
async def test_submenu(asession: AsyncSession) -> None:
|
||||||
async with asession:
|
async with asession:
|
||||||
# Создаем меню напрямую
|
# Создаем меню напрямую
|
||||||
menu = Menu(title="SomeMenu", description="SomeDescription")
|
menu: Menu = Menu(title="SomeMenu", description="SomeDescription")
|
||||||
asession.add(menu)
|
asession.add(menu)
|
||||||
await asession.commit()
|
await asession.commit()
|
||||||
await asession.refresh(menu)
|
await asession.refresh(menu)
|
||||||
|
@ -74,6 +76,7 @@ async def test_submenu(asession: AsyncSession) -> None:
|
||||||
asession,
|
asession,
|
||||||
)
|
)
|
||||||
assert submenu == req_submenu
|
assert submenu == req_submenu
|
||||||
|
assert submenu.dishes_count == 0
|
||||||
|
|
||||||
# Обновляем меню
|
# Обновляем меню
|
||||||
submenu.title = "UpdatedSubmenu"
|
submenu.title = "UpdatedSubmenu"
|
||||||
|
@ -96,9 +99,81 @@ async def test_submenu(asession: AsyncSession) -> None:
|
||||||
await MenuCrud.delete_menu_item(menu_id, asession)
|
await MenuCrud.delete_menu_item(menu_id, asession)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skip
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_dish(asession: AsyncSession):
|
async def test_dish(asession: AsyncSession):
|
||||||
"""Not Implemented yet"""
|
"""Not Implemented yet"""
|
||||||
async with asession:
|
async with asession:
|
||||||
pass
|
# Создаем меню напрямую
|
||||||
|
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)
|
||||||
|
|
Loading…
Reference in New Issue