Select Git revision
schema_json_test.go 1.55 KiB
package schema
import (
"testing"
"git.perx.ru/perxis/perxis-go/pkg/schema/field"
"github.com/stretchr/testify/assert"
)
func TestSchema_UnmarshalJSON(t *testing.T) {
s := `{
"ui": {
"options": {
"fields": [
"text"
]
}
},
"type": "object",
"params": {
"inline": false,
"fields": {
"text": {
"title": "text",
"ui": {
"widget": "StringInput"
},
"type": "string",
"params": {}
}
}
},
"loaded": false,
"metadata": null
}`
tests := []struct {
name string
b []byte
want *Schema
wantErr bool
}{
{
name: "No panic with 'null' value in b",
b: []byte("null"),
want: New(),
wantErr: false,
},
{
name: "UnmarshalJSON error #1",
b: []byte(""),
want: New(),
wantErr: true,
},
{
name: "UnmarshalJSON error #2",
b: []byte("abc"),
want: New(),
wantErr: true,
},
{
name: "OK",
b: []byte(s),
want: New("text", field.String().SetTitle("text").WithUI(&field.UI{Widget: "StringInput"})),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
schema := New()
if err := schema.UnmarshalJSON(tt.b); (err != nil) != tt.wantErr {
t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
}
schema.ClearState()
tt.want.ClearState()
assert.Equal(t, tt.want, schema)
})
}
}