Add tests for putting a recipe in a recipe

This commit is contained in:
Alessio 2024-11-10 22:34:48 -08:00
parent 52ed02b34c
commit fd909079a3
2 changed files with 47 additions and 0 deletions

View File

@ -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)
}
}

View File

@ -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)
}
}