Files
food-ai/backend/tests/recipe/repository_integration_test.go
dbastrikin bfaca1a2c1 test: expand test coverage across diary, product, savedrecipe, ingredient, menu, recognition
- Fix locale_test: add TestMain to pre-populate Supported map so zh/es tests pass
- Export pure functions for testability: ResolveWeekStart, MapCuisineSlug (menu + savedrecipe), MergeAndDeduplicate
- Introduce repository interfaces (DiaryRepository, ProductRepository, SavedRecipeRepository, IngredientSearcher) in each handler; NewHandler now accepts interfaces — concrete *Repository still satisfies them
- Add mock files: diary/mocks, product/mocks, savedrecipe/mocks
- Add handler unit tests (no DB) for diary (8), product (8), savedrecipe (8), ingredient (5)
- Add pure-function unit tests: menu/ResolveWeekStart (6), savedrecipe/MapCuisineSlug (5), recognition/MergeAndDeduplicate (6)
- Add repository integration tests (//go:build integration): diary (4), product (6)
- Extend recipe integration tests: GetByID_Found, GetByID_WithTranslation

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-15 22:54:09 +02:00

121 lines
3.4 KiB
Go

//go:build integration
package recipe_test
import (
"context"
"testing"
"github.com/food-ai/backend/internal/domain/dish"
"github.com/food-ai/backend/internal/domain/recipe"
"github.com/food-ai/backend/internal/infra/locale"
"github.com/food-ai/backend/internal/testutil"
)
func TestRecipeRepository_GetByID_NotFound(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := recipe.NewRepository(pool)
requestContext := context.Background()
got, getError := repo.GetByID(requestContext, "00000000-0000-0000-0000-000000000000")
if getError != nil {
t.Fatalf("unexpected error: %v", getError)
}
if got != nil {
t.Error("expected nil for non-existent ID")
}
}
func TestRecipeRepository_Count(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := recipe.NewRepository(pool)
requestContext := context.Background()
_, countError := repo.Count(requestContext)
if countError != nil {
t.Fatalf("count: %v", countError)
}
}
func TestRecipeRepository_GetByID_Found(t *testing.T) {
pool := testutil.SetupTestDB(t)
dishRepo := dish.NewRepository(pool)
recipeRepo := recipe.NewRepository(pool)
requestContext := context.Background()
recipeID, createError := dishRepo.Create(requestContext, dish.CreateRequest{
Name: "Test Pasta",
Description: "A simple pasta dish",
Source: "test",
Ingredients: []dish.IngredientInput{
{Name: "pasta", Amount: 200, Unit: "g"},
},
Steps: []dish.StepInput{
{Number: 1, Description: "Boil water"},
},
})
if createError != nil {
t.Fatalf("create dish+recipe: %v", createError)
}
found, getError := recipeRepo.GetByID(requestContext, recipeID)
if getError != nil {
t.Fatalf("get recipe: %v", getError)
}
if found == nil {
t.Fatal("expected recipe to be found, got nil")
}
if found.ID != recipeID {
t.Errorf("expected ID %q, got %q", recipeID, found.ID)
}
if len(found.Ingredients) != 1 {
t.Errorf("expected 1 ingredient, got %d", len(found.Ingredients))
}
if found.Ingredients[0].Name != "pasta" {
t.Errorf("expected ingredient name 'pasta', got %q", found.Ingredients[0].Name)
}
if len(found.Steps) != 1 {
t.Errorf("expected 1 step, got %d", len(found.Steps))
}
}
func TestRecipeRepository_GetByID_WithTranslation(t *testing.T) {
pool := testutil.SetupTestDB(t)
dishRepo := dish.NewRepository(pool)
recipeRepo := recipe.NewRepository(pool)
baseContext := context.Background()
recipeID, createError := dishRepo.Create(baseContext, dish.CreateRequest{
Name: "Pasta Bolognese",
Description: "Classic Italian pasta",
Source: "test",
})
if createError != nil {
t.Fatalf("create dish+recipe: %v", createError)
}
// Without translation context, the recipe should return English content.
enContext := locale.WithLang(baseContext, "en")
recipeEN, getError := recipeRepo.GetByID(enContext, recipeID)
if getError != nil {
t.Fatalf("get recipe (en): %v", getError)
}
if recipeEN == nil {
t.Fatal("expected recipe, got nil")
}
// With a language not having a translation, fallback to English should occur.
ruContext := locale.WithLang(baseContext, "ru")
recipeRU, getError := recipeRepo.GetByID(ruContext, recipeID)
if getError != nil {
t.Fatalf("get recipe (ru): %v", getError)
}
if recipeRU == nil {
t.Fatal("expected recipe with ru context, got nil")
}
// Both should refer to the same recipe ID.
if recipeEN.ID != recipeRU.ID {
t.Errorf("expected same recipe ID, got %q vs %q", recipeEN.ID, recipeRU.ID)
}
}