import unittest import test_data import pathlib from db import connect, create import products.db as products_db import recipes.db as recipes_db class TestProducts(unittest.IsolatedAsyncioTestCase): async def asyncSetUp(self): self.conn = await connect('./testdb.db') await create(self.conn) return await super().asyncSetUp() async def asyncTearDown(self) -> None: await self.conn.close() pathlib.Path('./testdb.db').unlink(missing_ok=True) return await super().asyncTearDown() async def testCreateAndFind(self) -> None: product = test_data.Products.broccoli await products_db.insert_product(self.conn, product, {}) self.assertIsNotNone(product) self.assertGreater(product.id, 0) product_by_id = await products_db.find_product_by_id(self.conn, product.id) self.assertIsNotNone(product_by_id) self.assertEqual(product_by_id.id, product.id) self.assertEqual(product_by_id.name, product.name) self.assertEqual(product_by_id.link, product.link) self.assertEqual(product_by_id.img_large, product.img_large) self.assertEqual(product_by_id.img_small, product.img_small) import main class TestRecipe(unittest.IsolatedAsyncioTestCase): async def asyncSetUp(self): pathlib.Path('./testdb.db').unlink(missing_ok=True) self.conn = await connect('./testdb.db') await create(self.conn) await test_data.create_persons(self.conn) return await super().asyncSetUp() async def asyncTearDown(self) -> None: await self.conn.close() pathlib.Path('./testdb.db').unlink(missing_ok=True) return await super().asyncTearDown() async def testCreateAndFind(self) -> None: recipe = test_data.Recipes.broccoli_soup ingredient = recipe.ingredients[0] product = ingredient.product person = test_data.Persons.jacob await products_db.insert_product(self.conn, product, {}) create_response = await main.create_recipe(recipe, self.conn, person) self.assertIsNotNone(create_response) self.assertIsInstance(create_response, recipes_db.Recipe, msg=create_response.body if hasattr(create_response, 'body') else create_response) recipe_by_id = await main.get_recipe(recipe.id, self.conn) self.assertIsNotNone(recipe_by_id) self.assertIsInstance(recipe_by_id, recipes_db.Recipe, msg=recipe_by_id.body if hasattr(recipe_by_id, 'body') else recipe_by_id) self.assertIsNotNone(recipe_by_id) self.assertEqual(recipe_by_id.id, recipe.id) self.assertEqual(recipe_by_id.name, recipe.name) self.assertEqual(recipe_by_id.link, recipe.link) self.assertEqual(recipe_by_id.image_urls, recipe.image_urls) self.assertEqual(len(recipe_by_id.ingredients), 1) self.assertEqual(recipe_by_id.ingredients[0].id, ingredient.id) self.assertEqual(recipe_by_id.ingredients[0].line, ingredient.line) self.assertEqual(recipe_by_id.ingredients[0].name, ingredient.name) self.assertEqual(recipe_by_id.ingredients[0].unit, ingredient.unit) self.assertEqual(recipe_by_id.ingredients[0].quantity, ingredient.quantity) self.assertEqual(recipe_by_id.ingredients[0].preparation, ingredient.preparation) self.assertEqual(recipe_by_id.ingredients[0].product.id, product.id) self.assertEqual(recipe_by_id.ingredients[0].product.name, product.name) self.assertEqual(recipe_by_id.ingredients[0].product.link, product.link) self.assertEqual(recipe_by_id.ingredients[0].product.img_large, product.img_large) self.assertEqual(recipe_by_id.ingredients[0].product.img_small, product.img_small) self.assertEqual(recipe_by_id.created_by.id, person.id) self.assertEqual(recipe_by_id.created_by.name, person.name) import meals class TestMeals(unittest.IsolatedAsyncioTestCase): async def asyncSetUp(self): pathlib.Path('./testdb.db').unlink(missing_ok=True) self.conn = await connect('./testdb.db') await create(self.conn) await test_data.create_persons(self.conn) return await super().asyncSetUp() async def asyncTearDown(self) -> None: await self.conn.close() pathlib.Path('./testdb.db').unlink(missing_ok=True) return await super().asyncTearDown() async def testCreateAndFind(self) -> None: meal = test_data.Meals.broccoli_soup_for_jacob recipe = meal.recipes[0] recipe_ingredient = recipe.ingredients[0] recipe_product = recipe_ingredient.product extra_ingredient = meal.extra_ingredients[0] extra_product = extra_ingredient.product person = test_data.Persons.jacob await products_db.insert_product(self.conn, recipe_product, {}) await products_db.insert_product(self.conn, extra_product, {}) created_recipe = await main.create_recipe(recipe, self.conn, person) recipe.id = created_recipe.id create_response = await main.create_meal(meal, self.conn) self.assertIsNotNone(create_response) self.assertIsInstance(create_response, meals.Meal, msg=create_response.body if hasattr(create_response, 'body') else create_response) meal_by_id = await main.get_meal(meal.id, self.conn) self.assertIsNotNone(meal_by_id) self.assertIsInstance(meal_by_id, meals.Meal, msg=meal_by_id.body if hasattr(meal_by_id, 'body') else meal_by_id) self.assertIsNotNone(meal_by_id) self.assertEqual(meal_by_id.id, meal.id) self.assertEqual(meal_by_id.meal_date, meal.meal_date) self.assertEqual(len(meal_by_id.chefs), 1) self.assertEqual(meal_by_id.chefs[0].id, meal.chefs[0].id) self.assertEqual(meal_by_id.chefs[0].name, meal.chefs[0].name) self.assertEqual(len(meal_by_id.cleanup), 1) self.assertEqual(meal_by_id.cleanup[0].id, meal.cleanup[0].id) self.assertEqual(meal_by_id.cleanup[0].name, meal.cleanup[0].name) self.assertEqual(len(meal_by_id.consumers), 2) expected = [p.id for p in meal.consumers] actual = [p.id for p in meal_by_id.consumers] self.assertEqual(sorted(expected), sorted(actual)) self.assertEqual(meal_by_id.recipes[0].id, recipe.id) self.assertEqual(meal_by_id.recipes[0].name, recipe.name) self.assertEqual(meal_by_id.recipes[0].link, recipe.link) self.assertEqual(meal_by_id.recipes[0].image_urls, recipe.image_urls) self.assertEqual(len(meal_by_id.recipes[0].ingredients), 1) self.assertEqual(meal_by_id.recipes[0].ingredients[0].id, recipe_ingredient.id) self.assertEqual(meal_by_id.recipes[0].ingredients[0].line, recipe_ingredient.line) self.assertEqual(meal_by_id.extra_ingredients[0].id, extra_ingredient.id) self.assertEqual(meal_by_id.extra_ingredients[0].line, extra_ingredient.line) meals_by_date_range = await main.get_meals(meal.meal_date, meal.meal_date, self.conn) self.assertIsNotNone(meals_by_date_range) self.assertIsInstance(meals_by_date_range, list, msg=meals_by_date_range.body if hasattr(meals_by_date_range, 'body') else meals_by_date_range) self.assertEqual(len(meals_by_date_range), 1) self.assertEqual(meals_by_date_range[0].id, meal.id) self.assertEqual(meals_by_date_range[0].meal_date, meal.meal_date) self.assertEqual(len(meals_by_date_range[0].chefs), 1) self.assertEqual(meals_by_date_range[0].chefs[0].id, meal.chefs[0].id) self.assertEqual(meals_by_date_range[0].chefs[0].name, meal.chefs[0].name) self.assertEqual(len(meals_by_date_range[0].cleanup), 1) self.assertEqual(meals_by_date_range[0].cleanup[0].id, meal.cleanup[0].id) self.assertEqual(meals_by_date_range[0].cleanup[0].name, meal.cleanup[0].name) self.assertEqual(len(meals_by_date_range[0].consumers), 2) self.assertEqual(meals_by_date_range[0].recipes[0].id, recipe.id) self.assertEqual(meals_by_date_range[0].recipes[0].name, recipe.name) if __name__ == '__main__': unittest.main()