2023-10-27 15:12:48 +02:00
|
|
|
import datetime
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
@pytest.mark.anyio
|
|
|
|
async def test_create_meal(
|
2023-10-27 16:17:10 +02:00
|
|
|
client, meal_payload_factory, product_payload_factory, entry_payload_factory
|
|
|
|
):
|
2023-10-27 15:12:48 +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 15:12:48 +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-10-27 15:12:48 +02:00
|
|
|
assert response.status_code == 200, response.json()
|
|
|
|
|
2023-10-27 16:17:10 +02:00
|
|
|
meal_id = response.json()["id"]
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
product_id = (await client.post("product", json=product_payload_factory())).json()[
|
|
|
|
"id"
|
|
|
|
]
|
2023-10-27 16:17:10 +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-10-27 16:17:10 +02:00
|
|
|
assert response.status_code == 200, response.json()
|
|
|
|
|
2023-10-27 15:12:48 +02:00
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
@pytest.mark.anyio
|
|
|
|
async def test_save_meal(client, meal_save_payload):
|
2023-10-27 15:12:48 +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 15:12:48 +02:00
|
|
|
|
2023-10-27 16:17:10 +02:00
|
|
|
meal = response.json()["meals"][0]
|
|
|
|
meal_id = meal["id"]
|
2023-10-27 15:12:48 +02:00
|
|
|
save_payload = meal_save_payload(meal_id)
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
response = await client.post(f"meal/{meal_id}/save", json=save_payload)
|
2023-10-27 15:12:48 +02:00
|
|
|
assert response.status_code == 200, response.json()
|
|
|
|
|
2023-10-27 16:17:10 +02:00
|
|
|
preset = response.json()
|
|
|
|
|
|
|
|
for k, v in preset.items():
|
|
|
|
if k in ("id", "name", "entries"):
|
|
|
|
continue
|
|
|
|
|
|
|
|
assert meal[k] == v, f"{k} != {v}"
|
|
|
|
|
2023-10-27 15:12:48 +02:00
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
@pytest.mark.anyio
|
|
|
|
async def test_list_presets(client, meal_save_payload):
|
|
|
|
response = await client.get("preset")
|
2023-10-27 15:12:48 +02:00
|
|
|
assert response.status_code == 200, response.json()
|
|
|
|
assert len(response.json()["presets"]) > 0, response.json()
|
|
|
|
|
|
|
|
name = meal_save_payload(0)["name"]
|
2024-05-20 12:10:17 +02:00
|
|
|
response = await client.get(f"preset?q={name}")
|
2023-10-27 15:12:48 +02:00
|
|
|
assert response.status_code == 200, response.json()
|
|
|
|
assert len(response.json()["presets"]) > 0, response.json()
|
|
|
|
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
@pytest.mark.anyio
|
|
|
|
async def test_create_meal_from_preset(client, meal_from_preset):
|
2023-10-27 15:12:48 +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 15:12:48 +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.get("preset")
|
2023-10-27 15:12:48 +02:00
|
|
|
assert response.status_code == 200, response.json()
|
|
|
|
assert len(response.json()["presets"]) > 0, response.json()
|
|
|
|
|
|
|
|
preset = response.json()["presets"][0]
|
|
|
|
|
|
|
|
payload = meal_from_preset(
|
|
|
|
meal_order,
|
|
|
|
diary_id,
|
|
|
|
preset["id"],
|
|
|
|
)
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
response = await client.post("meal/from_preset", json=payload)
|
2023-10-27 15:12:48 +02:00
|
|
|
assert response.status_code == 200, response.json()
|
|
|
|
meal = response.json()
|
|
|
|
|
|
|
|
for k, v in preset.items():
|
|
|
|
if k in ("id", "name", "entries"):
|
|
|
|
continue
|
|
|
|
|
|
|
|
assert meal[k] == v, f"{k} != {v}"
|
2023-10-27 16:49:19 +02:00
|
|
|
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
@pytest.mark.anyio
|
|
|
|
async def test_delete_preset(client):
|
|
|
|
presets = (await client.get("preset")).json()["presets"]
|
2023-10-27 16:49:19 +02:00
|
|
|
preset_id = presets[0]["id"]
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
response = await client.get(f"preset/{preset_id}")
|
2023-10-27 16:49:19 +02:00
|
|
|
assert response.status_code == 200, response.json()
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
response = await client.delete(f"preset/{preset_id}")
|
2023-10-27 16:49:19 +02:00
|
|
|
assert response.status_code == 200, response.json()
|
|
|
|
|
2024-05-20 12:10:17 +02:00
|
|
|
response = await client.get(f"preset/{preset_id}")
|
2023-10-27 16:49:19 +02:00
|
|
|
assert response.status_code == 404, response.json()
|