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:
@@ -6,15 +6,15 @@ import (
|
||||
)
|
||||
|
||||
// IngredientMapping is the canonical ingredient record used to link
|
||||
// user products, recipe ingredients, and Spoonacular data.
|
||||
// user products and recipe ingredients.
|
||||
// CanonicalName holds the content for the language resolved at query time
|
||||
// (English by default, or from ingredient_translations when available).
|
||||
type IngredientMapping struct {
|
||||
ID string `json:"id"`
|
||||
CanonicalName string `json:"canonical_name"`
|
||||
SpoonacularID *int `json:"spoonacular_id"`
|
||||
Aliases json.RawMessage `json:"aliases"` // []string
|
||||
Aliases json.RawMessage `json:"aliases"` // []string, populated by read queries
|
||||
Category *string `json:"category"`
|
||||
CategoryName *string `json:"category_name"` // localized category display name
|
||||
DefaultUnit *string `json:"default_unit"`
|
||||
|
||||
CaloriesPer100g *float64 `json:"calories_per_100g"`
|
||||
|
||||
@@ -22,18 +22,16 @@ func NewRepository(pool *pgxpool.Pool) *Repository {
|
||||
}
|
||||
|
||||
// Upsert inserts or updates an ingredient mapping (English canonical content).
|
||||
// Conflict is resolved on spoonacular_id when set.
|
||||
// Conflict is resolved on canonical_name.
|
||||
func (r *Repository) Upsert(ctx context.Context, m *IngredientMapping) (*IngredientMapping, error) {
|
||||
query := `
|
||||
INSERT INTO ingredient_mappings (
|
||||
canonical_name, spoonacular_id, aliases,
|
||||
canonical_name,
|
||||
category, default_unit,
|
||||
calories_per_100g, protein_per_100g, fat_per_100g, carbs_per_100g, fiber_per_100g,
|
||||
storage_days
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
||||
ON CONFLICT (spoonacular_id) DO UPDATE SET
|
||||
canonical_name = EXCLUDED.canonical_name,
|
||||
aliases = EXCLUDED.aliases,
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
ON CONFLICT (canonical_name) DO UPDATE SET
|
||||
category = EXCLUDED.category,
|
||||
default_unit = EXCLUDED.default_unit,
|
||||
calories_per_100g = EXCLUDED.calories_per_100g,
|
||||
@@ -43,62 +41,47 @@ func (r *Repository) Upsert(ctx context.Context, m *IngredientMapping) (*Ingredi
|
||||
fiber_per_100g = EXCLUDED.fiber_per_100g,
|
||||
storage_days = EXCLUDED.storage_days,
|
||||
updated_at = now()
|
||||
RETURNING id, canonical_name, spoonacular_id, aliases,
|
||||
category, default_unit,
|
||||
RETURNING id, canonical_name, category, default_unit,
|
||||
calories_per_100g, protein_per_100g, fat_per_100g, carbs_per_100g, fiber_per_100g,
|
||||
storage_days, created_at, updated_at`
|
||||
|
||||
row := r.pool.QueryRow(ctx, query,
|
||||
m.CanonicalName, m.SpoonacularID, m.Aliases,
|
||||
m.CanonicalName,
|
||||
m.Category, m.DefaultUnit,
|
||||
m.CaloriesPer100g, m.ProteinPer100g, m.FatPer100g, m.CarbsPer100g, m.FiberPer100g,
|
||||
m.StorageDays,
|
||||
)
|
||||
return scanMapping(row)
|
||||
}
|
||||
|
||||
// GetBySpoonacularID returns an ingredient mapping by Spoonacular ID.
|
||||
// CanonicalName is resolved for the language stored in ctx.
|
||||
// Returns nil, nil if not found.
|
||||
func (r *Repository) GetBySpoonacularID(ctx context.Context, id int) (*IngredientMapping, error) {
|
||||
lang := locale.FromContext(ctx)
|
||||
query := `
|
||||
SELECT im.id,
|
||||
COALESCE(it.name, im.canonical_name) AS canonical_name,
|
||||
im.spoonacular_id, im.aliases,
|
||||
im.category, im.default_unit,
|
||||
im.calories_per_100g, im.protein_per_100g, im.fat_per_100g, im.carbs_per_100g, im.fiber_per_100g,
|
||||
im.storage_days, im.created_at, im.updated_at
|
||||
FROM ingredient_mappings im
|
||||
LEFT JOIN ingredient_translations it ON it.ingredient_id = im.id AND it.lang = $2
|
||||
WHERE im.spoonacular_id = $1`
|
||||
|
||||
row := r.pool.QueryRow(ctx, query, id, lang)
|
||||
m, err := scanMapping(row)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
return m, err
|
||||
return scanMappingWrite(row)
|
||||
}
|
||||
|
||||
// GetByID returns an ingredient mapping by UUID.
|
||||
// CanonicalName is resolved for the language stored in ctx.
|
||||
// CanonicalName and aliases are resolved for the language stored in ctx.
|
||||
// Returns nil, nil if not found.
|
||||
func (r *Repository) GetByID(ctx context.Context, id string) (*IngredientMapping, error) {
|
||||
lang := locale.FromContext(ctx)
|
||||
query := `
|
||||
SELECT im.id,
|
||||
COALESCE(it.name, im.canonical_name) AS canonical_name,
|
||||
im.spoonacular_id, im.aliases,
|
||||
im.category, im.default_unit,
|
||||
im.category,
|
||||
COALESCE(ict.name, im.category) AS category_name,
|
||||
im.default_unit,
|
||||
im.calories_per_100g, im.protein_per_100g, im.fat_per_100g, im.carbs_per_100g, im.fiber_per_100g,
|
||||
im.storage_days, im.created_at, im.updated_at
|
||||
im.storage_days, im.created_at, im.updated_at,
|
||||
COALESCE(al.aliases, '[]'::json) AS aliases
|
||||
FROM ingredient_mappings im
|
||||
LEFT JOIN ingredient_translations it ON it.ingredient_id = im.id AND it.lang = $2
|
||||
LEFT JOIN ingredient_translations it
|
||||
ON it.ingredient_id = im.id AND it.lang = $2
|
||||
LEFT JOIN ingredient_category_translations ict
|
||||
ON ict.category_slug = im.category AND ict.lang = $2
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT json_agg(ia.alias ORDER BY ia.alias) AS aliases
|
||||
FROM ingredient_aliases ia
|
||||
WHERE ia.ingredient_id = im.id AND ia.lang = $2
|
||||
) al ON true
|
||||
WHERE im.id = $1`
|
||||
|
||||
row := r.pool.QueryRow(ctx, query, id, lang)
|
||||
m, err := scanMapping(row)
|
||||
m, err := scanMappingRead(row)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -120,8 +103,7 @@ func (r *Repository) FuzzyMatch(ctx context.Context, name string) (*IngredientMa
|
||||
}
|
||||
|
||||
// Search finds ingredient mappings matching the query string.
|
||||
// Searches English aliases/name and the translated name for the language in ctx.
|
||||
// Uses a three-level strategy: exact aliases match, ILIKE, and pg_trgm similarity.
|
||||
// Searches aliases table and translated names for the language in ctx.
|
||||
func (r *Repository) Search(ctx context.Context, query string, limit int) ([]*IngredientMapping, error) {
|
||||
if limit <= 0 {
|
||||
limit = 10
|
||||
@@ -130,17 +112,31 @@ func (r *Repository) Search(ctx context.Context, query string, limit int) ([]*In
|
||||
q := `
|
||||
SELECT im.id,
|
||||
COALESCE(it.name, im.canonical_name) AS canonical_name,
|
||||
im.spoonacular_id, im.aliases,
|
||||
im.category, im.default_unit,
|
||||
im.category,
|
||||
COALESCE(ict.name, im.category) AS category_name,
|
||||
im.default_unit,
|
||||
im.calories_per_100g, im.protein_per_100g, im.fat_per_100g, im.carbs_per_100g, im.fiber_per_100g,
|
||||
im.storage_days, im.created_at, im.updated_at
|
||||
im.storage_days, im.created_at, im.updated_at,
|
||||
COALESCE(al.aliases, '[]'::json) AS aliases
|
||||
FROM ingredient_mappings im
|
||||
LEFT JOIN ingredient_translations it ON it.ingredient_id = im.id AND it.lang = $3
|
||||
WHERE im.aliases @> to_jsonb(lower($1)::text)
|
||||
OR it.aliases @> to_jsonb(lower($1)::text)
|
||||
OR im.canonical_name ILIKE '%' || $1 || '%'
|
||||
OR it.name ILIKE '%' || $1 || '%'
|
||||
OR similarity(COALESCE(it.name, im.canonical_name), $1) > 0.3
|
||||
LEFT JOIN ingredient_translations it
|
||||
ON it.ingredient_id = im.id AND it.lang = $3
|
||||
LEFT JOIN ingredient_category_translations ict
|
||||
ON ict.category_slug = im.category AND ict.lang = $3
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT json_agg(ia.alias ORDER BY ia.alias) AS aliases
|
||||
FROM ingredient_aliases ia
|
||||
WHERE ia.ingredient_id = im.id AND ia.lang = $3
|
||||
) al ON true
|
||||
WHERE EXISTS (
|
||||
SELECT 1 FROM ingredient_aliases ia
|
||||
WHERE ia.ingredient_id = im.id
|
||||
AND (ia.lang = $3 OR ia.lang = 'en')
|
||||
AND ia.alias ILIKE '%' || $1 || '%'
|
||||
)
|
||||
OR im.canonical_name ILIKE '%' || $1 || '%'
|
||||
OR it.name ILIKE '%' || $1 || '%'
|
||||
OR similarity(COALESCE(it.name, im.canonical_name), $1) > 0.3
|
||||
ORDER BY similarity(COALESCE(it.name, im.canonical_name), $1) DESC
|
||||
LIMIT $2`
|
||||
|
||||
@@ -149,7 +145,7 @@ func (r *Repository) Search(ctx context.Context, query string, limit int) ([]*In
|
||||
return nil, fmt.Errorf("search ingredient_mappings: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
return collectMappings(rows)
|
||||
return collectMappingsRead(rows)
|
||||
}
|
||||
|
||||
// Count returns the total number of ingredient mappings.
|
||||
@@ -165,7 +161,7 @@ func (r *Repository) Count(ctx context.Context) (int, error) {
|
||||
// given language, ordered by id.
|
||||
func (r *Repository) ListMissingTranslation(ctx context.Context, lang string, limit, offset int) ([]*IngredientMapping, error) {
|
||||
query := `
|
||||
SELECT im.id, im.canonical_name, im.spoonacular_id, im.aliases,
|
||||
SELECT im.id, im.canonical_name,
|
||||
im.category, im.default_unit,
|
||||
im.calories_per_100g, im.protein_per_100g, im.fat_per_100g, im.carbs_per_100g, im.fiber_per_100g,
|
||||
im.storage_days, im.created_at, im.updated_at
|
||||
@@ -182,53 +178,119 @@ func (r *Repository) ListMissingTranslation(ctx context.Context, lang string, li
|
||||
return nil, fmt.Errorf("list missing translation (%s): %w", lang, err)
|
||||
}
|
||||
defer rows.Close()
|
||||
return collectMappings(rows)
|
||||
return collectMappingsWrite(rows)
|
||||
}
|
||||
|
||||
// UpsertTranslation inserts or replaces a translation for an ingredient mapping.
|
||||
func (r *Repository) UpsertTranslation(ctx context.Context, id, lang, name string, aliases json.RawMessage) error {
|
||||
// UpsertTranslation inserts or replaces a name translation for an ingredient.
|
||||
func (r *Repository) UpsertTranslation(ctx context.Context, id, lang, name string) error {
|
||||
query := `
|
||||
INSERT INTO ingredient_translations (ingredient_id, lang, name, aliases)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (ingredient_id, lang) DO UPDATE SET
|
||||
name = EXCLUDED.name,
|
||||
aliases = EXCLUDED.aliases`
|
||||
INSERT INTO ingredient_translations (ingredient_id, lang, name)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (ingredient_id, lang) DO UPDATE SET name = EXCLUDED.name`
|
||||
|
||||
if _, err := r.pool.Exec(ctx, query, id, lang, name, aliases); err != nil {
|
||||
if _, err := r.pool.Exec(ctx, query, id, lang, name); err != nil {
|
||||
return fmt.Errorf("upsert ingredient translation %s/%s: %w", id, lang, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// --- helpers ---
|
||||
// UpsertAliases inserts aliases for a given ingredient and language.
|
||||
// Each alias is inserted with ON CONFLICT DO NOTHING, so duplicates are skipped.
|
||||
func (r *Repository) UpsertAliases(ctx context.Context, id, lang string, aliases []string) error {
|
||||
if len(aliases) == 0 {
|
||||
return nil
|
||||
}
|
||||
batch := &pgx.Batch{}
|
||||
for _, alias := range aliases {
|
||||
batch.Queue(
|
||||
`INSERT INTO ingredient_aliases (ingredient_id, lang, alias) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING`,
|
||||
id, lang, alias,
|
||||
)
|
||||
}
|
||||
results := r.pool.SendBatch(ctx, batch)
|
||||
defer results.Close()
|
||||
for range aliases {
|
||||
if _, err := results.Exec(); err != nil {
|
||||
return fmt.Errorf("upsert ingredient alias %s/%s: %w", id, lang, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func scanMapping(row pgx.Row) (*IngredientMapping, error) {
|
||||
// UpsertCategoryTranslation inserts or replaces a localized category name.
|
||||
func (r *Repository) UpsertCategoryTranslation(ctx context.Context, slug, lang, name string) error {
|
||||
query := `
|
||||
INSERT INTO ingredient_category_translations (category_slug, lang, name)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (category_slug, lang) DO UPDATE SET name = EXCLUDED.name`
|
||||
|
||||
if _, err := r.pool.Exec(ctx, query, slug, lang, name); err != nil {
|
||||
return fmt.Errorf("upsert category translation %s/%s: %w", slug, lang, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// --- scan helpers ---
|
||||
|
||||
// scanMappingWrite scans rows from Upsert / ListMissingTranslation queries
|
||||
// (no aliases lateral join, no category_name).
|
||||
func scanMappingWrite(row pgx.Row) (*IngredientMapping, error) {
|
||||
var m IngredientMapping
|
||||
var aliases []byte
|
||||
|
||||
err := row.Scan(
|
||||
&m.ID, &m.CanonicalName, &m.SpoonacularID, &aliases,
|
||||
&m.Category, &m.DefaultUnit,
|
||||
&m.ID, &m.CanonicalName, &m.Category, &m.DefaultUnit,
|
||||
&m.CaloriesPer100g, &m.ProteinPer100g, &m.FatPer100g, &m.CarbsPer100g, &m.FiberPer100g,
|
||||
&m.StorageDays, &m.CreatedAt, &m.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m.Aliases = json.RawMessage("[]")
|
||||
return &m, nil
|
||||
}
|
||||
|
||||
// scanMappingRead scans rows from GetByID / Search queries
|
||||
// (includes category_name and aliases lateral join).
|
||||
func scanMappingRead(row pgx.Row) (*IngredientMapping, error) {
|
||||
var m IngredientMapping
|
||||
var aliases []byte
|
||||
err := row.Scan(
|
||||
&m.ID, &m.CanonicalName, &m.Category, &m.CategoryName, &m.DefaultUnit,
|
||||
&m.CaloriesPer100g, &m.ProteinPer100g, &m.FatPer100g, &m.CarbsPer100g, &m.FiberPer100g,
|
||||
&m.StorageDays, &m.CreatedAt, &m.UpdatedAt, &aliases,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m.Aliases = json.RawMessage(aliases)
|
||||
return &m, nil
|
||||
}
|
||||
|
||||
func collectMappings(rows pgx.Rows) ([]*IngredientMapping, error) {
|
||||
func collectMappingsWrite(rows pgx.Rows) ([]*IngredientMapping, error) {
|
||||
var result []*IngredientMapping
|
||||
for rows.Next() {
|
||||
var m IngredientMapping
|
||||
if err := rows.Scan(
|
||||
&m.ID, &m.CanonicalName, &m.Category, &m.DefaultUnit,
|
||||
&m.CaloriesPer100g, &m.ProteinPer100g, &m.FatPer100g, &m.CarbsPer100g, &m.FiberPer100g,
|
||||
&m.StorageDays, &m.CreatedAt, &m.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf("scan mapping: %w", err)
|
||||
}
|
||||
m.Aliases = json.RawMessage("[]")
|
||||
result = append(result, &m)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
func collectMappingsRead(rows pgx.Rows) ([]*IngredientMapping, error) {
|
||||
var result []*IngredientMapping
|
||||
for rows.Next() {
|
||||
var m IngredientMapping
|
||||
var aliases []byte
|
||||
if err := rows.Scan(
|
||||
&m.ID, &m.CanonicalName, &m.SpoonacularID, &aliases,
|
||||
&m.Category, &m.DefaultUnit,
|
||||
&m.ID, &m.CanonicalName, &m.Category, &m.CategoryName, &m.DefaultUnit,
|
||||
&m.CaloriesPer100g, &m.ProteinPer100g, &m.FatPer100g, &m.CarbsPer100g, &m.FiberPer100g,
|
||||
&m.StorageDays, &m.CreatedAt, &m.UpdatedAt,
|
||||
&m.StorageDays, &m.CreatedAt, &m.UpdatedAt, &aliases,
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf("scan mapping: %w", err)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user