diff --git a/mealie/schema/recipe/recipe_ingredient.py b/mealie/schema/recipe/recipe_ingredient.py index 61cac7db83..e77ad09710 100644 --- a/mealie/schema/recipe/recipe_ingredient.py +++ b/mealie/schema/recipe/recipe_ingredient.py @@ -74,7 +74,7 @@ def validate_quantity(cls, value, values) -> NoneFloat: empty string. """ if isinstance(value, float): - return value + return round(value, 3) if value is None or value == "": return None return value diff --git a/mealie/services/parser_services/crfpp/processor.py b/mealie/services/parser_services/crfpp/processor.py index 6152575539..d0bf1b69cc 100644 --- a/mealie/services/parser_services/crfpp/processor.py +++ b/mealie/services/parser_services/crfpp/processor.py @@ -37,7 +37,7 @@ def validate_qty(qty, values): # sourcery skip: merge-nested-ifs # Check if other contains a fraction try: if values["other"] is not None and values["other"].find("/") != -1: - return float(Fraction(values["other"])).__round__(1) + return round(float(Fraction(values["other"])), 3) else: return 1 except Exception: diff --git a/mealie/services/parser_services/ingredient_parser.py b/mealie/services/parser_services/ingredient_parser.py index e7766b4138..0762e6b8ad 100644 --- a/mealie/services/parser_services/ingredient_parser.py +++ b/mealie/services/parser_services/ingredient_parser.py @@ -74,7 +74,7 @@ def _crf_to_ingredient(self, crf_model: crfpp.CRFIngredient) -> ParsedIngredient unit=CreateIngredientUnit(name=crf_model.unit), food=CreateIngredientFood(name=crf_model.name), disable_amount=False, - quantity=float(sum(Fraction(s) for s in crf_model.qty.split())), + quantity=float(sum(Fraction(s).limit_denominator(32) for s in crf_model.qty.split())), ) except Exception as e: logger.error(f"Failed to parse ingredient: {crf_model}: {e}") diff --git a/tests/unit_tests/test_ingredient_parser.py b/tests/unit_tests/test_ingredient_parser.py index c5804c57e6..f9b4029246 100644 --- a/tests/unit_tests/test_ingredient_parser.py +++ b/tests/unit_tests/test_ingredient_parser.py @@ -26,8 +26,12 @@ def crf_exists() -> bool: test_ingredients = [ TestIngredient("½ cup all-purpose flour", 0.5, "cup", "all-purpose flour", ""), TestIngredient("1 ½ teaspoons ground black pepper", 1.5, "teaspoon", "black pepper", "ground"), - TestIngredient("⅔ cup unsweetened flaked coconut", 0.7, "cup", "coconut", "unsweetened flaked"), - TestIngredient("⅓ cup panko bread crumbs", 0.3, "cup", "panko bread crumbs", ""), + TestIngredient("⅔ cup unsweetened flaked coconut", 0.667, "cup", "coconut", "unsweetened flaked"), + TestIngredient("⅓ cup panko bread crumbs", 0.333, "cup", "panko bread crumbs", ""), + # Small Fraction Tests - PR #1369 + # Reported error is was for 1/8 - new lowest expected threshold is 1/32 + TestIngredient("1/8 cup all-purpose flour", 0.125, "cup", "all-purpose flour", ""), + TestIngredient("1/32 cup all-purpose flour", 0.03125, "cup", "all-purpose flour", ""), ]