fix
This commit is contained in:
@@ -470,3 +470,82 @@ func TestBackfillProcessedCategoriesFromMapped(t *testing.T) {
|
||||
t.Fatalf("coverage with=%d without=%d", withCat, withoutCat)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackfillMappedCategoriesFromProcessed(t *testing.T) {
|
||||
dsn := os.Getenv("DATABASE_URL")
|
||||
if dsn == "" {
|
||||
t.Skip("DATABASE_URL not set")
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
pg, err := pgxpool.New(ctx, dsn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer pg.Close()
|
||||
|
||||
companyID := uuid.New()
|
||||
rawID := uuid.New()
|
||||
ppID := uuid.New()
|
||||
gtin := "map-bf-" + companyID.String()[:8]
|
||||
|
||||
if _, err := pg.Exec(ctx, `INSERT INTO companies (id, name) VALUES ($1, $2)`, companyID, "mapped-backfill-test"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
_, _ = pg.Exec(context.Background(), `DELETE FROM processed_products WHERE company_id = $1`, companyID)
|
||||
_, _ = pg.Exec(context.Background(), `DELETE FROM raw_products WHERE company_id = $1`, companyID)
|
||||
_, _ = pg.Exec(context.Background(), `DELETE FROM companies WHERE id = $1`, companyID)
|
||||
}()
|
||||
|
||||
if _, err := pg.Exec(ctx, `
|
||||
INSERT INTO raw_products (id, company_id, gtin, raw_data, mapped_data, is_processed, processing_status)
|
||||
VALUES ($1, $2, $3, '{}'::jsonb, '{"name":"Widget"}'::jsonb, true, 'processed')`,
|
||||
rawID, companyID, gtin); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := pg.Exec(ctx, `
|
||||
INSERT INTO processed_products (
|
||||
id, company_id, product_id, name, category, description, processed_name, processed_description,
|
||||
raw_product_id, status, attributes, processed_attributes, field_sources
|
||||
) VALUES (
|
||||
$1, $2, $3, 'Widget', '42', 'x', 'Widget', 'x',
|
||||
$4, 'needs_review', '{}'::jsonb, '{}'::jsonb, '{}'::jsonb
|
||||
)`, ppID, companyID, gtin, rawID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
n, err := BackfillMappedCategoriesFromProcessed(ctx, pg, companyID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n != 1 {
|
||||
t.Fatalf("updated=%d want 1", n)
|
||||
}
|
||||
var mappedCat string
|
||||
if err := pg.QueryRow(ctx, `
|
||||
SELECT COALESCE(mapped_data->>'category', '') FROM raw_products WHERE id = $1`, rawID).Scan(&mappedCat); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if mappedCat != "42" {
|
||||
t.Fatalf("mapped category=%q want 42", mappedCat)
|
||||
}
|
||||
|
||||
n2, err := BackfillMappedCategoriesFromProcessed(ctx, pg, companyID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n2 != 0 {
|
||||
t.Fatalf("second backfill updated=%d want 0", n2)
|
||||
}
|
||||
|
||||
withCat, withoutCat, taxonomy, err := CountMappedCategoryCoverage(ctx, pg, companyID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if withCat != 1 || withoutCat != 0 || taxonomy != 0 {
|
||||
t.Fatalf("mapped coverage with=%d without=%d taxonomy=%d", withCat, withoutCat, taxonomy)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user