1
0
Fork 0
mirror of https://github.com/mealie-recipes/mealie.git synced 2025-08-06 22:15:22 +02:00

feat: Move "on hand" and "last made" to household (#4616)

Co-authored-by: Kuchenpirat <24235032+Kuchenpirat@users.noreply.github.com>
This commit is contained in:
Michael Genson 2025-01-13 10:19:49 -06:00 committed by GitHub
parent e565b919df
commit e9892aba89
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
53 changed files with 1618 additions and 400 deletions

View file

@ -1,12 +1,13 @@
from datetime import UTC, datetime, timedelta
from dateutil.parser import parse as parse_dt
from fastapi.testclient import TestClient
from pydantic import UUID4
from mealie.schema.household.household import HouseholdRecipeSummary
from mealie.schema.meal_plan.new_meal import CreatePlanEntry
from mealie.schema.recipe.recipe import RecipeSummary
from mealie.schema.recipe.recipe import RecipeLastMade, RecipeSummary
from mealie.services.scheduler.tasks.create_timeline_events import create_mealplan_timeline_events
from tests import utils
from tests.utils import api_routes
from tests.utils.factories import random_int, random_string
from tests.utils.fixture_schemas import TestUser
@ -17,7 +18,7 @@ def test_no_mealplans():
create_mealplan_timeline_events()
def test_new_mealplan_event(api_client: TestClient, unique_user: TestUser):
def test_new_mealplan_event(api_client: TestClient, unique_user: TestUser, h2_user: TestUser):
recipe_name = random_string(length=25)
response = api_client.post(api_routes.recipes, json={"name": recipe_name}, headers=unique_user.token)
assert response.status_code == 201
@ -65,7 +66,7 @@ def test_new_mealplan_event(api_client: TestClient, unique_user: TestUser):
response = api_client.get(api_routes.recipes_slug(recipe_name), headers=unique_user.token)
new_recipe_data: dict = response.json()
recipe = RecipeSummary.model_validate(new_recipe_data)
assert recipe.last_made.date() == datetime.now(UTC).date() # type: ignore
assert recipe.last_made and recipe.last_made.date() == datetime.now(UTC).date()
# make sure nothing else was updated
for data in [original_recipe_data, new_recipe_data]:
@ -85,6 +86,19 @@ def test_new_mealplan_event(api_client: TestClient, unique_user: TestUser):
assert original_recipe_data == new_recipe_data
# make sure the user's last made date was updated
response = api_client.get(api_routes.households_self_recipes_recipe_slug(recipe_name), headers=unique_user.token)
assert response.status_code == 200
response_json = response.json()
assert response_json["lastMade"]
assert parse_dt(response_json["lastMade"]).date() == datetime.now(UTC).date()
# make sure the other user's last made date was not updated
response = api_client.get(api_routes.households_self_recipes_recipe_slug(recipe_name), headers=h2_user.token)
assert response.status_code == 200
response_json = response.json()
assert response_json["lastMade"] is None
def test_new_mealplan_event_duplicates(api_client: TestClient, unique_user: TestUser):
recipe_name = random_string(length=25)
@ -191,7 +205,7 @@ def test_new_mealplan_events_with_multiple_recipes(api_client: TestClient, uniqu
assert len(response_json["items"]) == target_count
def test_preserve_future_made_date(api_client: TestClient, unique_user: TestUser):
def test_preserve_future_made_date(api_client: TestClient, unique_user: TestUser, h2_user: TestUser):
recipe_name = random_string(length=25)
response = api_client.post(api_routes.recipes, json={"name": recipe_name}, headers=unique_user.token)
assert response.status_code == 201
@ -201,12 +215,22 @@ def test_preserve_future_made_date(api_client: TestClient, unique_user: TestUser
recipe_id = str(recipe.id)
future_dt = datetime.now(UTC) + timedelta(days=random_int(1, 10))
recipe.last_made = future_dt
response = api_client.put(
api_routes.recipes_slug(recipe.slug), json=utils.jsonify(recipe), headers=unique_user.token
response = api_client.patch(
api_routes.recipes_slug_last_made(recipe.slug),
data=RecipeLastMade(timestamp=future_dt).model_dump_json(),
headers=unique_user.token,
)
assert response.status_code == 200
# verify the last made date was updated only on unique_user
response = api_client.get(api_routes.households_self_recipes_recipe_slug(recipe.slug), headers=unique_user.token)
household_recipe = HouseholdRecipeSummary.model_validate(response.json())
assert household_recipe.last_made == future_dt
response = api_client.get(api_routes.households_self_recipes_recipe_slug(recipe.slug), headers=h2_user.token)
household_recipe = HouseholdRecipeSummary.model_validate(response.json())
assert household_recipe.last_made is None
new_plan = CreatePlanEntry(date=datetime.now(UTC).date(), entry_type="dinner", recipe_id=recipe_id).model_dump(
by_alias=True
)
@ -216,9 +240,14 @@ def test_preserve_future_made_date(api_client: TestClient, unique_user: TestUser
response = api_client.post(api_routes.households_mealplans, json=new_plan, headers=unique_user.token)
assert response.status_code == 201
# run the task and make sure the recipe's last made date was not updated
# run the task and make sure the recipe's last made date was not updated for either user
create_mealplan_timeline_events()
response = api_client.get(api_routes.recipes_slug(recipe_name), headers=unique_user.token)
recipe = RecipeSummary.model_validate(response.json())
assert recipe.last_made == future_dt
response = api_client.get(api_routes.households_self_recipes_recipe_slug(recipe.slug), headers=unique_user.token)
assert response.status_code == 200
household_recipe = HouseholdRecipeSummary.model_validate(response.json())
assert household_recipe.last_made == future_dt
response = api_client.get(api_routes.households_self_recipes_recipe_slug(recipe.slug), headers=h2_user.token)
household_recipe = HouseholdRecipeSummary.model_validate(response.json())
assert household_recipe.last_made is None