feat: Default To Fractions When Unit Is Empty (#3587)

Co-authored-by: Hayden <64056131+hay-kot@users.noreply.github.com>
This commit is contained in:
Michael Genson 2024-05-12 14:15:26 -05:00 committed by GitHub
parent 554b3fa749
commit c82549ccb4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 22 additions and 7 deletions

View File

@ -34,6 +34,12 @@ describe(parseIngredientText.name, () => {
expect(parseIngredientText(ingredient, false, 1, true)).contain("1 <sup>1</sup>").and.to.contain("<sub>2</sub>");
});
test("ingredient text with fraction when unit is null", () => {
const ingredient = createRecipeIngredient({ quantity: 1.5, unit: undefined });
expect(parseIngredientText(ingredient, false, 1, true)).contain("1 <sup>1</sup>").and.to.contain("<sub>2</sub>");
});
test("ingredient text with fraction no formatting", () => {
const ingredient = createRecipeIngredient({ quantity: 1.5, unit: { fraction: true, id: "1", name: "cup" } });
const result = parseIngredientText(ingredient, false, 1, false);

View File

@ -53,7 +53,9 @@ export function useParsedIngredientText(ingredient: RecipeIngredient, disableAmo
// casting to number is required as sometimes quantity is a string
if (quantity && Number(quantity) !== 0) {
if (unit?.fraction) {
if (unit && !unit.fraction) {
returnQty = (quantity * scale).toString();
} else {
const fraction = frac(quantity * scale, 10, true);
if (fraction[0] !== undefined && fraction[0] > 0) {
returnQty += fraction[0];
@ -64,8 +66,6 @@ export function useParsedIngredientText(ingredient: RecipeIngredient, disableAmo
` <sup>${fraction[1]}</sup>&frasl;<sub>${fraction[2]}</sub>` :
` ${fraction[1]}/${fraction[2]}`;
}
} else {
returnQty = (quantity * scale).toString();
}
}

View File

@ -187,7 +187,7 @@ class RecipeIngredientBase(MealieModel):
qty: float | Fraction
# decimal
if not self.unit or not self.unit.fraction:
if self.unit and not self.unit.fraction:
qty = round(self.quantity or 0, INGREDIENT_QTY_PRECISION)
if qty.is_integer():
return str(int(qty))

View File

@ -2,8 +2,11 @@ from uuid import uuid4
import pytest
from mealie.schema.recipe.recipe_ingredient import IngredientFood, IngredientUnit, RecipeIngredient
from tests.utils.factories import random_string
from mealie.schema.recipe.recipe_ingredient import (
IngredientFood,
IngredientUnit,
RecipeIngredient,
)
@pytest.mark.parametrize(
@ -19,6 +22,12 @@ from tests.utils.factories import random_string
@pytest.mark.parametrize(
["unit", "expect_display_fraction", "expected_unit_singular_string", "expected_unit_plural_string"],
[
[
None,
True,
"",
"",
],
[
IngredientUnit(
id=uuid4(),
@ -154,7 +163,7 @@ def test_ingredient_display(
quantity: float | None,
quantity_display_decimal: str,
quantity_display_fraction: str,
unit: IngredientUnit,
unit: IngredientUnit | None,
food: IngredientFood,
note: str,
use_food: bool,