2025-11-01 03:21:34 +00:00
|
|
|
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 TestMealsHouseholdV2(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": "m@test.com", "password": "pw", "displayName": "M"},
|
|
|
|
|
)
|
|
|
|
|
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"]
|
|
|
|
|
|
|
|
|
|
# Insert meals directly with household_id to seed data
|
|
|
|
|
now = datetime.datetime.utcnow()
|
|
|
|
|
# Raw inserts
|
|
|
|
|
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])
|
|
|
|
|
|
|
|
|
|
await self.conn.execute(
|
|
|
|
|
"INSERT INTO Meal (suggested_date, consumed_date, deleted_date, purchase_date, household_id) VALUES (?, NULL, NULL, NULL, ?)",
|
|
|
|
|
(now + datetime.timedelta(days=1), self.h1_id),
|
|
|
|
|
)
|
|
|
|
|
await self.conn.execute(
|
|
|
|
|
"INSERT INTO Meal (suggested_date, consumed_date, deleted_date, purchase_date, household_id) VALUES (?, NULL, NULL, NULL, ?)",
|
|
|
|
|
(now + datetime.timedelta(days=2), self.h2_id),
|
|
|
|
|
)
|
|
|
|
|
await self.conn.commit()
|
|
|
|
|
|
2025-11-01 03:30:45 +00:00
|
|
|
# Capture meal ids for each household
|
|
|
|
|
async with self.conn.execute(
|
|
|
|
|
"SELECT id FROM Meal WHERE household_id = ? ORDER BY id LIMIT 1", (self.h1_id,)
|
|
|
|
|
) as c:
|
|
|
|
|
row = await c.fetchone()
|
|
|
|
|
assert row is not None
|
|
|
|
|
self.meal_h1_id = int(row[0])
|
|
|
|
|
async with self.conn.execute(
|
|
|
|
|
"SELECT id FROM Meal WHERE household_id = ? ORDER BY id LIMIT 1", (self.h2_id,)
|
|
|
|
|
) as c:
|
|
|
|
|
row = await c.fetchone()
|
|
|
|
|
assert row is not None
|
|
|
|
|
self.meal_h2_id = int(row[0])
|
|
|
|
|
|
2025-11-01 03:21:34 +00:00
|
|
|
async def asyncTearDown(self):
|
|
|
|
|
await self.conn.close()
|
|
|
|
|
main.app.dependency_overrides.clear()
|
|
|
|
|
|
|
|
|
|
def test_upcoming_meals_are_scoped(self):
|
|
|
|
|
now = datetime.datetime.utcnow()
|
|
|
|
|
params = {
|
|
|
|
|
"from": (now - datetime.timedelta(days=1)).isoformat() + "Z",
|
|
|
|
|
"to": (now + datetime.timedelta(days=7)).isoformat() + "Z",
|
|
|
|
|
}
|
|
|
|
|
r1 = self.client.get(
|
|
|
|
|
f"/api/v1/households/{self.h1}/meals/upcoming", headers=self.headers, params=params
|
|
|
|
|
)
|
|
|
|
|
assert r1.status_code == 200, r1.text
|
|
|
|
|
m1 = r1.json()
|
|
|
|
|
assert isinstance(m1, list)
|
|
|
|
|
assert len(m1) == 1
|
|
|
|
|
|
|
|
|
|
r2 = self.client.get(
|
|
|
|
|
f"/api/v1/households/{self.h2}/meals/upcoming", headers=self.headers, params=params
|
|
|
|
|
)
|
|
|
|
|
assert r2.status_code == 200, r2.text
|
|
|
|
|
m2 = r2.json()
|
|
|
|
|
assert isinstance(m2, list)
|
|
|
|
|
assert len(m2) == 1
|
|
|
|
|
# Ensure different meal ids per household
|
|
|
|
|
assert m1[0]["id"] != m2[0]["id"]
|
2025-11-01 03:30:45 +00:00
|
|
|
|
|
|
|
|
def test_get_meal_scoped(self):
|
|
|
|
|
# Correct household should succeed
|
|
|
|
|
r_ok = self.client.get(
|
|
|
|
|
f"/api/v1/households/{self.h1}/meals/{self.meal_h1_id}", headers=self.headers
|
|
|
|
|
)
|
|
|
|
|
assert r_ok.status_code == 200, r_ok.text
|
|
|
|
|
assert r_ok.json()["id"] == self.meal_h1_id
|
|
|
|
|
|
|
|
|
|
# Cross-household should 404
|
|
|
|
|
r_404 = self.client.get(
|
|
|
|
|
f"/api/v1/households/{self.h2}/meals/{self.meal_h1_id}", headers=self.headers
|
|
|
|
|
)
|
|
|
|
|
assert r_404.status_code == 404
|