Add db functions to get all recipes / base foods

This commit is contained in:
Alessio 2024-11-17 13:04:23 -08:00
parent fd909079a3
commit 750be015bb
4 changed files with 44 additions and 0 deletions

View File

@ -103,3 +103,17 @@ func (db *DB) GetFoodByID(id FoodID) (ret Food, err error) {
`, id)
return
}
func (db *DB) GetAllBaseFoods() []Food {
var ret []Food
err := db.DB.Select(&ret, `
select rowid, name, cals, carbs, protein, fat, sugar, alcohol, water, potassium, calcium, sodium,
magnesium, phosphorus, iron, zinc, mass, price, density, cook_ratio
from foods
where rowid not in (select computed_food_id from recipes)
`)
if err != nil {
panic(err)
}
return ret
}

View File

@ -70,3 +70,15 @@ func TestFoodSaveAndLoad(t *testing.T) {
t.Error(diff)
}
}
// Should list all the base foods (i.e., ones that aren't recipes)
func TestListAllBaseFoods(t *testing.T) {
assert := assert.New(t)
db := get_test_db()
base_foods := db.GetAllBaseFoods()
assert.True(len(base_foods) >= 100)
for _, f := range base_foods {
assert.NotContains([]FoodID{10000, 10001}, f.ID, f) // Computed foods have ID >= 10000 in seed data
}
}

View File

@ -132,6 +132,15 @@ func (db *DB) GetRecipeByID(id RecipeID) (ret Recipe, err error) {
return
}
func (db *DB) GetAllRecipes() []Recipe {
var ret []Recipe
err := db.DB.Select(&ret, `select rowid, name, blurb, instructions, computed_food_id from recipes`)
if err != nil {
panic(err)
}
return ret
}
func (r Recipe) ComputeFood() Food {
// If r.ComputedFoodID is 0, so should be the ID of returned Food
ret := Food{ID: r.ComputedFoodID, Name: r.Name}

View File

@ -140,3 +140,12 @@ func TestRecipeSaveComputedFood(t *testing.T) {
t.Error(diff)
}
}
// Should list all the recipes
func TestListAllRecipes(t *testing.T) {
assert := assert.New(t)
db := get_test_db()
recipes := db.GetAllRecipes()
assert.True(len(recipes) >= 2)
}