def setUp(self) -> None: super(TestUsersFavouriteRecipes, self).setUp() with self.app_context(): self.user = UserModel("TestUsername", "TestPwd1!") self.user.save_to_db() recipe = RecipeModel("Title1", "Url1", "Ingredients1, test1") recipe.save_to_db() self.current_time = datetime.utcnow() self.f_recipe = FavouriteRecipesModel(self.user.user_id, recipe.recipe_id, self.current_time, "Meat", "The most delicious ever!")
def put(self, name): data = Recipe.parser.parse_args() recipe = RecipeModel.find_by_name(name) if recipe: recipe.instructions = data['instructions'] else: recipe = RecipeModel(name, data['instructions']) recipe.save_to_db() return recipe.json()
def test_show_mine(self): with self.app_context(): self.f_recipe.save_to_db() recipe2 = RecipeModel("Title2", "Url2", "Ingredients2, test2") recipe2.save_to_db() f_recipe2 = FavouriteRecipesModel(self.user.user_id, recipe2.recipe_id, self.current_time) f_recipe2.save_to_db() my_recipes = [ x.json() for x in FavouriteRecipesModel.show_mine(self.user.user_id) ] expected = [{ "recipe_id": 1, "save_date": datetime.strftime(self.current_time, "%Y-%m-%d %H:%M"), "category": "Meat", "comment": "The most delicious ever!", "recipe_title": "Title1", "recipe_link": "Url1", "ingredients": "Ingredients1, test1" }, { "recipe_id": 2, "save_date": datetime.strftime(self.current_time, "%Y-%m-%d %H:%M"), "category": None, "comment": None, "recipe_title": "Title2", "recipe_link": "Url2", "ingredients": "Ingredients2, test2" }] self.assertListEqual( my_recipes, expected, "FavouriteRecipesModel.show_mine() query does not return proper values." )
def test_show_all(self): with self.app_context(): self.f_recipe.save_to_db() recipe2 = RecipeModel("Title2", "Url2", "Ingredients2, test2") recipe2.save_to_db() f_recipe2 = FavouriteRecipesModel(self.user.user_id, recipe2.recipe_id, self.current_time) f_recipe2.save_to_db() all_recipes = FavouriteRecipesModel.show_all() self.assertEqual( len(all_recipes), 2, "FavouriteRecipesModel.show_all() does not query the db appropriately." )
def test_show_my_recipe_ids(self): with self.app_context(): self.f_recipe.save_to_db() recipe2 = RecipeModel("Title2", "Url2", "Ingredients2, test2") recipe2.save_to_db() f_recipe2 = FavouriteRecipesModel(self.user.user_id, recipe2.recipe_id, self.current_time) f_recipe2.save_to_db() my_recipes_ids = FavouriteRecipesModel.show_my_recipe_ids( self.user.user_id) self.assertListEqual( my_recipes_ids, [(1, ), (2, )], "FavouriteRecipesModel.show_my_recipe_ids() returns wrong values." )
def post(self): data = self.parser.parse_args() if RecipeModel.find_by_name(data['name']): return { 'message': "A recipe with name '{}' already exists.".format(data['name']) }, 400 recipe = RecipeModel(**data) try: recipe.save_to_db() except: return {"message": "An error occurred creating the recipe."}, 500 return recipe.json(), 201
def post(self, name): if RecipeModel.find_by_name(name): return { 'message': "A recipe with name '{}' already exists.".format(name) }, 400 data = Recipe.parser.parse_args() recipe = RecipeModel(name, data['instructions'], data['category_id']) try: recipe.save_to_db() except: return {"message": "An error occurred inserting the recipe."}, 500 return recipe.json(), 201
def test_show_stats(self): with self.app_context(): self.f_recipe.save_to_db() recipe2 = RecipeModel("Title2", "Url2", "Ingredients2, test2") recipe2.save_to_db() f_recipe2 = FavouriteRecipesModel(self.user.user_id, recipe2.recipe_id, self.current_time) f_recipe2.save_to_db() user2 = UserModel("TestUsername2", "TestPwd1!") user2.save_to_db() f_rec_user2 = FavouriteRecipesModel(user2.user_id, recipe2.recipe_id, self.current_time) f_rec_user2.save_to_db() stats = [(rm.recipe_id, stat) for (rm, stat) in FavouriteRecipesModel.show_stats()] expected = [(1, 1), (2, 2)] self.assertListEqual( stats, expected, "FavouriteRecipesModel.show_stats() produces improper stats.")
def test_crud(self): with self.app_context(): recipe = RecipeModel("Title1", "Url1", "Ingredients1, test1") self.assertIsNone( RecipeModel.find_by_href("Url1"), f"Recipe object with a href {recipe.href} should not exist in the table." ) recipe.save_to_db() self.assertIsNotNone( RecipeModel.find_by_href("Url1"), f"Recipe object with a link {recipe.href} failed to save to db" ) self.assertIsNotNone( RecipeModel.find_by_recipe_id(1), f"Recipe did not find by its (manually created) id.") self.assertEqual( RecipeModel.find_by_href("Url1").recipe_title, "Title1", f"Recipe title does not equal the desired one {recipe.recipe_title}" ) recipe.delete_from_db(1) self.assertIsNone( RecipeModel.find_by_href("Url1"), "Recipe object found in db, while it should already be deleted." )
def put(self, recipe_id): data = NewRecipe.parser.parse_args() recipe = RecipeModel.find_by_id(recipe_id) if recipe: if recipe.chef_id == data['chef_id']: recipe.name = data['name'] recipe.recipe_type = data['recipe_type'] recipe.description = data['description'] recipe.steps = data['steps'] recipe.servings = data['servings'] recipe.prep_min = data['prep_min'] recipe.cook_min = data['cook_min'] else: return { 'message': 'You can not change this recipe as you are not the chef.' }, 401 else: recipe = RecipeModel(**data) recipe.save_to_db() return recipe.json(), 201
def test_count_all(self): with self.app_context(): recipe1 = RecipeModel("Title1", "Url1", "Ingredients1, test1") recipe2 = RecipeModel("Title2", "Url2", "Ingredients1, test2") recipe3 = RecipeModel("Title3", "Url3", "Ingredients1, test3") recipe1.save_to_db() recipe2.save_to_db() recipe3.save_to_db() no_of_recipes = RecipeModel.count_all()[0] self.assertEqual( no_of_recipes, 3, "RecipeModel.count_all() failed to count a proper number of recipes" )
def post(self, value): recipe_data = Recipe.recipe_parser.parse_args() portion_sum = 0 if RecipeModel.find_by_value(value): return {'message': "Recipe '{}' already exist".format(value)}, 400 recipe = RecipeModel(value, recipe_data['name']) for mixing in iter(recipe_data['ingredients_list']): portion_sum = portion_sum + mixing['portion'] if portion_sum > 200: return {"meessage": "portion over limit"} try: recipe_id = recipe.save_to_db() for mixing in iter(recipe_data['ingredients_list']): mixing_model = MixingModel(mixing['ingredient_id'], recipe_id, mixing['portion']) mixing_model.save_to_db() except: return {'message': 'An error occured during saving to DB'}, 500 return recipe.json(), 201