2025-11-02 07:54:04 +00:00
|
|
|
import unittest
|
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
|
|
|
|
import main
|
|
|
|
|
from db import connect, create
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestRecipeIngredientsLoadedV2(unittest.IsolatedAsyncioTestCase):
|
|
|
|
|
async def asyncSetUp(self):
|
|
|
|
|
self.conn = await connect(":memory:")
|
|
|
|
|
await create(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 household
|
|
|
|
|
r = self.client.post(
|
|
|
|
|
"/api/v1/auth/register",
|
|
|
|
|
json={"email": "ing@test.com", "password": "pw", "displayName": "ING"},
|
|
|
|
|
)
|
|
|
|
|
assert r.status_code == 200, r.text
|
|
|
|
|
self.headers = {"Authorization": f"Bearer {r.json()['accessToken']}"}
|
|
|
|
|
|
|
|
|
|
r = self.client.post("/api/v1/households", headers=self.headers, json={"name": "H"})
|
|
|
|
|
assert r.status_code == 200, r.text
|
|
|
|
|
self.slug = r.json()["slug"]
|
|
|
|
|
|
|
|
|
|
async def asyncTearDown(self):
|
|
|
|
|
await self.conn.close()
|
|
|
|
|
main.app.dependency_overrides.clear()
|
|
|
|
|
|
|
|
|
|
def _create_recipe(self):
|
|
|
|
|
recipe = {
|
|
|
|
|
"id": -1,
|
|
|
|
|
"name": "HasIngredients",
|
|
|
|
|
"link": "https://example.com/has",
|
|
|
|
|
"serves": 2,
|
|
|
|
|
"imageUrls": [],
|
|
|
|
|
"ingredients": [
|
|
|
|
|
{
|
|
|
|
|
"id": 0,
|
|
|
|
|
"line": "2 Eggs",
|
|
|
|
|
"name": "Eggs",
|
|
|
|
|
"unit": "Items",
|
|
|
|
|
"quantity": 2,
|
|
|
|
|
"preparation": "",
|
|
|
|
|
"product": None,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"id": 0,
|
|
|
|
|
"line": "1 tbsp Butter",
|
|
|
|
|
"name": "Butter",
|
|
|
|
|
"unit": "Tablespoon",
|
|
|
|
|
"quantity": 1,
|
|
|
|
|
"preparation": "",
|
|
|
|
|
"product": None,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
r = self.client.post(
|
|
|
|
|
f"/api/v1/households/{self.slug}/recipes", headers=self.headers, json=recipe
|
|
|
|
|
)
|
|
|
|
|
assert r.status_code == 200, r.text
|
|
|
|
|
return r.json()["id"], recipe
|
|
|
|
|
|
|
|
|
|
def test_get_by_id_includes_ingredients(self):
|
|
|
|
|
rid, expected = self._create_recipe()
|
2025-11-02 07:55:55 +00:00
|
|
|
g = self.client.get(f"/api/v1/households/{self.slug}/recipes/{rid}", headers=self.headers)
|
2025-11-02 07:54:04 +00:00
|
|
|
assert g.status_code == 200, g.text
|
|
|
|
|
body = g.json()
|
|
|
|
|
ings = body.get("ingredients", [])
|
|
|
|
|
assert len(ings) == 2
|
|
|
|
|
lines = [i.get("line") for i in ings]
|
|
|
|
|
assert "2 Eggs" in lines and "1 tbsp Butter" in lines
|
|
|
|
|
|
|
|
|
|
def test_list_includes_ingredients(self):
|
|
|
|
|
rid, expected = self._create_recipe()
|
2025-11-02 07:55:55 +00:00
|
|
|
lst = self.client.get(f"/api/v1/households/{self.slug}/recipes", headers=self.headers)
|
2025-11-02 07:54:04 +00:00
|
|
|
assert lst.status_code == 200, lst.text
|
|
|
|
|
items = lst.json().get("items", [])
|
|
|
|
|
# Find our recipe
|
|
|
|
|
match = next(i for i in items if i["id"] == rid)
|
|
|
|
|
ings = match.get("ingredients", [])
|
|
|
|
|
assert len(ings) == 2
|
|
|
|
|
lines = [i.get("line") for i in ings]
|
|
|
|
|
assert "2 Eggs" in lines and "1 tbsp Butter" in lines
|