2023-06-06 15:14:33 +03:00
|
|
|
package meta_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
|
|
|
"testing"
|
|
|
|
|
2023-09-11 17:41:16 +03:00
|
|
|
"git.bit5.ru/backend/meta/v5"
|
2023-06-06 15:14:33 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMsgpackReader(t *testing.T) {
|
2023-09-11 16:51:34 +03:00
|
|
|
t.Run("reading parent", func(t *testing.T) {
|
2023-11-23 17:11:43 +03:00
|
|
|
// ["blabla",[1],[2,4,6],[[10],[1024]],null]
|
|
|
|
src := "95A6626C61626C6191019302040692910A91CD0400C0"
|
2023-06-06 15:14:33 +03:00
|
|
|
|
|
|
|
expected := TestParent{
|
|
|
|
Field1: "blabla",
|
|
|
|
Field2: TestFoo{
|
|
|
|
Field: 1,
|
|
|
|
},
|
|
|
|
Field3: []int8{2, 4, 6},
|
|
|
|
Field4: []TestFoo{
|
|
|
|
{Field: 10},
|
|
|
|
{Field: 1024},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := hex.DecodeString(src)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rdr := meta.NewMsgpackReader(bytes.NewReader(data))
|
|
|
|
|
|
|
|
var actual TestParent
|
|
|
|
readErr := actual.Read(rdr)
|
|
|
|
require.NoError(t, readErr)
|
|
|
|
require.EqualValues(t, expected, actual)
|
|
|
|
})
|
|
|
|
|
2023-09-11 16:51:34 +03:00
|
|
|
t.Run("reading child", func(t *testing.T) {
|
2023-11-23 17:11:43 +03:00
|
|
|
// ["blabla",[1],[2,4,6],[[10],[1024]],null,"qwerty"]
|
|
|
|
src := "96A6626C61626C6191019302040692910A91CD0400C0A6717765727479"
|
2023-06-06 15:14:33 +03:00
|
|
|
|
2023-09-11 16:51:34 +03:00
|
|
|
expected := TestChild{
|
|
|
|
Field: "qwerty",
|
|
|
|
TestParent: TestParent{
|
|
|
|
Field1: "blabla",
|
|
|
|
Field2: TestFoo{
|
|
|
|
Field: 1,
|
|
|
|
},
|
|
|
|
Field3: []int8{2, 4, 6},
|
|
|
|
Field4: []TestFoo{
|
|
|
|
{Field: 10},
|
|
|
|
{Field: 1024},
|
|
|
|
},
|
2023-06-06 15:14:33 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := hex.DecodeString(src)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rdr := meta.NewMsgpackReader(bytes.NewReader(data))
|
|
|
|
|
2023-09-11 16:51:34 +03:00
|
|
|
var actual TestChild
|
2023-06-06 15:14:33 +03:00
|
|
|
readErr := actual.Read(rdr)
|
|
|
|
require.NoError(t, readErr)
|
|
|
|
require.EqualValues(t, expected, actual)
|
|
|
|
})
|
|
|
|
|
2023-09-11 16:51:34 +03:00
|
|
|
t.Run("fail reading parent as map", func(t *testing.T) {
|
|
|
|
// {"f1":"blabla","f3":[2,4,6],"f2":{"field":1},"f4":[{"field":10},{"field":1024}]}
|
|
|
|
src := "84a26631a6626c61626c61a2663393020406a2663281a56669656c6401a266349281a56669656c640a81a56669656c64cd0400"
|
2023-06-06 15:14:33 +03:00
|
|
|
|
2023-09-11 16:51:34 +03:00
|
|
|
expected := TestParent{}
|
2023-06-06 15:14:33 +03:00
|
|
|
|
|
|
|
data, err := hex.DecodeString(src)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rdr := meta.NewMsgpackReader(bytes.NewReader(data))
|
|
|
|
|
|
|
|
var actual TestParent
|
|
|
|
readErr := actual.Read(rdr)
|
2023-09-11 16:51:34 +03:00
|
|
|
require.ErrorContains(t, readErr, "field `` is not an array")
|
2023-06-06 15:14:33 +03:00
|
|
|
require.EqualValues(t, expected, actual)
|
|
|
|
})
|
|
|
|
|
2023-09-11 16:51:34 +03:00
|
|
|
t.Run("fail reading parent as array with maps", func(t *testing.T) {
|
|
|
|
// ["blabla",{"field":1},[2,4,6],[{"field":10},{"field":1024}]]
|
|
|
|
src := "94a6626c61626c6181a56669656c6401930204069281a56669656c640a81a56669656c64cd0400"
|
2023-06-06 15:14:33 +03:00
|
|
|
|
2023-09-11 16:51:34 +03:00
|
|
|
expected := TestParent{
|
|
|
|
Field1: "blabla",
|
|
|
|
Field3: []int8{},
|
|
|
|
Field4: []TestFoo{},
|
2023-06-06 15:14:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := hex.DecodeString(src)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rdr := meta.NewMsgpackReader(bytes.NewReader(data))
|
|
|
|
|
2023-09-11 16:51:34 +03:00
|
|
|
var actual TestParent
|
2023-06-06 15:14:33 +03:00
|
|
|
readErr := actual.Read(rdr)
|
2023-09-11 16:51:34 +03:00
|
|
|
require.ErrorContains(t, readErr, "field `f2` is not an array")
|
2023-06-06 15:14:33 +03:00
|
|
|
require.EqualValues(t, expected, actual)
|
|
|
|
})
|
|
|
|
|
2023-09-11 16:51:34 +03:00
|
|
|
t.Run("fail reading child struct as map", func(t *testing.T) {
|
|
|
|
// {"f":"qwerty","f1":"blabla","f3":[2,4,6],"f2":{"field":1},"f4":[{"field":10},{"field":1024}]}
|
|
|
|
src := "85a166a6717765727479a26631a6626c61626c61a2663393020406a2663281a56669656c6401a266349281a56669656c640a81a56669656c64cd0400"
|
2023-06-06 15:14:33 +03:00
|
|
|
|
2023-09-11 16:51:34 +03:00
|
|
|
expected := TestChild{}
|
2023-06-06 15:14:33 +03:00
|
|
|
|
|
|
|
data, err := hex.DecodeString(src)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rdr := meta.NewMsgpackReader(bytes.NewReader(data))
|
|
|
|
|
|
|
|
var actual TestChild
|
|
|
|
readErr := actual.Read(rdr)
|
2023-09-11 16:51:34 +03:00
|
|
|
require.ErrorContains(t, readErr, "field `` is not an array")
|
2023-06-06 15:14:33 +03:00
|
|
|
require.EqualValues(t, expected, actual)
|
|
|
|
})
|
|
|
|
}
|