munch-ease-backend/tests/test_meals_consumed_v2.py

127 lines
5 KiB
Python
Raw Normal View History

import datetime
import unittest
from fastapi.testclient import TestClient
import main
from db import connect, create
from scripts.migration_to_households import run_migration
class TestMealsConsumedV2(unittest.IsolatedAsyncioTestCase):
async def asyncSetUp(self):
self.conn = await connect(":memory:")
await create(self.conn)
await run_migration(self.conn)
async def override_get_db():
try:
yield self.conn
finally:
pass
main.app.dependency_overrides[main.get_db] = override_get_db
self.client = TestClient(main.app)
# Register a user and create two households
r = self.client.post(
"/api/v1/auth/register",
json={"email": "s@test.com", "password": "pw", "displayName": "S"},
)
assert r.status_code == 200, r.text
token = r.json()["accessToken"]
self.headers = {"Authorization": f"Bearer {token}"}
r = self.client.post("/api/v1/households", headers=self.headers, json={"name": "H1"})
assert r.status_code == 200, r.text
self.h1 = r.json()["slug"]
r = self.client.post("/api/v1/households", headers=self.headers, json={"name": "H2"})
assert r.status_code == 200, r.text
self.h2 = r.json()["slug"]
# Lookup household ids
async with self.conn.execute("SELECT id FROM Household WHERE slug = ?", (self.h1,)) as c:
row = await c.fetchone()
assert row is not None
self.h1_id = int(row[0])
async with self.conn.execute("SELECT id FROM Household WHERE slug = ?", (self.h2,)) as c:
row = await c.fetchone()
assert row is not None
self.h2_id = int(row[0])
# Seed two meals in different households
await self.conn.execute(
"INSERT INTO Meal (suggested_date, household_id) VALUES (?, ?)",
(datetime.datetime.now().astimezone().isoformat(), self.h1_id),
)
async with self.conn.execute("SELECT last_insert_rowid()") as c:
row = await c.fetchone()
assert row is not None
self.meal_h1 = int(row[0])
await self.conn.execute(
"INSERT INTO Meal (suggested_date, household_id) VALUES (?, ?)",
(datetime.datetime.now().astimezone().isoformat(), self.h2_id),
)
async with self.conn.execute("SELECT last_insert_rowid()") as c:
row = await c.fetchone()
assert row is not None
self.meal_h2 = int(row[0])
# Seed outstanding meal requests in both households
await self.conn.execute(
"INSERT INTO ShoppingListItem (ingredient_id, meal_id, person_id, created_date, household_id) VALUES (NULL, ?, 1, datetime('now'), ?)",
(self.meal_h1, self.h1_id),
)
await self.conn.execute(
"INSERT INTO ShoppingListItem (ingredient_id, meal_id, person_id, created_date, household_id) VALUES (NULL, ?, 1, datetime('now'), ?)",
(self.meal_h2, self.h2_id),
)
await self.conn.commit()
async def asyncTearDown(self):
await self.conn.close()
main.app.dependency_overrides.clear()
def test_mark_consumed_scoped(self):
body = {"consumedDate": datetime.datetime.now().astimezone().isoformat()}
r = self.client.post(
f"/api/v1/households/{self.h1}/meals/{self.meal_h1}/consumed",
headers=self.headers,
json=body,
)
assert r.status_code == 200, r.text
meal = r.json()
assert meal["id"] == self.meal_h1
assert meal["consumedDate"] is not None
# H1 meal request should be gone; H2 remains
2025-11-01 04:34:01 +00:00
r1 = self.client.get(f"/api/v1/households/{self.h1}/shopping/current", headers=self.headers)
assert r1.status_code == 200
cur1 = r1.json()
assert all(i.get("mealId") != self.meal_h1 for i in cur1["requestedMeals"]) # none for h1
2025-11-01 04:34:01 +00:00
r2 = self.client.get(f"/api/v1/households/{self.h2}/shopping/current", headers=self.headers)
assert r2.status_code == 200
cur2 = r2.json()
assert any(i.get("mealId") == self.meal_h2 for i in cur2["requestedMeals"]) # still present
async def test_mark_consumed_requires_timezone(self):
# Create another meal in H1
await self.conn.execute(
"INSERT INTO Meal (suggested_date, household_id) VALUES (datetime('now'), ?)",
(self.h1_id,),
)
async with self.conn.execute("SELECT last_insert_rowid()") as c:
row = await c.fetchone()
assert row is not None
meal_id = int(row[0])
# Missing tzinfo should 400
body = {"consumedDate": datetime.datetime.now().replace(tzinfo=None).isoformat()}
r = self.client.post(
f"/api/v1/households/{self.h1}/meals/{meal_id}/consumed",
headers=self.headers,
json=body,
)
assert r.status_code == 400
assert "timezone" in r.json()["title"].lower()