Files
descrybe/apps/api/internal/eprel/attrs_test.go
T
2026-08-16 16:57:36 +02:00

85 lines
2.3 KiB
Go

package eprel
import "testing"
func TestExtractFromAttrs_flatKeys(t *testing.T) {
got := ExtractFromAttrs(map[string]any{
"eprel_id": "1632113",
"eprel_label": "https://eprel.ec.europa.eu/api/product/1632113/labels?format=png",
"eprel_pdf_url": "https://eprel.ec.europa.eu/fiches/x.pdf",
"eprel_energy_class": "A",
"brand": "Samsung",
})
m, ok := got.(map[string]any)
if !ok {
t.Fatalf("type=%T", got)
}
if m["id"] != "1632113" || m["label"] == nil || m["pdf"] == nil || m["energy_class"] != "A" {
t.Fatalf("got=%v", m)
}
if _, leak := m["eprel_id"]; leak {
t.Fatalf("alias leaked: %v", m)
}
}
func TestExtractFromAttrs_nestedAliasesNormalized(t *testing.T) {
got := ExtractFromAttrs(map[string]any{
"eprel": map[string]any{
"eprel_id": "2208111",
"eprel_label": "https://eprel.ec.europa.eu/api/product/2208111/labels?format=png",
"eprel_pdf": "https://eprel.ec.europa.eu/fiches/y.pdf",
"eprel_energy_class": "E",
"source": "eprel_api",
},
})
m, ok := got.(map[string]any)
if !ok {
t.Fatalf("type=%T", got)
}
if m["id"] != "2208111" {
t.Fatalf("id=%v", m["id"])
}
if m["label"] == nil || m["pdf"] == nil {
t.Fatalf("missing label/pdf: %v", m)
}
if m["energy_class"] != "E" {
t.Fatalf("energy_class=%v", m["energy_class"])
}
if _, ok := m["eprel_id"]; ok {
t.Fatalf("eprel_id alias must be folded: %v", m)
}
if _, ok := m["source"]; ok {
t.Fatalf("non-canonical keys must be dropped: %v", m)
}
}
func TestExtractFromAttrs_nestedPartialFilledFromFlat(t *testing.T) {
got := ExtractFromAttrs(map[string]any{
"eprel": map[string]any{
"id": "1632113",
"label": "https://eprel.ec.europa.eu/api/product/1632113/labels?format=png",
},
"eprel_pdf": "https://eprel.ec.europa.eu/fiches/z.pdf",
"eprel_energy_class": "A",
})
m, ok := got.(map[string]any)
if !ok {
t.Fatalf("type=%T", got)
}
if m["id"] != "1632113" || m["pdf"] == nil || m["energy_class"] != "A" {
t.Fatalf("got=%v", m)
}
}
func TestNormalizeShape_empty(t *testing.T) {
if NormalizeShape(nil) != nil {
t.Fatal("nil")
}
if NormalizeShape(map[string]any{}) != nil {
t.Fatal("empty")
}
if NormalizeShape(map[string]any{"eprel_id": ""}) != nil {
t.Fatal("blank")
}
}