feat: rename ingredients→products, products→user_products; add barcode/OFF import
- Rename catalog: ingredient/* → product/* (canonical_name, barcode, nutrition per 100g)
- Rename pantry: product/* → userproduct/* (user-owned items with expiry)
- Squash migrations into single 001_initial_schema.sql (clean-db baseline)
- product_categories: add English canonical name column; fix COALESCE in queries
- Remove product_translations: product names are stored in their original language
- Add default_unit_name to product API responses via unit_translations JOIN
- Add cmd/importoff: bulk import from OpenFoodFacts JSONL dump (COPY + ON CONFLICT)
- Diary: support product_id entries alongside dish_id (CHECK num_nonnulls = 1)
- Home: getLoggedCalories joins both recipes and catalog products
- Flutter: rename models/providers/services to match backend rename
- Flutter: add barcode scan flow for diary (mobile_scanner, product_portion_sheet)
- Flutter: localise 6 new keys across 12 languages (barcode scan, portion weight)
- Routes: GET /products/search, GET /products/barcode/{barcode}, /user-products
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
137
backend/tests/userproduct/repository_integration_test.go
Normal file
137
backend/tests/userproduct/repository_integration_test.go
Normal file
@@ -0,0 +1,137 @@
|
||||
//go:build integration
|
||||
|
||||
package userproduct_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/food-ai/backend/internal/domain/userproduct"
|
||||
"github.com/food-ai/backend/internal/testutil"
|
||||
)
|
||||
|
||||
func TestUserProductRepository_Create_Defaults(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := userproduct.NewRepository(pool)
|
||||
requestContext := context.Background()
|
||||
|
||||
// storage_days=0 → 7; unit="" → "pcs"; quantity=0 → 1
|
||||
created, createError := repo.Create(requestContext, "test-user", userproduct.CreateRequest{
|
||||
Name: "Milk",
|
||||
StorageDays: 0,
|
||||
Unit: "",
|
||||
Quantity: 0,
|
||||
})
|
||||
if createError != nil {
|
||||
t.Fatalf("create user 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 TestUserProductRepository_List_OrderByExpiry(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := userproduct.NewRepository(pool)
|
||||
requestContext := context.Background()
|
||||
|
||||
userID := "list-order-user"
|
||||
_, createError := repo.Create(requestContext, userID, userproduct.CreateRequest{Name: "Milk", StorageDays: 14})
|
||||
if createError != nil {
|
||||
t.Fatalf("create Milk: %v", createError)
|
||||
}
|
||||
_, createError = repo.Create(requestContext, userID, userproduct.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 user 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 TestUserProductRepository_BatchCreate(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := userproduct.NewRepository(pool)
|
||||
requestContext := context.Background()
|
||||
|
||||
products, batchError := repo.BatchCreate(requestContext, "batch-user", []userproduct.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 TestUserProductRepository_Update_NotFound(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := userproduct.NewRepository(pool)
|
||||
requestContext := context.Background()
|
||||
|
||||
newName := "Ghost"
|
||||
_, updateError := repo.Update(requestContext, "00000000-0000-0000-0000-000000000000", "any-user",
|
||||
userproduct.UpdateRequest{Name: &newName})
|
||||
if updateError != userproduct.ErrNotFound {
|
||||
t.Errorf("expected ErrNotFound, got %v", updateError)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserProductRepository_Delete_WrongUser(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := userproduct.NewRepository(pool)
|
||||
requestContext := context.Background()
|
||||
|
||||
created, createError := repo.Create(requestContext, "owner-user", userproduct.CreateRequest{Name: "Cheese"})
|
||||
if createError != nil {
|
||||
t.Fatalf("create user product: %v", createError)
|
||||
}
|
||||
|
||||
deleteError := repo.Delete(requestContext, created.ID, "other-user")
|
||||
if deleteError != userproduct.ErrNotFound {
|
||||
t.Errorf("expected ErrNotFound when deleting another user's product, got %v", deleteError)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserProductRepository_ListForPrompt(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := userproduct.NewRepository(pool)
|
||||
requestContext := context.Background()
|
||||
|
||||
userID := "prompt-user"
|
||||
_, createError := repo.Create(requestContext, userID, userproduct.CreateRequest{
|
||||
Name: "Tomatoes", Quantity: 4, Unit: "pcs", StorageDays: 5,
|
||||
})
|
||||
if createError != nil {
|
||||
t.Fatalf("create user 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])
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user