diff --git a/pkg/db/recipe_test.go b/pkg/db/recipe_test.go index 24c845d..d965568 100644 --- a/pkg/db/recipe_test.go +++ b/pkg/db/recipe_test.go @@ -94,6 +94,14 @@ func TestRecipeComputeFood(t *testing.T) { assert.Equal(computed_food2.Zinc, float32(22.75)) assert.Equal(computed_food2.Mass, float32(23.75)) assert.Equal(computed_food2.Price, float32(24.75)) + + // Combine recipes in a recipe + recipe3 := Recipe{Ingredients: []Ingredient{ + COUNT.Portion(recipe, 2), + COUNT.Portion(recipe2, 1), + }} + computed_food3 := recipe3.ComputeFood() + assert.Equal(computed_food3.Cals, float32(44.75)) } func TestRecipeSaveComputedFood(t *testing.T) { @@ -118,4 +126,17 @@ func TestRecipeSaveComputedFood(t *testing.T) { if diff := deep.Equal(recipe.ComputeFood(), computed_food); diff != nil { t.Error(diff) } + + // Test putting a recipe in a recipe + mozza := get_food(db, 83) + require.Equal(mozza.Name, "mozzarella") + recipe2 := Recipe{Name: "baked pasta w/ mozza", Ingredients: []Ingredient{ + COUNT.Portion(recipe, 0.5), + GRAMS.Of(mozza, 300), + }} + db.SaveRecipe(&recipe2) + computed_food2 := get_food(db, recipe2.ComputedFoodID) + if diff := deep.Equal(recipe2.ComputeFood(), computed_food2); diff != nil { + t.Error(diff) + } } diff --git a/pkg/db/units.go b/pkg/db/units.go index 1fecdd3..4d9bb09 100644 --- a/pkg/db/units.go +++ b/pkg/db/units.go @@ -48,3 +48,29 @@ func (u Units) Of(f Food, n float32) Ingredient { panic(u) } } + +func (u Units) Portion(r Recipe, n float32) Ingredient { + f := r.ComputeFood() + switch u { + case COUNT: + return Ingredient{RecipeID: r.ID, Quantity: n, Units: u, Food: f} + case GRAMS: + return Ingredient{RecipeID: r.ID, Quantity: n / f.Mass, Units: u, Food: f} + case LBS: + return Ingredient{RecipeID: r.ID, Quantity: n * 454 / f.Mass, Units: u, Food: f} + case OZ: + return Ingredient{RecipeID: r.ID, Quantity: n * 28 / f.Mass, Units: u, Food: f} + case ML: + return Ingredient{RecipeID: r.ID, Quantity: n * f.Density / f.Mass, Units: u, Food: f} + case CUPS: + return Ingredient{RecipeID: r.ID, Quantity: n * f.Density * 250 / f.Mass, Units: u, Food: f} + case TSP: + return Ingredient{RecipeID: r.ID, Quantity: n * f.Density * 5 / f.Mass, Units: u, Food: f} + case TBSP: + return Ingredient{RecipeID: r.ID, Quantity: n * f.Density * 15 / f.Mass, Units: u, Food: f} + case FLOZ: + return Ingredient{RecipeID: r.ID, Quantity: n * f.Density * 30 / f.Mass, Units: u, Food: f} + default: + panic(u) + } +}