fooder-api/fooder/test/test_preset.py

104 lines
3.2 KiB
Python
Raw Normal View History

2023-10-27 15:12:48 +02:00
import datetime
import pytest
@pytest.mark.dependency()
2023-10-27 16:17:10 +02:00
def test_create_meal(
client, meal_payload_factory, product_payload_factory, entry_payload_factory
):
2023-10-27 15:12:48 +02:00
today = datetime.date.today().isoformat()
response = client.get("diary", params={"date": today})
diary_id = response.json()["id"]
meal_order = len(response.json()["meals"]) + 1
response = client.post("meal", json=meal_payload_factory(diary_id, meal_order))
assert response.status_code == 200, response.json()
2023-10-27 16:17:10 +02:00
meal_id = response.json()["id"]
product_id = client.post("product", json=product_payload_factory()).json()["id"]
entry_payload = entry_payload_factory(meal_id, product_id, 100.0)
response = client.post("entry", json=entry_payload)
assert response.status_code == 200, response.json()
2023-10-27 15:12:48 +02:00
@pytest.mark.dependency(depends=["test_create_meal"])
def test_save_meal(client, meal_save_payload):
today = datetime.date.today().isoformat()
response = client.get("diary", params={"date": today})
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)
response = client.post(f"meal/{meal_id}/save", json=save_payload)
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
@pytest.mark.dependency(depends=["test_create_meal"])
def test_list_presets(client, meal_save_payload):
response = client.get("preset")
assert response.status_code == 200, response.json()
assert len(response.json()["presets"]) > 0, response.json()
name = meal_save_payload(0)["name"]
response = client.get(f"preset?q={name}")
assert response.status_code == 200, response.json()
assert len(response.json()["presets"]) > 0, response.json()
@pytest.mark.dependency(depends=["test_list_presets"])
def test_create_meal_from_preset(client, meal_from_preset):
today = datetime.date.today().isoformat()
response = client.get("diary", params={"date": today})
diary_id = response.json()["id"]
meal_order = len(response.json()["meals"]) + 1
response = client.get("preset")
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"],
)
response = client.post("meal/from_preset", json=payload)
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
@pytest.mark.dependency(depends=["test_list_presets"])
def test_delete_preset(client):
presets = client.get("preset").json()["presets"]
preset_id = presets[0]["id"]
response = client.get(f"preset/{preset_id}")
assert response.status_code == 200, response.json()
response = client.delete(f"preset/{preset_id}")
assert response.status_code == 200, response.json()
response = client.get(f"preset/{preset_id}")
assert response.status_code == 404, response.json()