126 lines
4.8 KiB
Python
126 lines
4.8 KiB
Python
|
|
import unittest
|
||
|
|
from fastapi.testclient import TestClient
|
||
|
|
|
||
|
|
import main
|
||
|
|
from db import connect, create
|
||
|
|
from scripts.migration_to_households import run_migration
|
||
|
|
|
||
|
|
|
||
|
|
class TestShoppingPurchaseV2(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 ingredients and outstanding requests in both households
|
||
|
|
# H1 ingredient + request
|
||
|
|
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_id = int(row[0])
|
||
|
|
await self.conn.execute(
|
||
|
|
"INSERT INTO ShoppingListItem (ingredient_id, person_id, created_date, household_id) VALUES (?, 1, datetime('now'), ?)",
|
||
|
|
(self.milk_id, self.h1_id),
|
||
|
|
)
|
||
|
|
|
||
|
|
# H2 ingredient + request
|
||
|
|
await self.conn.execute(
|
||
|
|
"INSERT INTO Ingredient (name, line, unit, quantity, recipe_id, meal_id, household_id) VALUES ('Eggs', '12 eggs', 'dozen', 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.eggs_id = int(row[0])
|
||
|
|
await self.conn.execute(
|
||
|
|
"INSERT INTO ShoppingListItem (ingredient_id, person_id, created_date, household_id) VALUES (?, 1, datetime('now'), ?)",
|
||
|
|
(self.eggs_id, self.h2_id),
|
||
|
|
)
|
||
|
|
|
||
|
|
await self.conn.commit()
|
||
|
|
|
||
|
|
async def asyncTearDown(self):
|
||
|
|
await self.conn.close()
|
||
|
|
main.app.dependency_overrides.clear()
|
||
|
|
|
||
|
|
def test_purchase_scoped_success(self):
|
||
|
|
# Purchase the outstanding H1 ingredient
|
||
|
|
body = {
|
||
|
|
"storeName": "woolworths",
|
||
|
|
"items": [
|
||
|
|
{
|
||
|
|
"ingredientId": self.milk_id,
|
||
|
|
"personId": 1,
|
||
|
|
}
|
||
|
|
],
|
||
|
|
}
|
||
|
|
resp = self.client.post(
|
||
|
|
f"/api/v1/households/{self.h1}/shopping", headers=self.headers, json=body
|
||
|
|
)
|
||
|
|
assert resp.status_code == 200, resp.text
|
||
|
|
data = resp.json()
|
||
|
|
assert data["list"]["id"] > 0
|
||
|
|
assert data["list"]["storeName"] in ("woolworths", "coles", "home")
|
||
|
|
assert len(data["list"]["items"]) == 1
|
||
|
|
|
||
|
|
# Verify via API: H1 has no outstanding items; H2 still has one
|
||
|
|
r1 = self.client.get(
|
||
|
|
f"/api/v1/households/{self.h1}/shopping/current", headers=self.headers
|
||
|
|
)
|
||
|
|
assert r1.status_code == 200, r1.text
|
||
|
|
cur1 = r1.json()
|
||
|
|
assert len(cur1["outstandingItems"]) == 0
|
||
|
|
|
||
|
|
r2 = self.client.get(
|
||
|
|
f"/api/v1/households/{self.h2}/shopping/current", headers=self.headers
|
||
|
|
)
|
||
|
|
assert r2.status_code == 200, r2.text
|
||
|
|
cur2 = r2.json()
|
||
|
|
assert len(cur2["outstandingItems"]) == 1
|
||
|
|
|
||
|
|
def test_purchase_validation_error(self):
|
||
|
|
resp = self.client.post(
|
||
|
|
f"/api/v1/households/{self.h1}/shopping", headers=self.headers, json={"storeName": "woolworths", "items": []}
|
||
|
|
)
|
||
|
|
assert resp.status_code == 400, resp.text
|
||
|
|
assert "application/problem+json" in resp.headers.get("content-type", "")
|