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 post(self): data_parser = reqparse.RequestParser() data_parser.add_argument("title", required=True, type=str, help="Please add recipe's title") data_parser.add_argument("href", required=True, type=str, help="Please add recipe's url") data_parser.add_argument("ingredients", required=True, type=str, help="Please add recipe's ingredients") data_parser.add_argument("category", required=False, type=str, help="You can categorize this recipe") data_parser.add_argument("comment", required=False, type=str, help="You can add a comment to this recipe") data = data_parser.parse_args() user_id = get_raw_jwt()['identity'] current_time = datetime.utcnow() recipe_obj = RecipeModel.find_by_href(data['href']) if recipe_obj: recipe_id = recipe_obj.recipe_id else: recipe_id, recipe_obj = RecipeModel(data['title'], data['href'], data['ingredients']).save_to_db() if FavouriteRecipesModel.find_by_recipe_id_user_id(recipe_id, user_id): return {"message": "You have already this recipe saved in your favourites. " "If you want to update it, use the update endpoint."}, 400 favourite = FavouriteRecipesModel(user_id, recipe_id, current_time, data['category'], data['comment']) favourite.save_to_db() just_saved_recipe_display = favourite.json() return {'message': "Successfully saved", "saved_recipe": just_saved_recipe_display}, 201