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