71 lines
1.6 KiB
Go
71 lines
1.6 KiB
Go
package feeds
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestExtractCSVSchema(t *testing.T) {
|
||
|
|
data := []byte("ean,title,price\n123,Widget,9.99\n456,Gadget,12.50\n")
|
||
|
|
fields, rows, preview, err := extractCSVSchema(data)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if rows != 2 {
|
||
|
|
t.Fatalf("rows=%d", rows)
|
||
|
|
}
|
||
|
|
if len(fields) != 3 {
|
||
|
|
t.Fatalf("fields=%d", len(fields))
|
||
|
|
}
|
||
|
|
if !strings.Contains(preview, "ean,title,price") {
|
||
|
|
t.Fatalf("preview missing header: %q", preview)
|
||
|
|
}
|
||
|
|
if fields[0].Path != "ean" {
|
||
|
|
t.Fatalf("first field %q", fields[0].Path)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestExtractXMLSchema(t *testing.T) {
|
||
|
|
data := []byte(`<?xml version="1.0"?>
|
||
|
|
<rss><channel>
|
||
|
|
<item><title>A</title><g:gtin>111</g:gtin></item>
|
||
|
|
<item><title>B</title><g:gtin>222</g:gtin></item>
|
||
|
|
</channel></rss>`)
|
||
|
|
fields, rows, err := extractXMLSchema(data, "item")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if rows != 2 {
|
||
|
|
t.Fatalf("rows=%d", rows)
|
||
|
|
}
|
||
|
|
if len(fields) == 0 {
|
||
|
|
t.Fatal("expected fields")
|
||
|
|
}
|
||
|
|
foundTitle := false
|
||
|
|
for _, f := range fields {
|
||
|
|
if f.FieldName == "title" || f.Path == "title" {
|
||
|
|
foundTitle = true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if !foundTitle {
|
||
|
|
t.Fatalf("title not found in %#v", fields)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestParseMappingsWrapped(t *testing.T) {
|
||
|
|
raw := map[string]any{
|
||
|
|
"item_path": "rss/channel/item",
|
||
|
|
"fields": []any{
|
||
|
|
map[string]any{"source": "gtin", "target": "gtin"},
|
||
|
|
map[string]any{"source": "title", "target": "title"},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
got := parseMappings(raw)
|
||
|
|
if len(got) != 2 {
|
||
|
|
t.Fatalf("got %d mappings: %#v", len(got), got)
|
||
|
|
}
|
||
|
|
if itemPathFromMappings(raw) != "rss/channel/item" {
|
||
|
|
t.Fatalf("item path: %q", itemPathFromMappings(raw))
|
||
|
|
}
|
||
|
|
}
|