2023-09-03 15:43:24 +02:00
|
|
|
import datetime
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
@pytest.mark.anyio
|
|
|
|
async def test_get_diary(client):
|
2023-09-03 15:43:24 +02:00
|
|
|
today = datetime.date.today().isoformat()
|
2024-05-20 12:10:17 +02:00
|
|
|
response = await client.get("diary", params={"date": today})
|
2023-09-03 15:43:24 +02:00
|
|
|
assert response.status_code == 200, response.json()
|
|
|
|
|
|
|
|
assert response.json()["date"] == today
|
|
|
|
# new diary should contain exactly one meal
|
|
|
|
assert len(response.json()["meals"]) == 1
|
|
|
|
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
@pytest.mark.anyio
|
|
|
|
async def test_diary_add_meal(client, meal_payload_factory):
|
2023-09-03 15:43:24 +02:00
|
|
|
today = datetime.date.today().isoformat()
|
2024-05-20 12:10:17 +02:00
|
|
|
response = await client.get("diary", params={"date": today})
|
2023-09-03 15:43:24 +02:00
|
|
|
|
|
|
|
diary_id = response.json()["id"]
|
|
|
|
meal_order = len(response.json()["meals"]) + 1
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
response = await client.post(
|
|
|
|
"meal", json=meal_payload_factory(diary_id, meal_order)
|
|
|
|
)
|
2023-09-03 15:43:24 +02:00
|
|
|
assert response.status_code == 200, response.json()
|
|
|
|
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
@pytest.mark.anyio
|
|
|
|
async def test_diary_delete_meal(client):
|
2023-10-27 16:39:26 +02:00
|
|
|
today = datetime.date.today().isoformat()
|
2024-05-20 12:10:17 +02:00
|
|
|
response = await client.get("diary", params={"date": today})
|
2023-10-27 16:39:26 +02:00
|
|
|
|
|
|
|
meals_amount = len(response.json()["meals"])
|
|
|
|
meal_id = response.json()["meals"][0]["id"]
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
response = await client.delete(f"meal/{meal_id}")
|
2023-10-27 16:39:26 +02:00
|
|
|
assert response.status_code == 200, response.json()
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
response = await client.get("diary", params={"date": today})
|
2023-10-27 16:39:26 +02:00
|
|
|
assert response.status_code == 200, response.json()
|
|
|
|
assert len(response.json()["meals"]) == meals_amount - 1
|
|
|
|
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
@pytest.mark.anyio
|
|
|
|
async def test_diary_add_entry(client, product_payload_factory, entry_payload_factory):
|
2023-09-03 15:43:24 +02:00
|
|
|
today = datetime.date.today().isoformat()
|
2024-05-20 12:10:17 +02:00
|
|
|
response = await client.get("diary", params={"date": today})
|
2023-09-03 15:43:24 +02:00
|
|
|
|
|
|
|
meal_id = response.json()["meals"][0]["id"]
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
product_id = (await client.post("product", json=product_payload_factory())).json()[
|
|
|
|
"id"
|
|
|
|
]
|
2023-09-03 15:43:24 +02:00
|
|
|
|
|
|
|
entry_payload = entry_payload_factory(meal_id, product_id, 100.0)
|
2024-05-20 12:10:17 +02:00
|
|
|
response = await client.post("entry", json=entry_payload)
|
2023-09-03 15:43:24 +02:00
|
|
|
assert response.status_code == 200, response.json()
|
|
|
|
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
@pytest.mark.anyio
|
|
|
|
async def test_diary_edit_entry(client, entry_payload_factory):
|
2023-09-03 15:43:24 +02:00
|
|
|
today = datetime.date.today().isoformat()
|
2024-05-20 12:10:17 +02:00
|
|
|
response = await client.get("diary", params={"date": today})
|
2023-09-03 15:43:24 +02:00
|
|
|
|
|
|
|
entry = response.json()["meals"][0]["entries"][0]
|
|
|
|
id_ = entry["id"]
|
|
|
|
entry_payload = entry_payload_factory(
|
|
|
|
entry["meal_id"], entry["product"]["id"], entry["grams"] + 100.0
|
|
|
|
)
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
response = await client.patch(f"entry/{id_}", json=entry_payload)
|
2023-09-03 15:43:24 +02:00
|
|
|
assert response.status_code == 200, response.json()
|
|
|
|
assert response.json()["grams"] == entry_payload["grams"]
|
|
|
|
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
@pytest.mark.anyio
|
|
|
|
async def test_diary_delete_entry(client):
|
2023-09-03 15:43:24 +02:00
|
|
|
today = datetime.date.today().isoformat()
|
2024-05-20 12:10:17 +02:00
|
|
|
response = await client.get("diary", params={"date": today})
|
2023-09-03 15:43:24 +02:00
|
|
|
|
|
|
|
entry_id = response.json()["meals"][0]["entries"][0]["id"]
|
2024-05-20 12:10:17 +02:00
|
|
|
response = await client.delete(f"entry/{entry_id}")
|
2023-09-03 15:43:24 +02:00
|
|
|
assert response.status_code == 200, response.json()
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
response = await client.get("diary", params={"date": today})
|
2023-09-03 15:43:24 +02:00
|
|
|
assert response.status_code == 200, response.json()
|
|
|
|
deleted_entries = [
|
|
|
|
entry
|
|
|
|
for meal in response.json()["meals"]
|
|
|
|
for entry in meal["entries"]
|
|
|
|
if entry["id"] == entry_id
|
|
|
|
]
|
|
|
|
assert len(deleted_entries) == 0
|