mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
move lib/go/gremlin to ent/dialect (#1192)
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
This commit is contained in:
committed by
Facebook Github Bot
parent
8b2447b8eb
commit
1e47de5300
222
dialect/gremlin/encoding/graphson/slice_test.go
Normal file
222
dialect/gremlin/encoding/graphson/slice_test.go
Normal file
@@ -0,0 +1,222 @@
|
||||
package graphson
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/modern-go/reflect2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestEncodeArray(t *testing.T) {
|
||||
t.Parallel()
|
||||
got, err := MarshalToString([...]string{"a", "b", "c"})
|
||||
require.NoError(t, err)
|
||||
want := `{ "@type": "g:List", "@value": ["a", "b", "c"]}`
|
||||
assert.JSONEq(t, want, got)
|
||||
}
|
||||
|
||||
func TestEncodeSlice(t *testing.T) {
|
||||
tests := []struct {
|
||||
in interface{}
|
||||
want string
|
||||
}{
|
||||
{
|
||||
in: []int32{5, 6, 7, 8},
|
||||
want: `{
|
||||
"@type": "g:List",
|
||||
"@value": [
|
||||
{
|
||||
"@type": "g:Int32",
|
||||
"@value": 5
|
||||
},
|
||||
{
|
||||
"@type": "g:Int32",
|
||||
"@value": 6
|
||||
},
|
||||
{
|
||||
"@type": "g:Int32",
|
||||
"@value": 7
|
||||
},
|
||||
{
|
||||
"@type": "g:Int32",
|
||||
"@value": 8
|
||||
}
|
||||
]
|
||||
}`,
|
||||
},
|
||||
{
|
||||
in: []byte{1, 2, 3, 4, 5},
|
||||
want: `{
|
||||
"@type": "gx:ByteBuffer",
|
||||
"@value": "AQIDBAU="
|
||||
}`,
|
||||
},
|
||||
{
|
||||
in: [...]byte{4, 5},
|
||||
want: `{
|
||||
"@type": "g:List",
|
||||
"@value": [
|
||||
{
|
||||
"@type": "gx:Byte",
|
||||
"@value": 4
|
||||
},
|
||||
{
|
||||
"@type": "gx:Byte",
|
||||
"@value": 5
|
||||
}
|
||||
]
|
||||
}`,
|
||||
},
|
||||
{
|
||||
in: []uint64(nil),
|
||||
want: "null",
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
tc := tc
|
||||
t.Run(fmt.Sprintf("%T", tc.in), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
var got bytes.Buffer
|
||||
err := NewEncoder(&got).Encode(tc.in)
|
||||
assert.NoError(t, err)
|
||||
assert.JSONEq(t, tc.want, got.String())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeSlice(t *testing.T) {
|
||||
tests := []struct {
|
||||
in string
|
||||
want interface{}
|
||||
}{
|
||||
{
|
||||
in: `{
|
||||
"@type": "g:List",
|
||||
"@value": [
|
||||
{
|
||||
"@type": "g:Int32",
|
||||
"@value": 3
|
||||
},
|
||||
{
|
||||
"@type": "g:Int32",
|
||||
"@value": -2
|
||||
},
|
||||
{
|
||||
"@type": "g:Int32",
|
||||
"@value": 1
|
||||
}
|
||||
]
|
||||
}`,
|
||||
want: []int32{3, -2, 1},
|
||||
},
|
||||
{
|
||||
in: `{
|
||||
"@type": "g:List",
|
||||
"@value": ["a", "b", "c"]
|
||||
}`,
|
||||
want: []string{"a", "b", "c"},
|
||||
},
|
||||
{
|
||||
in: `{
|
||||
"@type": "gx:ByteBuffer",
|
||||
"@value": "AQIDBAU="
|
||||
}`,
|
||||
want: []byte{1, 2, 3, 4, 5},
|
||||
},
|
||||
{
|
||||
in: `{
|
||||
"@type": "g:List",
|
||||
"@value": [
|
||||
{
|
||||
"@type": "gx:Byte",
|
||||
"@value": 42
|
||||
},
|
||||
{
|
||||
"@type": "gx:Byte",
|
||||
"@value": 55
|
||||
},
|
||||
{
|
||||
"@type": "gx:Byte",
|
||||
"@value": 94
|
||||
}
|
||||
]
|
||||
}`,
|
||||
want: [...]byte{42, 55},
|
||||
},
|
||||
{
|
||||
in: `{
|
||||
"@type": "g:List",
|
||||
"@value": null
|
||||
}`,
|
||||
want: []int(nil),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
tc := tc
|
||||
t.Run(fmt.Sprintf("%T", tc.want), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
typ := reflect2.TypeOf(tc.want)
|
||||
got := typ.New()
|
||||
err := NewDecoder(strings.NewReader(tc.in)).Decode(got)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tc.want, typ.Indirect(got))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeBadSlice(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
in string
|
||||
new func() interface{}
|
||||
}{
|
||||
{
|
||||
name: "TypeMismatch",
|
||||
in: `{
|
||||
"@type": "g:List",
|
||||
"@value": [
|
||||
{
|
||||
"@type": "g:Int32",
|
||||
"@value": 3
|
||||
},
|
||||
{
|
||||
"@type": "g:Int64",
|
||||
"@value": 2
|
||||
}
|
||||
]
|
||||
}`,
|
||||
new: func() interface{} { return &[]int{} },
|
||||
},
|
||||
{
|
||||
name: "BadValue",
|
||||
in: `{
|
||||
"@type": "g:List",
|
||||
"@value": [
|
||||
{
|
||||
"@type": "g:Int32",
|
||||
"@value": 3
|
||||
},
|
||||
{
|
||||
"@type": "g:Int32",
|
||||
"@value": "2"
|
||||
}
|
||||
]
|
||||
}`,
|
||||
new: func() interface{} { return &[2]int{} },
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
err := NewDecoder(strings.NewReader(tc.in)).Decode(tc.new())
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user