refactor: migrate ingredient aliases/categories to dedicated tables, drop spoonacular_id

- migration 012: create ingredient_categories + ingredient_category_translations
  tables (7 slugs, Russian names); add ingredient_aliases (ingredient_id, lang,
  alias) with GIN trigram index; migrate aliases JSONB from ingredient_mappings
  and ingredient_translations; drop aliases columns and spoonacular_id; add
  UNIQUE (canonical_name) as conflict key
- ingredient/model: remove SpoonacularID, add CategoryName for localized display
- ingredient/repository: conflict on canonical_name; GetByID/Search join category
  translations and aliases lateral; new UpsertAliases (pgx batch),
  UpsertCategoryTranslation; remove GetBySpoonacularID; split scan helpers into
  scanMappingWrite / scanMappingRead
- gemini/recognition: add IngredientTranslation type; IngredientClassification
  now carries Translations []IngredientTranslation instead of CanonicalNameRu;
  update ClassifyIngredient prompt to English with structured translations array
- recognition/handler: update ingredientRepo interface; saveClassification uses
  UpsertAliases and iterates Translations
- recipe/model: remove SpoonacularID from RecipeIngredient
- integration tests: remove SpoonacularID fixtures, replace GetBySpoonacularID
  tests with GetByID, add UpsertAliases and UpsertCategoryTranslation tests
- Flutter: remove canonicalNameRu from IngredientMapping, add categoryName;
  displayName returns server-resolved canonicalName; regenerate .g.dart

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
dbastrikin
2026-03-10 14:40:07 +02:00
parent c9ddb708b1
commit a225f6c47a
9 changed files with 373 additions and 152 deletions

View File

