78 lines
2.6 KiB
Python
78 lines
2.6 KiB
Python
|
|
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 TestMealsWriteV2(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)
|
||
|
|
|
||
|
|
# Auth user and create household
|
||
|
|
r = self.client.post(
|
||
|
|
"/api/v1/auth/register",
|
||
|
|
json={"email": "w@test.com", "password": "pw", "displayName": "W"},
|
||
|
|
)
|
||
|
|
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": "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 test_create_update_delete_scoped(self):
|
||
|
|
# Create a meal with suggested date and one extra ingredient
|
||
|
|
body = {
|
||
|
|
"suggestedDate": datetime.datetime.now().astimezone().isoformat(),
|
||
|
|
"chefs": [{"id": 1, "name": "A"}],
|
||
|
|
"cleanup": [{"id": 1, "name": "A"}],
|
||
|
|
"consumers": [{"id": 1, "name": "A"}],
|
||
|
|
"recipes": [],
|
||
|
|
"extraIngredients": [
|
||
|
|
{"name": "Salt", "line": "Salt", "unit": "Items", "quantity": 1, "preparation": ""}
|
||
|
|
],
|
||
|
|
}
|
||
|
|
r = self.client.post(
|
||
|
|
f"/api/v1/households/{self.slug}/meals",
|
||
|
|
headers=self.headers,
|
||
|
|
json=body,
|
||
|
|
)
|
||
|
|
assert r.status_code == 200, r.text
|
||
|
|
created = r.json()
|
||
|
|
meal_id = created["id"]
|
||
|
|
|
||
|
|
# Update: add an extra ingredient
|
||
|
|
created["extraIngredients"].append(
|
||
|
|
{"name": "Pepper", "line": "Pepper", "unit": "Items", "quantity": 1, "preparation": ""}
|
||
|
|
)
|
||
|
|
r2 = self.client.put(
|
||
|
|
f"/api/v1/households/{self.slug}/meals/{meal_id}", headers=self.headers, json=created
|
||
|
|
)
|
||
|
|
assert r2.status_code == 200, r2.text
|
||
|
|
updated = r2.json()
|
||
|
|
assert len(updated["extraIngredients"]) == 2
|
||
|
|
|
||
|
|
# Delete
|
||
|
|
r3 = self.client.delete(
|
||
|
|
f"/api/v1/households/{self.slug}/meals/{meal_id}", headers=self.headers
|
||
|
|
)
|
||
|
|
assert r3.status_code == 200, r3.text
|