- 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>
139 lines
4.2 KiB
Go
139 lines
4.2 KiB
Go
//go:build integration
|
|
|
|
package product_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/food-ai/backend/internal/domain/product"
|
|
"github.com/food-ai/backend/internal/testutil"
|
|
)
|
|
|
|
func TestProductRepository_Create_Defaults(t *testing.T) {
|
|
pool := testutil.SetupTestDB(t)
|
|
repo := product.NewRepository(pool)
|
|
requestContext := context.Background()
|
|
|
|
// storage_days=0 → 7; unit="" → "pcs"; quantity=0 → 1
|
|
created, createError := repo.Create(requestContext, "test-user", product.CreateRequest{
|
|
Name: "Milk",
|
|
StorageDays: 0,
|
|
Unit: "",
|
|
Quantity: 0,
|
|
})
|
|
if createError != nil {
|
|
t.Fatalf("create product: %v", createError)
|
|
}
|
|
if created.StorageDays != 7 {
|
|
t.Errorf("expected storage_days=7, got %d", created.StorageDays)
|
|
}
|
|
if created.Unit != "pcs" {
|
|
t.Errorf("expected unit=pcs, got %q", created.Unit)
|
|
}
|
|
if created.Quantity != 1 {
|
|
t.Errorf("expected quantity=1, got %v", created.Quantity)
|
|
}
|
|
}
|
|
|
|
func TestProductRepository_List_OrderByExpiry(t *testing.T) {
|
|
pool := testutil.SetupTestDB(t)
|
|
repo := product.NewRepository(pool)
|
|
requestContext := context.Background()
|
|
|
|
userID := "list-order-user"
|
|
// Create product with longer expiry first, shorter expiry second.
|
|
_, createError := repo.Create(requestContext, userID, product.CreateRequest{Name: "Milk", StorageDays: 14})
|
|
if createError != nil {
|
|
t.Fatalf("create Milk: %v", createError)
|
|
}
|
|
_, createError = repo.Create(requestContext, userID, product.CreateRequest{Name: "Butter", StorageDays: 3})
|
|
if createError != nil {
|
|
t.Fatalf("create Butter: %v", createError)
|
|
}
|
|
|
|
products, listError := repo.List(requestContext, userID)
|
|
if listError != nil {
|
|
t.Fatalf("list products: %v", listError)
|
|
}
|
|
if len(products) != 2 {
|
|
t.Fatalf("expected 2 products, got %d", len(products))
|
|
}
|
|
// Butter (3 days) should come before Milk (14 days).
|
|
if products[0].Name != "Butter" {
|
|
t.Errorf("expected first product Butter (expires sooner), got %q", products[0].Name)
|
|
}
|
|
}
|
|
|
|
func TestProductRepository_BatchCreate(t *testing.T) {
|
|
pool := testutil.SetupTestDB(t)
|
|
repo := product.NewRepository(pool)
|
|
requestContext := context.Background()
|
|
|
|
products, batchError := repo.BatchCreate(requestContext, "batch-user", []product.CreateRequest{
|
|
{Name: "Eggs", Quantity: 12, Unit: "pcs"},
|
|
{Name: "Flour", Quantity: 500, Unit: "g"},
|
|
})
|
|
if batchError != nil {
|
|
t.Fatalf("batch create: %v", batchError)
|
|
}
|
|
if len(products) != 2 {
|
|
t.Errorf("expected 2 products, got %d", len(products))
|
|
}
|
|
}
|
|
|
|
func TestProductRepository_Update_NotFound(t *testing.T) {
|
|
pool := testutil.SetupTestDB(t)
|
|
repo := product.NewRepository(pool)
|
|
requestContext := context.Background()
|
|
|
|
newName := "Ghost"
|
|
_, updateError := repo.Update(requestContext, "00000000-0000-0000-0000-000000000000", "any-user",
|
|
product.UpdateRequest{Name: &newName})
|
|
if updateError != product.ErrNotFound {
|
|
t.Errorf("expected ErrNotFound, got %v", updateError)
|
|
}
|
|
}
|
|
|
|
func TestProductRepository_Delete_WrongUser(t *testing.T) {
|
|
pool := testutil.SetupTestDB(t)
|
|
repo := product.NewRepository(pool)
|
|
requestContext := context.Background()
|
|
|
|
created, createError := repo.Create(requestContext, "owner-user", product.CreateRequest{Name: "Cheese"})
|
|
if createError != nil {
|
|
t.Fatalf("create product: %v", createError)
|
|
}
|
|
|
|
deleteError := repo.Delete(requestContext, created.ID, "other-user")
|
|
if deleteError != product.ErrNotFound {
|
|
t.Errorf("expected ErrNotFound when deleting another user's product, got %v", deleteError)
|
|
}
|
|
}
|
|
|
|
func TestProductRepository_ListForPrompt(t *testing.T) {
|
|
pool := testutil.SetupTestDB(t)
|
|
repo := product.NewRepository(pool)
|
|
requestContext := context.Background()
|
|
|
|
userID := "prompt-user"
|
|
_, createError := repo.Create(requestContext, userID, product.CreateRequest{
|
|
Name: "Tomatoes", Quantity: 4, Unit: "pcs", StorageDays: 5,
|
|
})
|
|
if createError != nil {
|
|
t.Fatalf("create product: %v", createError)
|
|
}
|
|
|
|
lines, listError := repo.ListForPrompt(requestContext, userID)
|
|
if listError != nil {
|
|
t.Fatalf("list for prompt: %v", listError)
|
|
}
|
|
if len(lines) != 1 {
|
|
t.Fatalf("expected 1 line, got %d", len(lines))
|
|
}
|
|
// Line should start with "- Tomatoes".
|
|
if len(lines[0]) < 11 || lines[0][:11] != "- Tomatoes " {
|
|
t.Errorf("unexpected prompt line format: %q", lines[0])
|
|
}
|
|
}
|