munch-ease-backend/tests/test_ingredients_parse_multiple_v2.py

61 lines
1.9 KiB
Python
Raw Permalink Normal View History

import unittest
from fastapi.testclient import TestClient
import main
from db import connect, create
class TestIngredientsParseMultipleV2(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 user and create household
r = self.client.post(
"/api/v1/auth/register",
json={"email": "ing2@test.com", "password": "pw", "displayName": "Ing2"},
)
assert r.status_code == 200, r.text
token = r.json()["accessToken"]
self.headers = {"Authorization": f"Bearer {token}"}
r2 = self.client.post("/api/v1/households", headers=self.headers, json={"name": "H2"})
assert r2.status_code == 200, r2.text
self.slug = r2.json()["slug"]
async def asyncTearDown(self):
await self.conn.close()
main.app.dependency_overrides.clear()
def test_parse_multiple_lines_with_lines_param(self):
lines = [
"14oz milk powder",
"2 cups flour",
"1 tsp salt",
"egg",
]
r = self.client.get(
f"/api/v1/households/{self.slug}/ingredients/parse",
params=[("lines", line) for line in lines],
headers=self.headers,
)
assert r.status_code == 200, r.text
items = r.json()
assert isinstance(items, list)
assert len(items) == 4
# spot check
oz, cups, tsp, egg = items
assert float(oz["quantity"]) == 14.0 and oz["unit"] == "Ounce"
assert cups["name"].lower() == "flour"
assert tsp["unit"] == "Teaspoon"
assert egg["name"].lower() == "egg"