173 lines
5.7 KiB
Go
173 lines
5.7 KiB
Go
package processing
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func TestParseV1ProcessingTypeFullAndSteps(t *testing.T) {
|
|
storage, resp, err := ParseV1ProcessingType(nil)
|
|
if err != nil || storage != "full" || resp != "full" {
|
|
t.Fatalf("nil: storage=%q resp=%v err=%v", storage, resp, err)
|
|
}
|
|
storage, resp, err = ParseV1ProcessingType("title")
|
|
if err != nil || storage != "title" || resp != "title" {
|
|
t.Fatalf("title: storage=%q resp=%v err=%v", storage, resp, err)
|
|
}
|
|
storage, _, err = ParseV1ProcessingType("normalize_only")
|
|
if err != nil || storage != "normalize_only" {
|
|
t.Fatalf("normalize_only: storage=%q err=%v", storage, err)
|
|
}
|
|
storage, resp, err = ParseV1ProcessingType([]any{"title", "attributes"})
|
|
if err != nil || storage != `["title","attributes"]` {
|
|
t.Fatalf("array: storage=%q resp=%v err=%v", storage, resp, err)
|
|
}
|
|
arr, ok := resp.([]string)
|
|
if !ok || len(arr) != 2 {
|
|
t.Fatalf("resp=%v", resp)
|
|
}
|
|
_, _, err = ParseV1ProcessingType("nope")
|
|
if err == nil {
|
|
t.Fatal("expected invalid type error")
|
|
}
|
|
}
|
|
|
|
func TestMapJobStatusForV1(t *testing.T) {
|
|
if got := MapJobStatusForV1("pending"); got != "PENDING" {
|
|
t.Fatalf("got %q", got)
|
|
}
|
|
if got := MapJobStatusForV1("running"); got != "PROCESSING" {
|
|
t.Fatalf("got %q", got)
|
|
}
|
|
if got := MapJobStatusForV1("completed"); got != "COMPLETED" {
|
|
t.Fatalf("got %q", got)
|
|
}
|
|
for _, syn := range []string{"success", "done", "finished", "processed"} {
|
|
if got := MapJobStatusForV1(syn); got != "COMPLETED" {
|
|
t.Fatalf("%s -> %q", syn, got)
|
|
}
|
|
}
|
|
if !JobStatusIncludesProducts("completed") || JobStatusIncludesProducts("running") {
|
|
t.Fatal("JobStatusIncludesProducts mismatch")
|
|
}
|
|
}
|
|
|
|
func TestMapV1JobItemStatus(t *testing.T) {
|
|
if got := MapV1JobItemStatus("processed", true); got != "processed" {
|
|
t.Fatalf("got %q", got)
|
|
}
|
|
if got := MapV1JobItemStatus("failed", false); got != "failed" {
|
|
t.Fatalf("got %q", got)
|
|
}
|
|
if got := MapV1JobItemStatus("", false); got != "not_found" {
|
|
t.Fatalf("got %q", got)
|
|
}
|
|
if got := MapV1JobItemStatus("", true); got != "processed" {
|
|
t.Fatalf("got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestProjectV1ProcessJobItemsPartial(t *testing.T) {
|
|
items := []V1ProcessJobItem{{
|
|
"ean": "123", "id": "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb",
|
|
"processed_product_id": "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb",
|
|
"raw_product_id": "cccccccc-cccc-cccc-cccc-cccccccccccc",
|
|
"status": "processed",
|
|
"title": "T", "meta_title": "MT",
|
|
"description": "D", "attributes": map[string]any{"brand": "X"},
|
|
"main_image": "https://example.com/a.jpg", "more_images": []string{"https://example.com/b.jpg"},
|
|
"eprel": nil, "category": "cat", "category_name": "Cat",
|
|
}}
|
|
out := ProjectV1ProcessJobItems("title", items)
|
|
if len(out) != 1 {
|
|
t.Fatalf("len=%d", len(out))
|
|
}
|
|
raw, _ := json.Marshal(out[0])
|
|
var got map[string]any
|
|
_ = json.Unmarshal(raw, &got)
|
|
if got["title"] != "T" || got["ean"] != "123" {
|
|
t.Fatalf("got=%v", got)
|
|
}
|
|
if _, ok := got["attributes"]; ok {
|
|
t.Fatalf("attributes should be projected out: %v", got)
|
|
}
|
|
if got["main_image"] != "https://example.com/a.jpg" {
|
|
t.Fatalf("images always included: %v", got)
|
|
}
|
|
if got["status"] != "processed" || got["id"] != "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb" {
|
|
t.Fatalf("meta should be preserved: %v", got)
|
|
}
|
|
if got["processed_product_id"] != "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb" {
|
|
t.Fatalf("processed_product_id should be preserved: %v", got)
|
|
}
|
|
if got["raw_product_id"] != "cccccccc-cccc-cccc-cccc-cccccccccccc" {
|
|
t.Fatalf("raw_product_id should be preserved: %v", got)
|
|
}
|
|
}
|
|
|
|
func TestV1PlainDescriptionStandalone(t *testing.T) {
|
|
got := v1PlainDescription("Line1<br>Line2 & ok")
|
|
if strings.Contains(got, "<") {
|
|
t.Fatalf("html left: %q", got)
|
|
}
|
|
if !strings.Contains(got, "Line1") || !strings.Contains(got, "Line2") || !strings.Contains(got, "& ok") {
|
|
t.Fatalf("got=%q", got)
|
|
}
|
|
}
|
|
|
|
func TestApplyV1ProcessItemIDsDualMode(t *testing.T) {
|
|
processed := mustParseTestUUID(t, "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb")
|
|
raw := mustParseTestUUID(t, "cccccccc-cccc-cccc-cccc-cccccccccccc")
|
|
item := V1ProcessJobItem{"ean": "1", "status": "processed"}
|
|
applyV1ProcessItemIDs(item, &processed, &raw)
|
|
if item["id"] != processed.String() {
|
|
t.Fatalf("id=%v", item["id"])
|
|
}
|
|
if item["processed_product_id"] != processed.String() {
|
|
t.Fatalf("processed_product_id=%v", item["processed_product_id"])
|
|
}
|
|
if item["raw_product_id"] != raw.String() {
|
|
t.Fatalf("raw_product_id=%v", item["raw_product_id"])
|
|
}
|
|
missing := V1ProcessJobItem{"ean": "2", "status": "not_found"}
|
|
applyV1ProcessItemIDs(missing, nil, &raw)
|
|
if _, ok := missing["id"]; ok {
|
|
t.Fatalf("id must stay absent without processed row: %v", missing)
|
|
}
|
|
if missing["raw_product_id"] != raw.String() {
|
|
t.Fatalf("raw_product_id on not_found: %v", missing)
|
|
}
|
|
}
|
|
|
|
func TestFormatJobStatusResponseAddsItems(t *testing.T) {
|
|
jobID := mustParseTestUUID(t, "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")
|
|
job := Job{ID: jobID, Status: "completed", TotalProducts: 1}
|
|
items := []V1ProcessJobItem{{"ean": "1", "status": "processed", "id": "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"}}
|
|
out, ok := FormatJobStatusResponse(job, items, true).(map[string]any)
|
|
if !ok {
|
|
t.Fatalf("type=%T", FormatJobStatusResponse(job, items, true))
|
|
}
|
|
if out["status"] != "completed" {
|
|
t.Fatalf("status=%v", out["status"])
|
|
}
|
|
if n, ok := out["total_items"].(int); !ok || n != 1 {
|
|
t.Fatalf("total_items=%v (%T)", out["total_items"], out["total_items"])
|
|
}
|
|
arr, ok := out["items"].([]V1ProcessJobItem)
|
|
if !ok || len(arr) != 1 {
|
|
t.Fatalf("items=%v (%T)", out["items"], out["items"])
|
|
}
|
|
}
|
|
|
|
func mustParseTestUUID(t *testing.T, s string) uuid.UUID {
|
|
t.Helper()
|
|
id, err := uuid.Parse(s)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return id
|
|
}
|