78 lines
2.4 KiB
Go
78 lines
2.4 KiB
Go
package feeds
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Regression: Excel/UTF-8-SIG CSV headers include a BOM on the first column.
|
||
|
|
// This silently skipped every Grohe row (127/127) when the first column was gtin.
|
||
|
|
func TestParseCSVStripsUTF8BOMFromGTIN(t *testing.T) {
|
||
|
|
csvBody := "\ufeffgtin,sku,title\r\n4005176980640,32917KS0,Grohe Minta\r\n4005176980641,32917KS1,Grohe Test\r\n"
|
||
|
|
mappings := []FieldMapping{
|
||
|
|
{Source: "gtin", Target: "gtin"},
|
||
|
|
{Source: "title", Target: "title"},
|
||
|
|
}
|
||
|
|
var got []string
|
||
|
|
count, err := parseCSV(strings.NewReader(csvBody), func(row feedRow) error {
|
||
|
|
if _, hasBOM := row["\ufeffgtin"]; hasBOM {
|
||
|
|
t.Fatal("BOM was not removed from first CSV header")
|
||
|
|
}
|
||
|
|
mapped, gtin := applyMappings(row, mappings)
|
||
|
|
if mapped["title"] == "" || gtin == "" {
|
||
|
|
t.Fatalf("missing recognized mapped values: %v, gtin %q", mapped, gtin)
|
||
|
|
}
|
||
|
|
got = append(got, gtin)
|
||
|
|
return nil
|
||
|
|
})
|
||
|
|
if err != nil || count != 2 || len(got) != 2 || got[0] != "4005176980640" {
|
||
|
|
t.Fatalf("count=%d gtins=%v err=%v", count, got, err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestCSVLegacySavedBOMMappingIsRecognized(t *testing.T) {
|
||
|
|
mapping := []FieldMapping{{Source: "\ufeffgtin", Target: "gtin"}}
|
||
|
|
_, err := parseCSV(strings.NewReader("\ufeffgtin,title\n4005176980640,Grohe\n"), func(row feedRow) error {
|
||
|
|
_, gtin := applyMappings(row, mapping)
|
||
|
|
if gtin != "4005176980640" {
|
||
|
|
t.Fatalf("legacy BOM mapping could not read normalized header: %q", gtin)
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestExtractCSVSchemaStripsUTF8BOM(t *testing.T) {
|
||
|
|
fields, rows, _, err := extractCSVSchema([]byte("\ufeffgtin,title\n4005176980640,Grohe\n"))
|
||
|
|
if err != nil || rows != 1 {
|
||
|
|
t.Fatalf("rows=%d err=%v", rows, err)
|
||
|
|
}
|
||
|
|
found := false
|
||
|
|
for _, field := range fields {
|
||
|
|
if strings.Contains(field.Path, "\ufeff") {
|
||
|
|
t.Fatalf("schema still exposes BOM: %q", field.Path)
|
||
|
|
}
|
||
|
|
if field.Path == "gtin" {
|
||
|
|
found = true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if !found {
|
||
|
|
t.Fatalf("GTIN absent from schema fields: %v", fields)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestSyncSkipDiagnosticsExplainsEmptyImport(t *testing.T) {
|
||
|
|
stats := syncStats{Total: 127, Skipped: 127, MissingGTIN: 127,
|
||
|
|
MissingRows: []int{1, 2, 3}, SourceColumns: []string{"sku", "title"}}
|
||
|
|
msg := syncSkipDiagnostics(stats)
|
||
|
|
for _, term := range []string{"127 of 127", "GTIN", "sku", "title", "1, 2, 3"} {
|
||
|
|
if !strings.Contains(msg, term) {
|
||
|
|
t.Fatalf("diagnostic %q missing %q", msg, term)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if syncSkipDiagnostics(syncStats{Total: 2}) != "" {
|
||
|
|
t.Fatal("no skipped rows should have no warning")
|
||
|
|
}
|
||
|
|
}
|