import pytest from pydantic import ValidationError from mealie.schema.recipe.recipe_step import IngredientReferences, RecipeStep, StepCooking def test_legacy_step_and_explicit_zero(): step = RecipeStep(text="Mix", ingredient_references=[IngredientReferences(quantity=0)]) assert step.cooking is None assert step.ingredient_references[0].quantity == 0 assert IngredientReferences().quantity is None @pytest.mark.parametrize("speed", [1, 0.5, "spoon"]) def test_cooking_speeds(speed): assert StepCooking(speed=speed).speed == speed @pytest.mark.parametrize("quantity", [-1, float("inf"), float("nan")]) def test_invalid_quantity(quantity): with pytest.raises(ValidationError): IngredientReferences(quantity=quantity) def test_nonfinite_speed_rejected(): with pytest.raises(ValidationError): StepCooking(speed=float("inf")) def test_unknown_cooking_settings_are_not_silently_discarded(): with pytest.raises(ValidationError): StepCooking.model_validate({"speed": 2, "unknownSetting": True}) assert "reverse" in StepCooking.model_json_schema(by_alias=True)["properties"] @pytest.mark.parametrize("reverse", [True, False, None]) def test_rotation_direction_round_trip(reverse): step = RecipeStep.model_validate({"text": "Mix", "cooking": {"speed": "spoon", "reverse": reverse}}) saved = RecipeStep.model_validate_json(step.model_dump_json(by_alias=True)) assert saved.cooking.reverse is reverse assert StepCooking().reverse is None @pytest.mark.parametrize("reverse", ["false", "true", 0, 1, "inversa"]) def test_invalid_rotation_direction_rejected(reverse): with pytest.raises(ValidationError): StepCooking(reverse=reverse)