This commit is contained in:
greeneclipse committed 2026-11-15 10:04:44 +01:00
1 parent 41f1a7dbe4
commit e7964c58c8
13 files changed
+1215 -24

No files matched your search

@@ -0,0 +1,96 @@
package catalog
import (
"context"
"os"
"strings"
"testing"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgxpool"
)
// The product detail payload must carry feed_field_sources so the product page can
// show which feed supplied each field.
func TestProductDetailsExposeFeedFieldSources(t *testing.T) {
dsn := strings.TrimSpace(os.Getenv("DATABASE_URL"))
if dsn == "" {
t.Skip("DATABASE_URL not set")
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
pg, err := pgxpool.New(ctx, dsn)
if err != nil {
t.Fatal(err)
}
defer pg.Close()
companyID := uuid.New()
if _, err := pg.Exec(ctx, `INSERT INTO companies (id, name) VALUES ($1, $2)`, companyID, "ffs-"+companyID.String()[:8]); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _, _ = pg.Exec(context.Background(), `DELETE FROM companies WHERE id = $1`, companyID) })
var feedA, feedB uuid.UUID
for name, dst := range map[string]*uuid.UUID{"Vendor A": &feedA, "Vendor B": &feedB} {
if err := pg.QueryRow(ctx, `
INSERT INTO input_feeds (company_id, name, url, feed_type, status, sync_interval_minutes, options)
VALUES ($1, $2, 'https://example.com/f.csv', 'csv', 'active', 60, '{}'::jsonb) RETURNING id`,
companyID, name).Scan(dst); err != nil {
t.Fatal(err)
}
}
sources := `{"primary":{"feed_id":"` + feedA.String() + `","feed_name":"Vendor A"},
"feeds":[{"feed_id":"` + feedA.String() + `","feed_name":"Vendor A"},{"feed_id":"` + feedB.String() + `","feed_name":"Vendor B"}],
"fields":{"description":{"feed_id":"` + feedB.String() + `","feed_name":"Vendor B","conflict":true,"also":[{"feed_id":"` + feedA.String() + `","feed_name":"Vendor A"}]},
"brand":{"feed_id":"` + feedA.String() + `","feed_name":"Vendor A"}}}`
var rawID, procID uuid.UUID
if err := pg.QueryRow(ctx, `
INSERT INTO raw_products (company_id, gtin, feed_id, mapped_data, feed_field_sources)
VALUES ($1, '4548736180307', $2, '{"name":"TV","brand":"Sony"}'::jsonb, $3::jsonb) RETURNING id`,
companyID, feedA, sources).Scan(&rawID); err != nil {
t.Fatal(err)
}
if err := pg.QueryRow(ctx, `
INSERT INTO processed_products (company_id, raw_product_id, product_id, name)
VALUES ($1, $2, '4548736180307', 'TV') RETURNING id`, companyID, rawID).Scan(&procID); err != nil {
t.Fatal(err)
}
svc := &Service{Pool: pg}
for name, load := range map[string]func() (map[string]any, error){
"processed": func() (map[string]any, error) { return svc.GetProcessedProduct(ctx, companyID, procID) },
"raw": func() (map[string]any, error) { return svc.GetRawProduct(ctx, companyID, rawID) },
} {
item, err := load()
if err != nil {
t.Fatalf("%s: %v", name, err)
}
ffs, ok := item["feed_field_sources"].(map[string]any)
if !ok {
t.Fatalf("%s: feed_field_sources missing or wrong type: %#v", name, item["feed_field_sources"])
}
fields, _ := ffs["fields"].(map[string]any)
desc, _ := fields["description"].(map[string]any)
if desc["feed_name"] != "Vendor B" || desc["conflict"] != true {
t.Errorf("%s: description source = %#v", name, desc)
}
if feeds, _ := ffs["feeds"].([]any); len(feeds) != 2 {
t.Errorf("%s: feeds = %#v", name, ffs["feeds"])
}
}
// A product synced before merging existed has no sources: the key is present but empty.
if _, err := pg.Exec(ctx, `UPDATE raw_products SET feed_field_sources = NULL WHERE id = $1`, rawID); err != nil {
t.Fatal(err)
}
item, err := svc.GetRawProduct(ctx, companyID, rawID)
if err != nil {
t.Fatal(err)
}
if v := item["feed_field_sources"]; v != nil {
t.Errorf("NULL sources should surface as nil, got %#v", v)
}
}
+4
View File
@@ -1317,6 +1317,7 @@ func (s *Service) GetProcessedProduct(ctx context.Context, companyID, id uuid.UU
COALESCE(p.meta_title, '') AS meta_title,
COALESCE(p.meta_description, '') AS meta_description,
p.status, p.processing_origin, p.attributes, p.processed_attributes, r.gtin, r.mapped_data,
r.feed_field_sources,
COALESCE(p.feed_id, r.feed_id) AS feed_id,
f.name AS feed_name,
f.last_synced_at AS feed_last_synced_at,
@@ -1339,6 +1340,7 @@ func (s *Service) GetProcessedProduct(ctx context.Context, companyID, id uuid.UU
"description", "original_description", "processed_name", "processed_description",
"meta_title", "meta_description",
"status", "processing_origin", "attributes", "processed_attributes", "gtin", "mapped_data",
"feed_field_sources",
"feed_id", "feed_name", "feed_last_synced_at", "raw_updated_at",
"has_processed_name", "has_processed_description", "has_attributes", "has_processed_attributes",
"has_eprel",
@@ -1378,6 +1380,7 @@ func (s *Service) GetRawProduct(ctx context.Context, companyID, id uuid.UUID) (m
'{}'::jsonb AS processed_attributes,
rp.gtin,
rp.mapped_data,
rp.feed_field_sources,
rp.feed_id,
f.name AS feed_name,
f.last_synced_at AS feed_last_synced_at,
@@ -1401,6 +1404,7 @@ func (s *Service) GetRawProduct(ctx context.Context, companyID, id uuid.UUID) (m
"id", "product_id", "name", "category", "category_name", "category_unique_id",
"description", "processed_name", "processed_description",
"status", "attributes", "processed_attributes", "gtin", "mapped_data",
"feed_field_sources",
"feed_id", "feed_name", "feed_last_synced_at", "raw_updated_at",
"has_processed_name", "has_processed_description", "has_attributes", "has_processed_attributes",
"has_eprel",