import unittest from fastapi.testclient import TestClient import main from db import connect, create from scripts.migration_to_households import run_migration class TestShoppingUnrequestIngredientV2(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 user and create two households r = self.client.post( "/api/v1/auth/register", json={"email": "unreq@test.com", "password": "pw", "displayName": "UnReq"}, ) 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"] # Resolve 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 one ingredient in both households await self.conn.execute( "INSERT INTO Ingredient (name, line, unit, quantity, recipe_id, meal_id, household_id) VALUES ('Milk', '1L milk', 'L', 1, NULL, NULL, ?)", (self.h1_id,), ) async with self.conn.execute("SELECT last_insert_rowid()") as c: row = await c.fetchone() assert row is not None self.milk_h1_id = int(row[0]) await self.conn.execute( "INSERT INTO Ingredient (name, line, unit, quantity, recipe_id, meal_id, household_id) VALUES ('Milk', '1L milk', 'L', 1, NULL, NULL, ?)", (self.h2_id,), ) async with self.conn.execute("SELECT last_insert_rowid()") as c: row = await c.fetchone() assert row is not None self.milk_h2_id = int(row[0]) await self.conn.commit() async def asyncTearDown(self): await self.conn.close() main.app.dependency_overrides.clear() def test_unrequest_ingredient_idempotent_and_scoped(self): # Request ingredient in H1 r = self.client.post( f"/api/v1/households/{self.h1}/shopping/current/ingredients", headers=self.headers, json={"ingredientId": self.milk_h1_id}, ) assert r.status_code == 200, r.text # Ensure it shows up in H1 current cur = self.client.get( f"/api/v1/households/{self.h1}/shopping/current", headers=self.headers ).json() assert any(i.get("ingredientId") == self.milk_h1_id for i in cur["outstandingItems"]) # Delete it via new endpoint d = self.client.request( "DELETE", f"/api/v1/households/{self.h1}/shopping/current/ingredients", headers=self.headers, json={"ingredientId": self.milk_h1_id}, ) assert d.status_code == 200, d.text assert d.json()["ok"] is True # It disappears from H1 current cur = self.client.get( f"/api/v1/households/{self.h1}/shopping/current", headers=self.headers ).json() assert not any(i.get("ingredientId") == self.milk_h1_id for i in cur["outstandingItems"]) # Idempotent: delete again d2 = self.client.request( "DELETE", f"/api/v1/households/{self.h1}/shopping/current/ingredients", headers=self.headers, json={"ingredientId": self.milk_h1_id}, ) assert d2.status_code == 200, d2.text assert d2.json()["ok"] is True # Ensure H2 unaffected cur2 = self.client.get( f"/api/v1/households/{self.h2}/shopping/current", headers=self.headers ).json() # No request in H2, so still absent assert not any(i.get("ingredientId") == self.milk_h2_id for i in cur2["outstandingItems"])