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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
// 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 graphson
import (
"testing"
"github.com/modern-go/reflect2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEncodeStruct(t *testing.T) {
tests := []struct {
name string
in interface{}
want string
}{
{
name: "Simple",
in: struct {
S string
I int
}{
S: "string",
I: 1000,
},
want: `{
"S":"string",
"I": {
"@type": "g:Int64",
"@value": 1000
}
}`,
},
{
name: "Tagged",
in: struct {
ID string `json:"requestId" graphson:"g:UUID"`
Seq int `json:"seq" graphson:"g:Int32"`
Op string `json:"op" graphson:","`
Args map[string]string `json:"args"`
}{
ID: "cb682578-9d92-4499-9ebc-5c6aa73c5397",
Seq: 42,
Op: "authentication",
Args: map[string]string{
"sasl": "AHN0ZXBocGhlbgBwYXNzd29yZA==",
},
},
want: `{
"requestId": {
"@type": "g:UUID",
"@value": "cb682578-9d92-4499-9ebc-5c6aa73c5397"
},
"seq": {
"@type": "g:Int32",
"@value": 42
},
"op": "authentication",
"args": {
"@type": "g:Map",
"@value": ["sasl", "AHN0ZXBocGhlbgBwYXNzd29yZA=="]
}
}`,
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
data, err := MarshalToString(tc.in)
require.NoError(t, err)
assert.JSONEq(t, tc.want, data)
})
}
}
func TestEncodeNestedStruct(t *testing.T) {
type S struct {
Parent *S `json:"parent,omitempty"`
ID int `json:"id" graphson:"g:Int32"`
}
v := S{Parent: &S{ID: 1}, ID: 2}
want := `{
"id": {
"@type": "g:Int32",
"@value": 2
},
"parent": {
"id": {
"@type": "g:Int32",
"@value": 1
}
}
}`
got, err := MarshalToString(&v)
require.NoError(t, err)
assert.JSONEq(t, want, got)
}
func TestDecodeStruct(t *testing.T) {
tests := []struct {
name string
in string
want interface{}
}{
{
name: "Simple",
in: `{
"S":"str",
"I": {
"@type": "g:Int32",
"@value": 9999
}
}`,
want: struct {
S string
I int32
}{
S: "str",
I: 9999,
},
},
{
name: "Tagged",
in: `{
"requestId": {
"@type": "g:UUID",
"@value": "cb682578-9d92-4499-9ebc-5c6aa73c5397"
},
"seq": {
"@type": "g:Int32",
"@value": 42
},
"op": "authentication",
"args": {
"@type": "g:Map",
"@value": ["sasl", "AHN0ZXBocGhlbgBwYXNzd29yZA=="]
}
}`,
want: struct {
ID string `json:"requestId" graphson:"g:UUID"`
Seq int `json:"seq" graphson:"g:Int32"`
Op string `json:"op" graphson:","`
Args map[string]string `json:"args"`
}{
ID: "cb682578-9d92-4499-9ebc-5c6aa73c5397",
Seq: 42,
Op: "authentication",
Args: map[string]string{
"sasl": "AHN0ZXBocGhlbgBwYXNzd29yZA==",
},
},
},
{
name: "Empty",
in: `{}`,
want: struct{}{},
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
typ := reflect2.TypeOf(tc.want)
got := typ.New()
err := UnmarshalFromString(tc.in, got)
require.NoError(t, err)
assert.Equal(t, tc.want, typ.Indirect(got))
})
}
}
func TestDecodeNestedStruct(t *testing.T) {
type S struct {
Parent *S `json:"parent,omitempty"`
ID int `json:"id" graphson:"g:Int32"`
}
in := `{
"id": {
"@type": "g:Int32",
"@value": 37
},
"parent": {
"id": {
"@type": "g:Int32",
"@value": 65
}
}
}`
var got S
err := UnmarshalFromString(in, &got)
require.NoError(t, err)
want := S{Parent: &S{ID: 65}, ID: 37}
assert.Equal(t, want, got)
}
|