fix: fall back to servings for recipe yield display (#7881)
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com> Co-authored-by: Michael Genson <genson.michael@gmail.com>
This commit is contained in:
2 files changed
+44
-1
No files matched your search
@@ -166,7 +166,11 @@ class RecipeSummary(MealieModel):
|
||||
|
||||
@property
|
||||
def recipe_yield_display(self) -> str:
|
||||
return f"{self.recipe_yield_quantity} {self.recipe_yield}".strip()
|
||||
# Fall back to recipe_servings when no yield is set at all, otherwise
|
||||
# a servings-only recipe emits a bare "0.0" (or "0.0 None") into schema.org.
|
||||
quantity = self.recipe_yield_quantity or (self.recipe_servings if not self.recipe_yield else 0)
|
||||
number = f"{quantity:g}" if quantity else ""
|
||||
return f"{number} {self.recipe_yield or ''}".strip()
|
||||
|
||||
@classmethod
|
||||
def loader_options(cls) -> list[LoaderOption]:
|
||||
|
||||
@@ -63,6 +63,45 @@ def test_recipe_string_sanitation(field: str, val: Any, expected: Any):
|
||||
assert getattr(recipe, field) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
["recipe_yield_quantity", "recipe_yield", "recipe_servings", "expected"],
|
||||
[
|
||||
# Explicit yield quantity + text
|
||||
(8, "slices", 0, "8 slices"),
|
||||
# Whole-number quantities render without a trailing ".0"
|
||||
(8, "servings", 0, "8 servings"),
|
||||
# Fractional quantities are preserved
|
||||
(2.25, "loaves", 0, "2.25 loaves"),
|
||||
# Yield text only, no quantity
|
||||
(0, "a dozen cookies", 0, "a dozen cookies"),
|
||||
# Yield text only, with servings set: servings don't get prefixed onto the text
|
||||
(0, "a dozen cookies", 4, "a dozen cookies"),
|
||||
# Servings-only recipe: falls back to recipe_servings instead of "0.0"
|
||||
(0, None, 4, "4"),
|
||||
(0, "", 4, "4"),
|
||||
# Nothing set at all
|
||||
(0, None, 0, ""),
|
||||
],
|
||||
)
|
||||
def test_recipe_yield_display(
|
||||
recipe_yield_quantity: float,
|
||||
recipe_yield: str | None,
|
||||
recipe_servings: float,
|
||||
expected: str,
|
||||
):
|
||||
recipe = RecipeSummary(
|
||||
id=uuid4(),
|
||||
user_id=uuid4(),
|
||||
household_id=uuid4(),
|
||||
group_id=uuid4(),
|
||||
recipe_yield_quantity=recipe_yield_quantity,
|
||||
recipe_yield=recipe_yield,
|
||||
recipe_servings=recipe_servings,
|
||||
)
|
||||
|
||||
assert recipe.recipe_yield_display == expected
|
||||
|
||||
|
||||
def test_recipe_preserves_existing_slug():
|
||||
recipe = RecipeSummary(
|
||||
id=uuid4(),
|
||||
|
||||
Reference in new issue
Block a user