def test_delete_mealplan_with_recipes_added_success(self, test_client, logged_in_user, user, db): """ GIVEN a Flask application, user is logged in, meal plan has been created and recipes are in meal plan WHEN user requests meal plan to be deleted THEN meal plan deletion obliges, and all recipes in MealPlanRecipes are deleted """ create_mealplan(test_client) recipe_ids = random.sample(range(1400), 10) for id in recipe_ids: add_to_mealplan(test_client, id) # Add 10 random recipes to meal plan from app.models import MealPlans, MealPlanRecipes mealplan_added = db.session.query(MealPlans).filter(MealPlans.user_id == user.id).first() assert mealplan_added mealplan_id = mealplan_added.mealplan_id mealplan_has_recipes = db.session.query(MealPlanRecipes) \ .filter(MealPlanRecipes.mealplan_id == mealplan_id).first() assert mealplan_has_recipes # MealPlanRecipes associated with meal plan id exists response = delete_mealplan(test_client, mealplan_id) assert b'warning' in response.data mealplan_added = db.session.query(MealPlans).filter(MealPlans.mealplan_id == mealplan_id).first() assert not mealplan_added # meal plan has been deleted mealplan_has_recipes = db.session.query(MealPlanRecipes) \ .filter(MealPlanRecipes.mealplan_id == mealplan_id).first() assert not mealplan_has_recipes # all MealPlanRecipes with associated meal plan id has been deleted
def test_add_and_delete_recipe_from_mealplan(self, test_client, logged_in_user, user, db): """ GIVEN a Flask application, user is logged in and meal plan is created WHEN user adds new recipe to mealplan THEN success message is shown, and the recipe is added to meal plan """ create_mealplan(test_client) from app.models import MealPlans, MealPlanRecipes mealplan_id, = db.session.query(MealPlans.mealplan_id).filter(MealPlans.user_id == user.id).first() # Get current mealplan id recipe_id = random.randint(0, 1400) response = add_to_mealplan(test_client, recipe_id) assert response.status_code == 200 assert b'success' in response.data recipe_is_added = db.session.query(MealPlanRecipes) \ .filter(MealPlanRecipes.mealplan_id == mealplan_id) \ .filter(MealPlanRecipes.recipe_id == recipe_id) assert recipe_is_added # recipe is added to mealplan """ GIVEN a Flask application, user is logged in and recipe has been added to a mealplan WHEN when user removes the recipe from mealplan THEN recipe is removed from mealplan """ response = del_from_mealplan(test_client, mealplan_id, recipe_id) assert response.status_code == 200 assert b'success' in response.data recipe_is_added = db.session.query(MealPlanRecipes) \ .filter(MealPlanRecipes.mealplan_id == mealplan_id) \ .filter(MealPlanRecipes.recipe_id == recipe_id).first() assert not recipe_is_added # recipe is not added to mealplan
def test_add_duplicate_recipes_to_mealplan(self, test_client, logged_in_user): """ GIVEN a Flask application, user is logged in and meal plan is created WHEN user adds the same recipe to mealplan twice THEN error message shows """ create_mealplan(test_client) recipe_id = random.randint(0, 1400) add_to_mealplan(test_client, recipe_id) # Add recipe to meal plan first time response = add_to_mealplan(test_client, recipe_id) # Add recipe to meal plan second time assert b'failure' in response.data
def test_cannot_add_new_mealplan_while_old_mealplan_is_empty(self, test_client, user, logged_in_user, db): """ GIVEN a Flask application and user is logged in WHEN user requests 'create_mealplan' twice (thus on the second request, the active meal plan is empty) THEN appropriate error messages flashes on second attempt """ create_mealplan(test_client) # Create meal plan once response = create_mealplan(test_client) # Create meal plan twice assert b'Your most recent meal plan' in response.data assert b'has no recipes. Please make use of it before' in response.data assert b'creating a new meal plan' in response.data assert response.status_code == 200
def test_mail_grocery_list_fails_if_mealplan_is_empty(self, test_client, db, user, logged_in_user): """ GIVEN a flask mail and logged in user with mealplan created WHEN user requests grocery list sent to email, but no recipes are in meal plan THEN warning message flashes """ from app.models import MealPlans create_mealplan(test_client) mealplan_id, = db.session.query(MealPlans.mealplan_id).filter(MealPlans.user_id == user.id).first() # Get current mealplan id response = send_grocery_list(test_client, mealplan_id) assert b'There are no recipes in this meal plan, so we could not send you a grocery list!' in response.data
def test_grocery_list_contains_ingredients_of_mealplan_recipes(self, test_client, logged_in_user, user, db, recipe_ids): """ GIVEN a Flask application, user is logged in, meal plan is created and user has added 5 recipes to mealplan WHEN user requests to view grocery list of meal plan THEN correct ingredients are shown """ create_mealplan(test_client) from app.models import MealPlans, RecipeIngredients # Get current mealplan id mealplan_id, = db.session.query(MealPlans.mealplan_id).filter(MealPlans.user_id == user.id).first() for id in recipe_ids: add_to_mealplan(test_client, id) # Add 5 random recipes to meal plan response = view_grocery_list(test_client, mealplan_id) query = db.session.query(RecipeIngredients) \ .filter(RecipeIngredients.recipe_id.in_(recipe_ids)) \ .all() # Query where recipe_id in recipe_ids ingredients_list = [i.ingredient for i in query] blockers = ["'", "&", "…"] for ingredient in ingredients_list: # Check that ingredients are in grocery list if "href=" in str(ingredient): # If an ingredient has a link in it, then don't check pass else: ing = [] split = [] ing[:0] = ingredient for blocker in blockers: if blocker in ingredient: split.append(int(ing.index(blocker))) ing.remove(blocker) start = 0 index = 1 for s in split: end = (s + 1) - index search_string = ''.join(ing[start:end]) if "amp;" in search_string: s_list = search_string.split("amp;") # Split string by ampersand and assert them separately assert s_list[0].encode() in response.data else: check = search_string.strip().encode() assert check in response.data start = end index += 1
def test_add_recipes_to_mealplan_and_mealplan_view(self, test_client, user, logged_in_user, db, recipe_ids): """ GIVEN a Flask application, user is logged in, meal plan is created and user has added 5 recipes to mealplan WHEN user requests meal plan view THEN success message is shown, and the recipe is added to meal plan """ create_mealplan(test_client) from app.models import MealPlans # Get current mealplan id mealplan_id, = db.session.query(MealPlans.mealplan_id).filter(MealPlans.user_id == user.id).first() for id in recipe_ids: add_to_mealplan(test_client, id) # Add 5 random recipes to meal plan response = view_mealplan(test_client, mealplan_id) # Navigate to meal plan page response_recipe_ids = get_recipe_ids(test_client, response) # Get recipe ids from recipes on page assert recipe_ids.sort() == response_recipe_ids.sort()
def test_remove_recipe_from_mealplan_when_recipe_already_not_in_mealplan_fails(self, test_client, logged_in_user, user, db): """ GIVEN a Flask application, user is logged in and meal plan is created WHEN user requests to remove a recipe from mealplan while recipe isn't in mealplan THEN error message shows """ create_mealplan(test_client) from app.models import MealPlans, MealPlanRecipes mealplan_id, = db.session.query(MealPlans.mealplan_id).filter(MealPlans.user_id == user.id).first() # Get current mealplan id mealplan_has_recipes = db.session.query(MealPlanRecipes) \ .filter(MealPlanRecipes.mealplan_id == mealplan_id).all() assert not mealplan_has_recipes # Check that mealplan has no recipes recipe_id = random.randint(0, 1400) # Random recipe id to 'remove' from mealplan response = del_from_mealplan(test_client, mealplan_id, recipe_id) assert b'failure' in response.data
def test_delete_mealplan_success(self, test_client, logged_in_user, user, db): """ GIVEN a Flask application, user is logged in and has created a mealplan WHEN user requests to delete a mealplan THEN deletion succeeds """ create_mealplan(test_client) from app.models import MealPlans mealplan_added = db.session.query(MealPlans).filter(MealPlans.user_id == user.id).first() assert mealplan_added mealplan_id = mealplan_added.mealplan_id response = delete_mealplan(test_client, mealplan_id) assert response.status_code == 200 assert b'warning' in response.data mealplan_added = db.session.query(MealPlans).filter(MealPlans.user_id == user.id).first() assert not mealplan_added
def test_mail_grocery_list_succeeds_if_mealplan_is_not_empty(self, test_client, db, user, logged_in_user, recipe_ids): """ GIVEN a flask mail and logged in user with mealplan created WHEN user requests grocery list sent to email, and there are recipes in meal plan THEN email is sent successfully and user is notified """ from app.models import MealPlans create_mealplan(test_client) for id in recipe_ids: add_to_mealplan(test_client, id) mealplan_id, = db.session.query(MealPlans.mealplan_id).filter(MealPlans.user_id == user.id).first() # Get current mealplan id response = send_grocery_list(test_client, mealplan_id) success_message = f"Your grocery list has been sent to {user.email}!" assert (success_message.encode() in response.data) or \ (b'Unfortunately the email could not be sent due to a server error, please try again at a later time.' in response.data)
def test_create_new_mealplan_success(self, test_client, user, logged_in_user, db): """ GIVEN a Flask application and user is logged in WHEN the 'create_mealplan' is requested THEN response is valid and meal plan is added to database, and that you can view the meal plan """ from app.models import MealPlans query = db.session.query(MealPlans).filter(MealPlans.user_id == user.id).first() assert query is None # Assert user has not already created a meal plan response = create_mealplan(test_client) assert b'Success, new meal plan' in response.data assert response.status_code == 200 mealplan_id, = db.session.query(MealPlans.mealplan_id).filter(MealPlans.user_id == user.id).first() assert mealplan_id is not None response = view_mealplan(test_client, mealplan_id) check_message = f"{user.first_name}'s meal plan {mealplan_id}" assert response.status_code == 200 assert check_message.encode() in response.data