54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package schema
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestFlatten_NestedObject(t *testing.T) {
|
|
in := map[string]any{
|
|
"user": map[string]any{
|
|
"id": "u_1",
|
|
"profile": map[string]any{"age": 30, "name": "Phuoc"},
|
|
},
|
|
"plan": "pro",
|
|
}
|
|
got := Flatten(in)
|
|
assert.Equal(t, "u_1", got["user_id"])
|
|
assert.Equal(t, 30, got["user_profile_age"])
|
|
assert.Equal(t, "Phuoc", got["user_profile_name"])
|
|
assert.Equal(t, "pro", got["plan"])
|
|
}
|
|
|
|
func TestFlatten_SanitizesKeys(t *testing.T) {
|
|
in := map[string]any{
|
|
"User Email": "x@y",
|
|
"price.usd": 9.99,
|
|
"meta!": map[string]any{"X-Y": 1},
|
|
}
|
|
got := Flatten(in)
|
|
assert.Equal(t, "x@y", got["user_email"])
|
|
assert.Equal(t, 9.99, got["price_usd"])
|
|
assert.Equal(t, 1, got["meta_x_y"])
|
|
}
|
|
|
|
func TestFlatten_PreservesArrays(t *testing.T) {
|
|
in := map[string]any{
|
|
"tags": []any{"a", "b"},
|
|
}
|
|
got := Flatten(in)
|
|
arr, ok := got["tags"].([]any)
|
|
assert.True(t, ok)
|
|
assert.Equal(t, 2, len(arr))
|
|
}
|
|
|
|
func TestClassify(t *testing.T) {
|
|
assert.Equal(t, TypeString, Classify("hi"))
|
|
assert.Equal(t, TypeNumber, Classify(float64(1.5)))
|
|
assert.Equal(t, TypeBoolean, Classify(true))
|
|
assert.Equal(t, TypeNull, Classify(nil))
|
|
assert.Equal(t, TypeArray, Classify([]any{1, 2}))
|
|
assert.Equal(t, TypeObject, Classify(map[string]any{}))
|
|
}
|