munch-ease-backend/tests/test_shopping.py
jableader e2a59536b1 Squashed commit of the following:
commit a63d4740d5594fb603ba13fbf1515e8b7081d370
Author: jableader <jacobdunk@gmail.com>
Date:   Sat Sep 28 12:41:37 2024 +1000

    Impl
2024-09-28 14:54:54 +10:00

156 lines
No EOL
6.3 KiB
Python

from datetime import datetime, timedelta
import importlib
import unittest
import tests.test_data as test_data
import shopping
import ingredients, products
from db import connect, create
def reload_test_data():
global test_data
test_data = importlib.reload(test_data)
def first(iterable: list, predicate: callable):
for item in iterable:
if predicate(item):
return item
return None
class TestShopping(unittest.IsolatedAsyncioTestCase):
async def asyncSetUp(self):
self.conn = await connect(':memory:')
await create(self.conn)
reload_test_data()
return await super().asyncSetUp()
async def asyncTearDown(self) -> None:
await self.conn.close()
return await super().asyncTearDown()
async def test_current_shopping_list(self):
shopping_list = await shopping.current_shopping_list(self.conn)
self.assertEqual(len(shopping_list.requests), 0)
self.assertEqual(len(shopping_list.results), 0)
async def test_get_current_adds_upcoming_meals(self):
import meals, recipes
meal = test_data.Meals.broccoli_soup_for_jacob
for product in [i.product for i in meal.extra_ingredients] + [i.product for r in meal.recipes for i in r.recipe.ingredients]:
if product.id < 0:
await products.insert_product(self.conn, product, {})
for mr in meal.recipes:
await recipes.insert_recipe(self.conn, mr.recipe)
mr.recipe_id = mr.recipe.id
meal.suggested_date = datetime.now() + timedelta(days=1)
await meals.insert_meal(self.conn, meal)
shopping_list = await shopping.current_shopping_list(self.conn)
self.assertEqual(len(shopping_list.requests), 1)
self.assertEqual(len(shopping_list.results), 0)
request = shopping_list.requests[0]
self.assertEqual(request.meal_id, meal.id)
async def test_sync_persons_requests(self):
ingredient = test_data.Ingredients.one_apple
person = test_data.Persons.jacob
await products.insert_product(self.conn, ingredient.product, {})
shopping_list = await shopping.current_shopping_list(self.conn)
async for _ in shopping.sync_persons_requested_ingredients(self.conn, shopping_list, person, [ingredient]):
pass
shopping_list = await shopping.current_shopping_list(self.conn)
self.assertEqual(len(shopping_list.requests), 1)
self.assertEqual(len(shopping_list.results), 0)
request = shopping_list.requests[0]
self.assertEqual(request.person_id, person.id)
self.assertEqual(request.ingredient.line, ingredient.line)
async def test_sync_persons_requests_multiple_add_item(self):
first = test_data.Ingredients.one_apple
second = test_data.Ingredients.salt
person = test_data.Persons.jacob
await products.insert_product(self.conn, first.product, {})
await products.insert_product(self.conn, second.product, {})
shopping_list = await shopping.current_shopping_list(self.conn)
async for _ in shopping.sync_persons_requested_ingredients(self.conn, shopping_list, person, [first]):
pass
async for _ in shopping.sync_persons_requested_ingredients(self.conn, shopping_list, person, [first, second]):
pass
shopping_list = await shopping.current_shopping_list(self.conn)
self.assertEqual(len(shopping_list.requests), 2)
self.assertEqual(len(shopping_list.results), 0)
request_by_line = {r.ingredient.line: r for r in shopping_list.requests}
self.assertEqual(len(request_by_line), 2)
for requested_ingredient in [first, second]:
request = request_by_line[requested_ingredient.line]
self.assertEqual(request.person_id, person.id)
self.assertEqual(request.ingredient.line, requested_ingredient.line)
async def test_mark_found(self):
ingredient = test_data.Ingredients.one_apple
await products.insert_product(self.conn, ingredient.product, {})
shopping_list = await shopping.current_shopping_list(self.conn)
await shopping.mark_found(self.conn, ingredient, date_found=datetime.now())
shopping_list = await shopping.current_shopping_list(self.conn)
self.assertEqual(len(shopping_list.results), 1)
self.assertEqual(shopping_list.results[0].product_id, ingredient.product.id)
self.assertEqual(shopping_list.results[0].quantity, 1)
self.assertEqual(shopping_list.results[0].unit, ingredient.unit)
ingredient.quantity = 2
await shopping.mark_found(self.conn, ingredient, date_found=datetime.now())
shopping_list = await shopping.current_shopping_list(self.conn)
self.assertEqual(len(shopping_list.results), 1)
self.assertEqual(shopping_list.results[0].product_id, ingredient.product.id)
self.assertEqual(shopping_list.results[0].quantity, 2)
self.assertEqual(shopping_list.results[0].unit, ingredient.unit)
ingredient.unit = 'kg'
await shopping.mark_found(self.conn, ingredient, date_found=datetime.now())
shopping_list = await shopping.current_shopping_list(self.conn)
self.assertEqual(len(shopping_list.results), 2)
results_by_unit = {r.unit: r for r in shopping_list.results}
self.assertEqual(len(results_by_unit), 2)
self.assertIn('kg', results_by_unit)
self.assertIn('Items', results_by_unit)
self.assertEqual(results_by_unit['kg'].product_id, results_by_unit['Items'].product_id)
async def test_purchase(self):
ingredient = test_data.Ingredients.one_apple
person = test_data.Persons.jacob
await products.insert_product(self.conn, ingredient.product, {})
shopping_list = await shopping.current_shopping_list(self.conn)
self.assertIsNone(shopping_list.purchased_date)
shopping_list = await shopping.mark_purchased(self.conn)
self.assertIsNotNone(shopping_list.purchased_date)
self.assertLessEqual(shopping_list.purchased_date - datetime.now(), timedelta(seconds=1))
new_shopping_list = await shopping.current_shopping_list(self.conn)
self.assertNotEqual(shopping_list.id, new_shopping_list.id)
self.assertIsNone(new_shopping_list.purchased_date)