fix: staple library — consistent tofu_firm slug, load error handling, typed yield_formats, expanded test coverage

This commit is contained in:
pyr0ball 2026-03-30 23:10:51 -07:00
parent a03807951b
commit e57ae74e27
3 changed files with 42 additions and 14 deletions

View file

@ -6,6 +6,7 @@ from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from typing import Any
import yaml import yaml
@ -21,7 +22,7 @@ class StapleEntry:
base_ingredients: list[str] base_ingredients: list[str]
base_method: str base_method: str
base_time_minutes: int base_time_minutes: int
yield_formats: dict[str, dict] yield_formats: dict[str, Any]
compatible_styles: list[str] compatible_styles: list[str]
@ -42,6 +43,7 @@ class StapleLibrary:
return [s for s in self._staples.values() if label in s.dietary_labels] return [s for s in self._staples.values() if label in s.dietary_labels]
def _load(self, path: Path) -> StapleEntry: def _load(self, path: Path) -> StapleEntry:
try:
data = yaml.safe_load(path.read_text()) data = yaml.safe_load(path.read_text())
return StapleEntry( return StapleEntry(
slug=data["slug"], slug=data["slug"],
@ -54,3 +56,5 @@ class StapleLibrary:
yield_formats=data.get("yield_formats", {}), yield_formats=data.get("yield_formats", {}),
compatible_styles=data.get("compatible_styles", []), compatible_styles=data.get("compatible_styles", []),
) )
except (KeyError, yaml.YAMLError) as exc:
raise ValueError(f"Failed to load staple from {path}: {exc}") from exc

View file

@ -1,4 +1,4 @@
slug: tofu-firm slug: tofu_firm
name: Firm Tofu name: Firm Tofu
description: Pressed soybean curd. Neutral flavor, excellent at absorbing surrounding flavors. Freeze-thaw cycle creates meatier texture. description: Pressed soybean curd. Neutral flavor, excellent at absorbing surrounding flavors. Freeze-thaw cycle creates meatier texture.
dietary_labels: [vegan, high-protein] dietary_labels: [vegan, high-protein]

View file

@ -22,3 +22,27 @@ def test_list_all_staples():
slugs = [s.slug for s in all_staples] slugs = [s.slug for s in all_staples]
assert "seitan" in slugs assert "seitan" in slugs
assert "tempeh" in slugs assert "tempeh" in slugs
def test_tofu_firm_is_loadable():
from app.services.recipe.staple_library import StapleLibrary
lib = StapleLibrary()
tofu = lib.get("tofu_firm")
assert tofu is not None
assert tofu.slug == "tofu_firm"
def test_filter_by_dietary_vegan():
from app.services.recipe.staple_library import StapleLibrary
lib = StapleLibrary()
vegan = lib.filter_by_dietary("vegan")
assert len(vegan) > 0
assert all("vegan" in s.dietary_labels for s in vegan)
def test_list_all_returns_all_three():
from app.services.recipe.staple_library import StapleLibrary
lib = StapleLibrary()
all_staples = lib.list_all()
slugs = {s.slug for s in all_staples}
assert {"seitan", "tempeh", "tofu_firm"} == slugs