1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
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)
}
|