mirror of
https://github.com/hay-kot/mealie.git
synced 2025-08-22 06:23:34 -07:00
fix: Add Recipe From Another Household To Shopping List (#5892)
This commit is contained in:
parent
db765b0693
commit
6cbc308d83
2 changed files with 67 additions and 1 deletions
|
@ -3,6 +3,7 @@ from typing import cast
|
||||||
from pydantic import UUID4
|
from pydantic import UUID4
|
||||||
|
|
||||||
from mealie.core.exceptions import UnexpectedNone
|
from mealie.core.exceptions import UnexpectedNone
|
||||||
|
from mealie.repos.all_repositories import get_repositories
|
||||||
from mealie.repos.repository_factory import AllRepositories
|
from mealie.repos.repository_factory import AllRepositories
|
||||||
from mealie.schema.household.group_shopping_list import (
|
from mealie.schema.household.group_shopping_list import (
|
||||||
ShoppingListAddRecipeParamsBulk,
|
ShoppingListAddRecipeParamsBulk,
|
||||||
|
@ -303,7 +304,10 @@ class ShoppingListService:
|
||||||
"""Generates a list of new list items based on a recipe"""
|
"""Generates a list of new list items based on a recipe"""
|
||||||
|
|
||||||
if recipe_ingredients is None:
|
if recipe_ingredients is None:
|
||||||
recipe = self.repos.recipes.get_one(recipe_id, "id")
|
group_recipes_repo = get_repositories(
|
||||||
|
self.repos.session, group_id=self.repos.group_id, household_id=None
|
||||||
|
).recipes
|
||||||
|
recipe = group_recipes_repo.get_one(recipe_id, "id")
|
||||||
if not recipe:
|
if not recipe:
|
||||||
raise UnexpectedNone("Recipe not found")
|
raise UnexpectedNone("Recipe not found")
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
import pytest
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
from mealie.schema.household.group_shopping_list import (
|
from mealie.schema.household.group_shopping_list import (
|
||||||
|
@ -244,6 +245,67 @@ def test_shopping_lists_add_recipes(
|
||||||
assert refs_by_id[str(recipe.id)]["recipeQuantity"] == 2
|
assert refs_by_id[str(recipe.id)]["recipeQuantity"] == 2
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("is_private_household", [True, False])
|
||||||
|
@pytest.mark.parametrize("household_lock_recipe_edits", [True, False])
|
||||||
|
def test_shopping_lists_add_cross_household_recipe(
|
||||||
|
api_client: TestClient,
|
||||||
|
unique_user: TestUser,
|
||||||
|
h2_user: TestUser,
|
||||||
|
shopping_lists: list[ShoppingListOut],
|
||||||
|
is_private_household: bool,
|
||||||
|
household_lock_recipe_edits: bool,
|
||||||
|
):
|
||||||
|
sample_list = random.choice(shopping_lists)
|
||||||
|
item_name = random_string(10)
|
||||||
|
|
||||||
|
# set up household
|
||||||
|
household = h2_user.repos.households.get_one(h2_user.household_id)
|
||||||
|
assert household and household.preferences
|
||||||
|
household.preferences.private_household = is_private_household
|
||||||
|
household.preferences.lock_recipe_edits_from_other_households = household_lock_recipe_edits
|
||||||
|
h2_user.repos.household_preferences.update(household.id, household.preferences)
|
||||||
|
|
||||||
|
# set up recipe
|
||||||
|
recipe = h2_user.repos.recipes.create(
|
||||||
|
Recipe(
|
||||||
|
user_id=h2_user.user_id,
|
||||||
|
group_id=h2_user.group_id,
|
||||||
|
name=random_string(10),
|
||||||
|
recipe_ingredient=[{"note": item_name, "quantity": 1}],
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
# add recipe once
|
||||||
|
response = api_client.post(
|
||||||
|
api_routes.households_shopping_lists_item_id_recipe_recipe_id(sample_list.id, recipe.id),
|
||||||
|
headers=unique_user.token,
|
||||||
|
)
|
||||||
|
assert response.status_code == 200
|
||||||
|
response = api_client.get(
|
||||||
|
api_routes.households_shopping_lists_item_id(sample_list.id),
|
||||||
|
headers=unique_user.token,
|
||||||
|
)
|
||||||
|
as_json = utils.assert_deserialize(response, 200)
|
||||||
|
assert len(as_json["listItems"]) == 1
|
||||||
|
assert as_json["listItems"][0]["note"] == item_name
|
||||||
|
assert as_json["listItems"][0]["quantity"] == 1
|
||||||
|
|
||||||
|
# add recipe again
|
||||||
|
response = api_client.post(
|
||||||
|
api_routes.households_shopping_lists_item_id_recipe_recipe_id(sample_list.id, recipe.id),
|
||||||
|
headers=unique_user.token,
|
||||||
|
)
|
||||||
|
assert response.status_code == 200
|
||||||
|
response = api_client.get(
|
||||||
|
api_routes.households_shopping_lists_item_id(sample_list.id),
|
||||||
|
headers=unique_user.token,
|
||||||
|
)
|
||||||
|
as_json = utils.assert_deserialize(response, 200)
|
||||||
|
assert len(as_json["listItems"]) == 1
|
||||||
|
assert as_json["listItems"][0]["note"] == item_name
|
||||||
|
assert as_json["listItems"][0]["quantity"] == 2
|
||||||
|
|
||||||
|
|
||||||
def test_shopping_lists_add_one_with_zero_quantity(
|
def test_shopping_lists_add_one_with_zero_quantity(
|
||||||
api_client: TestClient,
|
api_client: TestClient,
|
||||||
unique_user: TestUser,
|
unique_user: TestUser,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue