dooey/app/tests/api/shopping_lists.py
mohamad 240e54eec4 weeee💃
2025-03-27 08:13:54 +01:00

110 lines
5.1 KiB
Python

import pytest
from httpx import AsyncClient
from fastapi import status
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.shopping_list import ShoppingList, ListItem
from app.schemas.shopping_list import ShoppingListCreate, ListItemCreate
@pytest.mark.asyncio
async def test_get_shopping_lists(client: AsyncClient, db_session: AsyncSession, test_user, test_house):
response = await client.get(f"/{test_house.id}/lists", headers={"Authorization": f"Bearer {test_user.token}"})
assert response.status_code == status.HTTP_200_OK
@pytest.mark.asyncio
async def test_create_shopping_list(client: AsyncClient, db_session: AsyncSession, test_user, test_house):
shopping_list_data = ShoppingListCreate(name="Groceries")
response = await client.post(
f"/{test_house.id}/lists",
json=shopping_list_data.dict(),
headers={"Authorization": f"Bearer {test_user.token}"}
)
assert response.status_code == status.HTTP_201_CREATED
assert response.json()["name"] == shopping_list_data.name
@pytest.mark.asyncio
async def test_get_shopping_list(client: AsyncClient, db_session: AsyncSession, test_user, test_house, test_shopping_list):
response = await client.get(
f"/{test_house.id}/lists/{test_shopping_list.id}",
headers={"Authorization": f"Bearer {test_user.token}"}
)
assert response.status_code == status.HTTP_200_OK
assert response.json()["id"] == str(test_shopping_list.id)
@pytest.mark.asyncio
async def test_update_shopping_list(client: AsyncClient, db_session: AsyncSession, test_user, test_house, test_shopping_list):
update_data = {"name": "Updated List"}
response = await client.put(
f"/{test_house.id}/lists/{test_shopping_list.id}",
json=update_data,
headers={"Authorization": f"Bearer {test_user.token}"}
)
assert response.status_code == status.HTTP_200_OK
assert response.json()["name"] == update_data["name"]
@pytest.mark.asyncio
async def test_delete_shopping_list(client: AsyncClient, db_session: AsyncSession, test_user, test_house, test_shopping_list):
response = await client.delete(
f"/{test_house.id}/lists/{test_shopping_list.id}",
headers={"Authorization": f"Bearer {test_user.token}"}
)
assert response.status_code == status.HTTP_204_NO_CONTENT
@pytest.mark.asyncio
async def test_get_list_items(client: AsyncClient, db_session: AsyncSession, test_user, test_house, test_shopping_list):
response = await client.get(
f"/{test_house.id}/lists/{test_shopping_list.id}/items",
headers={"Authorization": f"Bearer {test_user.token}"}
)
assert response.status_code == status.HTTP_200_OK
@pytest.mark.asyncio
async def test_add_list_item(client: AsyncClient, db_session: AsyncSession, test_user, test_house, test_shopping_list):
item_data = ListItemCreate(name="Milk", quantity=2)
response = await client.post(
f"/{test_house.id}/lists/{test_shopping_list.id}/items",
json=item_data.dict(),
headers={"Authorization": f"Bearer {test_user.token}"}
)
assert response.status_code == status.HTTP_201_CREATED
assert response.json()["name"] == item_data.name
@pytest.mark.asyncio
async def test_update_list_item(client: AsyncClient, db_session: AsyncSession, test_user, test_house, test_shopping_list, test_list_item):
update_data = {"name": "Updated Item"}
response = await client.put(
f"/{test_house.id}/lists/{test_shopping_list.id}/items/{test_list_item.id}",
json=update_data,
headers={"Authorization": f"Bearer {test_user.token}"}
)
assert response.status_code == status.HTTP_200_OK
assert response.json()["name"] == update_data["name"]
@pytest.mark.asyncio
async def test_delete_list_item(client: AsyncClient, db_session: AsyncSession, test_user, test_house, test_shopping_list, test_list_item):
response = await client.delete(
f"/{test_house.id}/lists/{test_shopping_list.id}/items/{test_list_item.id}",
headers={"Authorization": f"Bearer {test_user.token}"}
)
assert response.status_code == status.HTTP_204_NO_CONTENT
@pytest.mark.asyncio
async def test_mark_item_complete(client: AsyncClient, db_session: AsyncSession, test_user, test_house, test_shopping_list, test_list_item):
response = await client.patch(
f"/{test_house.id}/lists/{test_shopping_list.id}/items/{test_list_item.id}/complete",
json={"is_completed": True},
headers={"Authorization": f"Bearer {test_user.token}"}
)
assert response.status_code == status.HTTP_200_OK
assert response.json()["is_completed"] is True
@pytest.mark.asyncio
async def test_reorder_items(client: AsyncClient, db_session: AsyncSession, test_user, test_house, test_shopping_list, test_list_item):
reorder_data = [{"item_id": str(test_list_item.id), "new_position": 1}]
response = await client.post(
f"/{test_house.id}/lists/{test_shopping_list.id}/items/reorder",
json=reorder_data,
headers={"Authorization": f"Bearer {test_user.token}"}
)
assert response.status_code == status.HTTP_200_OK
assert response.json()["detail"] == "Items reordered successfully"