120 lines
4.2 KiB
Python
120 lines
4.2 KiB
Python
import datetime
|
|
|
|
TODAY = datetime.date.today().isoformat()
|
|
|
|
|
|
async def test_get_diary_returns_200(auth_client, diary):
|
|
response = await auth_client.get(f"/api/diary/{TODAY}")
|
|
assert response.status_code == 200
|
|
|
|
|
|
async def test_get_diary_returns_correct_fields(auth_client, diary, user_settings):
|
|
response = await auth_client.get(f"/api/diary/{TODAY}")
|
|
body = response.json()
|
|
assert body["date"] == TODAY
|
|
assert body["protein_goal"] == user_settings.protein_goal
|
|
assert body["calories_goal"] == user_settings.calories_goal
|
|
assert "meals" in body
|
|
assert "id" in body
|
|
|
|
|
|
async def test_get_diary_not_found_returns_404(auth_client):
|
|
response = await auth_client.get("/api/diary/2000-01-01")
|
|
assert response.status_code == 404
|
|
|
|
|
|
async def test_get_diary_without_auth_returns_401(client):
|
|
response = await client.get(f"/api/diary/{TODAY}")
|
|
assert response.status_code == 401
|
|
|
|
|
|
async def test_create_diary_returns_201(auth_client, user_settings):
|
|
response = await auth_client.post("/api/diary", json={"date": "2030-06-01"})
|
|
assert response.status_code == 201
|
|
|
|
|
|
async def test_create_diary_copies_goals_from_user_settings(auth_client, user_settings):
|
|
response = await auth_client.post("/api/diary", json={"date": "2030-06-02"})
|
|
body = response.json()
|
|
assert body["protein_goal"] == user_settings.protein_goal
|
|
assert body["carb_goal"] == user_settings.carb_goal
|
|
assert body["fat_goal"] == user_settings.fat_goal
|
|
assert body["fiber_goal"] == user_settings.fiber_goal
|
|
assert body["calories_goal"] == user_settings.calories_goal
|
|
|
|
|
|
async def test_create_diary_creates_breakfast_meal(auth_client, user_settings):
|
|
response = await auth_client.post("/api/diary", json={"date": "2030-06-03"})
|
|
meals = response.json()["meals"]
|
|
assert len(meals) == 1
|
|
assert meals[0]["name"] == "Breakfast"
|
|
assert meals[0]["order"] == 0
|
|
|
|
|
|
async def test_create_diary_without_settings_returns_404(auth_client):
|
|
response = await auth_client.post("/api/diary", json={"date": "2030-06-04"})
|
|
assert response.status_code == 404
|
|
|
|
|
|
async def test_create_diary_duplicate_date_returns_409(auth_client, user_settings):
|
|
await auth_client.post("/api/diary", json={"date": "2030-06-05"})
|
|
response = await auth_client.post("/api/diary", json={"date": "2030-06-05"})
|
|
assert response.status_code == 409
|
|
|
|
|
|
async def test_create_diary_without_auth_returns_401(client):
|
|
response = await client.post("/api/diary", json={"date": "2030-06-06"})
|
|
assert response.status_code == 401
|
|
|
|
|
|
async def test_update_diary_returns_200(auth_client, diary):
|
|
response = await auth_client.patch(
|
|
f"/api/diary/{TODAY}", json={"protein_goal": 180.0}
|
|
)
|
|
assert response.status_code == 200
|
|
|
|
|
|
async def test_update_diary_partial_update(auth_client, diary, user_settings):
|
|
response = await auth_client.patch(
|
|
f"/api/diary/{TODAY}", json={"protein_goal": 180.0}
|
|
)
|
|
body = response.json()
|
|
assert body["protein_goal"] == 180.0
|
|
assert body["carb_goal"] == user_settings.carb_goal
|
|
assert body["calories_goal"] == user_settings.calories_goal
|
|
|
|
|
|
async def test_update_diary_all_goals(auth_client, diary):
|
|
payload = {
|
|
"protein_goal": 120.0,
|
|
"carb_goal": 250.0,
|
|
"fat_goal": 60.0,
|
|
"fiber_goal": 25.0,
|
|
"calories_goal": 1800.0,
|
|
}
|
|
response = await auth_client.patch(f"/api/diary/{TODAY}", json=payload)
|
|
body = response.json()
|
|
assert body["protein_goal"] == 120.0
|
|
assert body["carb_goal"] == 250.0
|
|
assert body["fat_goal"] == 60.0
|
|
assert body["fiber_goal"] == 25.0
|
|
assert body["calories_goal"] == 1800.0
|
|
|
|
|
|
async def test_update_diary_not_found_returns_404(auth_client):
|
|
response = await auth_client.patch(
|
|
"/api/diary/2000-01-01", json={"protein_goal": 180.0}
|
|
)
|
|
assert response.status_code == 404
|
|
|
|
|
|
async def test_update_diary_negative_goal_returns_422(auth_client, diary):
|
|
response = await auth_client.patch(
|
|
f"/api/diary/{TODAY}", json={"protein_goal": -10.0}
|
|
)
|
|
assert response.status_code == 422
|
|
|
|
|
|
async def test_update_diary_without_auth_returns_401(client):
|
|
response = await client.patch(f"/api/diary/{TODAY}", json={"protein_goal": 180.0})
|
|
assert response.status_code == 401
|