mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
Summary: Pull Request resolved: https://github.com/facebookexternal/fbc/pull/1192 Pull Request resolved: https://github.com/facebookincubator/ent/pull/11 Reviewed By: alexsn Differential Revision: D16377224 fbshipit-source-id: 07ca7436eb9b64fbe2299568560b91466b2417ba
71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
package graph
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestValueMapDecodeOne(t *testing.T) {
|
|
vm := ValueMap{map[string]interface{}{
|
|
"id": int64(1),
|
|
"label": "person",
|
|
"name": []interface{}{"marko"},
|
|
"age": []interface{}{int32(29)},
|
|
}}
|
|
|
|
var ent struct {
|
|
ID uint64 `json:"id"`
|
|
Label string `json:"label"`
|
|
Name string `json:"name"`
|
|
Age uint8 `json:"age"`
|
|
}
|
|
err := vm.Decode(&ent)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, uint64(1), ent.ID)
|
|
assert.Equal(t, "person", ent.Label)
|
|
assert.Equal(t, "marko", ent.Name)
|
|
assert.Equal(t, uint8(29), ent.Age)
|
|
}
|
|
|
|
func TestValueMapDecodeMany(t *testing.T) {
|
|
vm := ValueMap{
|
|
map[string]interface{}{
|
|
"id": int64(1),
|
|
"label": "person",
|
|
"name": []interface{}{"chico"},
|
|
},
|
|
map[string]interface{}{
|
|
"id": int64(2),
|
|
"label": "person",
|
|
"name": []interface{}{"dico"},
|
|
},
|
|
}
|
|
|
|
ents := []struct {
|
|
ID int `json:"id"`
|
|
Label string `json:"label"`
|
|
Name string `json:"name"`
|
|
}{}
|
|
err := vm.Decode(&ents)
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, ents, 2)
|
|
assert.Equal(t, 1, ents[0].ID)
|
|
assert.Equal(t, "person", ents[0].Label)
|
|
assert.Equal(t, "chico", ents[0].Name)
|
|
assert.Equal(t, 2, ents[1].ID)
|
|
assert.Equal(t, "person", ents[1].Label)
|
|
assert.Equal(t, "dico", ents[1].Name)
|
|
}
|
|
|
|
func TestValueMapDecodeBadInput(t *testing.T) {
|
|
type s struct{ Name string }
|
|
err := ValueMap{}.Decode(s{})
|
|
assert.Error(t, err)
|
|
err = ValueMap{}.Decode((*s)(nil))
|
|
assert.Error(t, err)
|
|
}
|