def test_delete_success(self): with self.app_context(): with self.app() as client: recipe_id, _ = RecipeModel("Title1", "Url1", "Ingredients1, test1").save_to_db() FavouriteRecipesModel(1, recipe_id, self.current_time, "Meat", "The most delicious ever!").save_to_db() self.assertIsNotNone( RecipeModel.find_by_recipe_id(1), "While running TestFavouriteRecipeResource.test_delete_success()," "the recipe was not saved successfully to db.") self.assertIsNotNone( FavouriteRecipesModel.find_by_recipe_id(1), "While running TestFavouriteRecipeResource.test_delete_success()," "the favourite recipe was not saved successfully to db.") response = client.delete( f"{URL}/favourite_recipe/1", headers={"Authorization": f"Bearer {self.access_token}"}) expected = { "message": f"Recipe with the id 1 removed successfully from your favourites!" } self.assertDictEqual( json.loads(response.data), expected, "Incorrect message returned while trying to successfully delete " "a favourite recipe.") self.assertEqual( response.status_code, 200, "Incorrect status code returned while trying to successfully delete " "a favourite recipe.") self.assertIsNone( RecipeModel.find_by_recipe_id(1), "While running TestFavouriteRecipeResource.test_delete_success()," "the recipe was not deleted successfully from db.") self.assertListEqual( FavouriteRecipesModel.find_by_recipe_id(1), [], "While running TestFavouriteRecipeResource.test_delete_success()," "the favourite recipe was not deleted successfully from db." )
def get(self, recipe_id): recipe_obj = RecipeModel.find_by_recipe_id(recipe_id) if recipe_obj: user_id = get_jwt_identity() favourite = FavouriteRecipesModel.find_by_recipe_id_user_id(recipe_id, user_id) if favourite: recipe_to_display = favourite.json() return {"Recipe": recipe_to_display}, 200 return {'message': "Sorry, I couldn't find this recipe in your favourites."}, 404
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." )