@@ -4,7 +4,6 @@ package ingredient
import (
"context"
"encoding/json"
"testing"
"github.com/food-ai/backend/internal/locale"
@@ -16,17 +15,14 @@ func TestIngredientRepository_Upsert_Insert(t *testing.T) {
repo := NewRepository(pool)
ctx := context.Background()
id := 1001
cat := "produce"
unit := "g"
cal := 52.0
m := &IngredientMapping{
CanonicalName: "apple",
SpoonacularID: &id,
Aliases: json.RawMessage(`["apple", "apples"]`),
Category: &cat,
DefaultUnit: &unit,
CanonicalName: "apple",
Category: &cat,
DefaultUnit: &unit,
CaloriesPer100g: &cal,
}
@@ -50,14 +46,11 @@ func TestIngredientRepository_Upsert_ConflictUpdates(t *testing.T) {
repo := NewRepository(pool)
ctx := context.Background()
id := 2001
cat := "produce"
unit := "g"
first := &IngredientMapping{
CanonicalName: "banana",
SpoonacularID: &id,
Aliases: json.RawMessage(`["banana"]`),
Category: &cat,
DefaultUnit: &unit,
}
@@ -68,9 +61,7 @@ func TestIngredientRepository_Upsert_ConflictUpdates(t *testing.T) {
cal := 89.0
second := &IngredientMapping{
CanonicalName: "banana_updated",
SpoonacularID: &id,
Aliases: json.RawMessage(`["banana", "bananas"]`),
CanonicalName: "banana",
Category: &cat,
DefaultUnit: &unit,
CaloriesPer100g: &cal,
@@ -83,7 +74,7 @@ func TestIngredientRepository_Upsert_ConflictUpdates(t *testing.T) {
if got1.ID != got2.ID {
t.Errorf("ID changed on conflict update: %s != %s", got1.ID, got2.ID)
}
if got2.CanonicalName != "banana_updated" {
if got2.CanonicalName != "banana" {
t.Errorf("canonical_name not updated: got %s", got2.CanonicalName)
}
if got2.CaloriesPer100g == nil || *got2.CaloriesPer100g != 89.0 {
@@ -91,19 +82,16 @@ func TestIngredientRepository_Upsert_ConflictUpdates(t *testing.T) {
}
}
func TestIngredientRepository_GetBySpoonacularID_Found(t *testing.T) {
func TestIngredientRepository_GetByID_Found(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := NewRepository(pool)
ctx := context.Background()
id := 3001
cat := "dairy"
unit := "g"
_, err := repo.Upsert(ctx, &IngredientMapping{
saved, err := repo.Upsert(ctx, &IngredientMapping{
CanonicalName: "cheese",
SpoonacularID: &id,
Aliases: json.RawMessage(`["cheese"]`),
Category: &cat,
DefaultUnit: &unit,
})
@@ -111,7 +99,7 @@ func TestIngredientRepository_GetBySpoonacularID_Found(t *testing.T) {
t.Fatalf("upsert: %v", err)
}
got, err := repo.GetBySpoonacularID(ctx, id)
got, err := repo.GetByID(ctx, saved.ID)
if err != nil {
t.Fatalf("get: %v", err)
}
@@ -123,12 +111,12 @@ func TestIngredientRepository_GetBySpoonacularID_Found(t *testing.T) {
}
}
func TestIngredientRepository_GetBySpoonacularID_NotFound(t *testing.T) {
func TestIngredientRepository_GetByID_NotFound(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := NewRepository(pool)
ctx := context.Background()
got, err := repo.GetBySpoonacularID(ctx, 99999999)
got, err := repo.GetByID(ctx, "00000000-0000-0000-0000-000000000000")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
@@ -146,12 +134,9 @@ func TestIngredientRepository_ListMissingTranslation(t *testing.T) {
unit := "g"
// Insert 3 without any translation.
for i, name := range []string{"carrot", "onion", "garlic"} {
id := 4000 + i
for _, name := range []string{"carrot", "onion", "garlic"} {
_, err := repo.Upsert(ctx, &IngredientMapping{
CanonicalName: name,
SpoonacularID: &id,
Aliases: json.RawMessage(`[]`),
Category: &cat,
DefaultUnit: &unit,
})
@@ -160,19 +145,16 @@ func TestIngredientRepository_ListMissingTranslation(t *testing.T) {
}
}
// Insert 1 and add a Russian translation — should not appear in the result.
id := 4100
// Insert 1 and add a translation — should not appear in the result.
saved, err := repo.Upsert(ctx, &IngredientMapping{
CanonicalName: "tomato",
SpoonacularID: &id,
Aliases: json.RawMessage(`[]`),
Category: &cat,
DefaultUnit: &unit,
})
if err != nil {
t.Fatalf("upsert tomato: %v", err)
}
if err := repo.UpsertTranslation(ctx, saved.ID, "ru", "помидор", json.RawMessage(`["помидор","томат"]`)); err != nil {
if err := repo.UpsertTranslation(ctx, saved.ID, "ru", "помидор"); err != nil {
t.Fatalf("upsert translation: %v", err)
}
@@ -196,14 +178,11 @@ func TestIngredientRepository_UpsertTranslation(t *testing.T) {
repo := NewRepository(pool)
ctx := context.Background()
id := 5001
cat := "meat"
unit := "g"
saved, err := repo.Upsert(ctx, &IngredientMapping{
CanonicalName: "chicken_breast",
SpoonacularID: &id,
Aliases: json.RawMessage(`["chicken breast"]`),
Category: &cat,
DefaultUnit: &unit,
})
@@ -211,9 +190,7 @@ func TestIngredientRepository_UpsertTranslation(t *testing.T) {
t.Fatalf("upsert: %v", err)
}
err = repo.UpsertTranslation(ctx, saved.ID, "ru", "куриная грудка",
json.RawMessage(`["куриная грудка","куриное филе"]`))
if err != nil {
if err := repo.UpsertTranslation(ctx, saved.ID, "ru", "куриная грудка"); err != nil {
t.Fatalf("upsert translation: %v", err)
}
@@ -237,3 +214,83 @@ func TestIngredientRepository_UpsertTranslation(t *testing.T) {
t.Errorf("expected English CanonicalName='chicken_breast', got %q", gotEn.CanonicalName)
}
}
func TestIngredientRepository_UpsertAliases(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := NewRepository(pool)
ctx := context.Background()
cat := "produce"
unit := "g"
saved, err := repo.Upsert(ctx, &IngredientMapping{
CanonicalName: "apple_test_aliases",
Category: &cat,
DefaultUnit: &unit,
})
if err != nil {
t.Fatalf("upsert: %v", err)
}
aliases := []string{"apple", "apples", "red apple"}
if err := repo.UpsertAliases(ctx, saved.ID, "en", aliases); err != nil {
t.Fatalf("upsert aliases: %v", err)
}
// Idempotent — second call should not fail.
if err := repo.UpsertAliases(ctx, saved.ID, "en", aliases); err != nil {
t.Fatalf("second upsert aliases: %v", err)
}
// Retrieve with English context — aliases should appear.
enCtx := locale.WithLang(ctx, "en")
got, err := repo.GetByID(enCtx, saved.ID)
if err != nil {
t.Fatalf("get by id: %v", err)
}
if string(got.Aliases) == "[]" || string(got.Aliases) == "null" {
t.Errorf("expected non-empty aliases, got %s", got.Aliases)
}
}
func TestIngredientRepository_UpsertCategoryTranslation(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := NewRepository(pool)
ctx := context.Background()
// Upsert an ingredient with a known category.
cat := "dairy"
unit := "g"
saved, err := repo.Upsert(ctx, &IngredientMapping{
CanonicalName: "milk_test_category",
Category: &cat,
DefaultUnit: &unit,
})
if err != nil {
t.Fatalf("upsert: %v", err)
}
// The migration already inserted Russian translations for known categories.
// Retrieve with Russian context — CategoryName should be set.
ruCtx := locale.WithLang(ctx, "ru")
got, err := repo.GetByID(ruCtx, saved.ID)
if err != nil {
t.Fatalf("get by id: %v", err)
}
if got.CategoryName == nil || *got.CategoryName == "" {
t.Error("expected non-empty CategoryName for 'dairy' in Russian")
}
// Upsert a new translation and verify it is returned.
if err := repo.UpsertCategoryTranslation(ctx, "dairy", "de", "Milchprodukte"); err != nil {
t.Fatalf("upsert category translation: %v", err)
}
deCtx := locale.WithLang(ctx, "de")
gotDe, err := repo.GetByID(deCtx, saved.ID)
if err != nil {
t.Fatalf("get by id (de): %v", err)
}
if gotDe.CategoryName == nil || *gotDe.CategoryName != "Milchprodukte" {
t.Errorf("expected CategoryName='Milchprodukte', got %v", gotDe.CategoryName)
}
}