1
0
Fork 0
mirror of https://github.com/mealie-recipes/mealie.git synced 2025-08-05 13:35:23 +02:00

feat: Recipe Finder (aka Cocktail Builder) (#4542)

This commit is contained in:
Michael Genson 2024-12-03 07:27:41 -06:00 committed by GitHub
parent d26e29d1c5
commit 4e0cf985bc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 1959 additions and 151 deletions

View file

@ -1,5 +1,6 @@
import random
from typing import Any
from uuid import uuid4
import pytest
from fastapi.testclient import TestClient
@ -8,6 +9,7 @@ from pydantic import UUID4
from mealie.schema.cookbook.cookbook import SaveCookBook
from mealie.schema.recipe.recipe import Recipe
from mealie.schema.recipe.recipe_category import TagSave
from mealie.schema.recipe.recipe_ingredient import RecipeIngredient, SaveIngredientFood
from tests.utils import api_routes
from tests.utils.factories import random_int, random_string
from tests.utils.fixture_schemas import TestUser
@ -335,3 +337,71 @@ def test_public_recipe_cookbook_filter_with_recipes(
assert str(other_household_recipe.id) not in recipe_ids
else:
assert str(other_household_recipe.id) in recipe_ids
@pytest.mark.parametrize("is_private_group", [True, False])
@pytest.mark.parametrize("is_private_household", [True, False])
@pytest.mark.parametrize("is_private_recipe", [True, False])
def test_get_suggested_recipes(
api_client: TestClient,
unique_user: TestUser,
random_recipe: Recipe,
is_private_group: bool,
is_private_household: bool,
is_private_recipe: bool,
):
database = unique_user.repos
## Set Up Group
group = database.groups.get_one(unique_user.group_id)
assert group and group.preferences
group.preferences.private_group = is_private_group
database.group_preferences.update(group.id, group.preferences)
## Set Up Household
household = database.households.get_one(unique_user.household_id)
assert household and household.preferences
household.preferences.private_household = is_private_household
household.preferences.recipe_public = not is_private_household
database.household_preferences.update(household.id, household.preferences)
## Set Recipe `settings.public` attribute
assert random_recipe.settings
random_recipe.settings.public = not is_private_recipe
database.recipes.update(random_recipe.slug, random_recipe)
## Add a known food to the recipe
known_food = database.ingredient_foods.create(
SaveIngredientFood(id=uuid4(), name=random_string(), group_id=unique_user.group_id)
)
random_recipe.recipe_ingredient = [RecipeIngredient(food_id=known_food.id, food=known_food)]
random_recipe.settings.disable_amount = False
database.recipes.update(random_recipe.slug, random_recipe)
## Try to find suggested recipes
recipe_group = database.groups.get_by_slug_or_id(random_recipe.group_id)
recipe_household = database.households.get_by_slug_or_id(random_recipe.household_id)
assert recipe_group
assert recipe_household
response = api_client.get(
api_routes.explore_groups_group_slug_recipes_suggestions(recipe_group.slug),
params={"maxMissingFoods": 0, "foods": [str(known_food.id)], "includeFoodsOnHand": False},
)
if is_private_group:
assert response.status_code == 404
assert response.json()["detail"] == "group not found"
return
if is_private_household or is_private_recipe:
if is_private_group:
assert response.json()["detail"] == "group not found"
else:
assert response.json()["items"] == []
return
as_json = response.json()
assert len(as_json["items"]) == 1
assert as_json["items"][0]["recipe"]["name"] == random_recipe.name
assert as_json["items"][0]["recipe"]["slug"] == random_recipe.slug