Files
dataxl/cmd/dataxl/merge_test.go
T
agent d3a08c25e2
CI / test (pull_request) Failing after 39s
feat: add keyed record merge (#9)
2026-10-26 15:55:49 +09:00

135 lines
3.9 KiB
Go

package main
import (
"bytes"
"encoding/json"
"strings"
"testing"
)
func TestMergeCLIEnrichesPrimaryRecords(t *testing.T) {
t.Parallel()
primary := "id,name\n1,Alice\n2,Bob\n3,Carol\n"
details := "[{\"id\":1,\"contact\":{\"email\":\"[email protected]\"},\"active\":true},{\"id\":2,\"contact\":{\"email\":\"[email protected]\"},\"active\":false},{\"id\":99,\"ignored\":\"no primary match\"}]"
detailsPath := t.TempDir() + "/details.json"
if err := writeOutput(detailsPath, ioDiscard{}, []byte(details)); err != nil {
t.Fatal(err)
}
var out bytes.Buffer
err := run(
[]string{"-from", "csv", "-to", "json", "-merge", detailsPath, "-on", "id", "-pretty=false"},
strings.NewReader(primary), &out, ioDiscard{},
)
if err != nil {
t.Fatal(err)
}
var got []map[string]any
if err := json.Unmarshal(out.Bytes(), &got); err != nil {
t.Fatal(err)
}
if len(got) != 3 {
t.Fatalf("record count = %d, want 3", len(got))
}
if got[0]["active"] != true || got[1]["active"] != false {
t.Fatalf("merged active fields = %#v, %#v", got[0]["active"], got[1]["active"])
}
if _, exists := got[2]["active"]; exists {
t.Fatalf("unmatched primary record was modified: %#v", got[2])
}
contact := got[0]["contact"].(map[string]any)
if contact["email"] != "[email protected]" {
t.Fatalf("nested merged field = %#v", contact)
}
}
func TestMergeRecordsSupportsNestedKeyAndFields(t *testing.T) {
t.Parallel()
primary := []map[string]any{{"user": map[string]any{"id": "001", "name": "Alice"}}}
secondary := []map[string]any{{"user": map[string]any{"id": "001", "role": "admin"}}}
got, err := mergeRecords(primary, secondary, "user.id")
if err != nil {
t.Fatal(err)
}
user := got[0]["user"].(map[string]any)
if user["name"] != "Alice" || user["role"] != "admin" {
t.Fatalf("merged user = %#v", user)
}
if _, exists := primary[0]["user"].(map[string]any)["role"]; exists {
t.Fatal("merge mutated primary input")
}
}
func TestMergeRecordsRejectsDuplicateLookupKey(t *testing.T) {
t.Parallel()
_, err := mergeRecords(
[]map[string]any{{"id": int64(1)}},
[]map[string]any{{"id": int64(1)}, {"id": int64(1)}},
"id",
)
if err == nil || !strings.Contains(err.Error(), "duplicate key") {
t.Fatalf("error = %v, want duplicate-key error", err)
}
}
func TestMergeRecordsRejectsConflictingField(t *testing.T) {
t.Parallel()
_, err := mergeRecords(
[]map[string]any{{"id": int64(1), "name": "Alice"}},
[]map[string]any{{"id": int64(1), "name": "Alicia"}},
"id",
)
if err == nil || !strings.Contains(err.Error(), "merge field \"name\" conflicts") {
t.Fatalf("error = %v, want field-conflict error", err)
}
}
func TestMergeRecordsMatchesNumericKeysAcrossInputTypes(t *testing.T) {
t.Parallel()
got, err := mergeRecords(
[]map[string]any{{"id": int64(1)}},
[]map[string]any{{"id": json.Number("1.0"), "label": "one"}},
"id",
)
if err != nil {
t.Fatal(err)
}
if got[0]["label"] != "one" {
t.Fatalf("merged record = %#v", got[0])
}
}
func TestMergeRecordsPreservesLargeNumericKeyPrecision(t *testing.T) {
t.Parallel()
got, err := mergeRecords(
[]map[string]any{{"id": uint64(18_446_744_073_709_551_615)}},
[]map[string]any{{"id": json.Number("18446744073709551615"), "label": "max"}},
"id",
)
if err != nil {
t.Fatal(err)
}
if got[0]["label"] != "max" {
t.Fatalf("merged record = %#v", got[0])
}
}
func TestMergeOptionsRequireJoinPathAndFormat(t *testing.T) {
t.Parallel()
opt, err := parseOptions([]string{"-from", "csv", "-to", "json", "-merge", "-"}, ioDiscard{})
if err != nil {
t.Fatal(err)
}
if err := opt.resolveFormats(); err == nil || !strings.Contains(err.Error(), "-merge-from is required") {
t.Fatalf("error = %v, want merge-format error", err)
}
opt, err = parseOptions([]string{"-from", "csv", "-to", "json", "-merge", "lookup.json"}, ioDiscard{})
if err != nil {
t.Fatal(err)
}
if err := opt.resolveFormats(); err == nil || !strings.Contains(err.Error(), "-on is required") {
t.Fatalf("error = %v, want join-path error", err)
}
}