2023-09-11 16:52:12 +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-09-11 16:52:12 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAssocMsgpackReader(t *testing.T) {
|
|
|
|
t.Run("reading parent", func(t *testing.T) {
|
2023-11-23 17:11:43 +03:00
|
|
|
// {"f1":"blabla","f3":[2,4,6],"f2":{"field":1},"f4":[{"field":10},{"field":1024}],"f5":null}
|
|
|
|
src := "85A26631A6626C61626C61A2663393020406A2663281A56669656C6401A266349281A56669656C640A81A56669656C64CD0400A26635C0"
|
2023-09-11 16:52:12 +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.NewMsgpackAssocReader(bytes.NewReader(data))
|
|
|
|
|
|
|
|
var actual TestParent
|
|
|
|
readErr := actual.Read(rdr)
|
|
|
|
require.NoError(t, readErr)
|
|
|
|
require.EqualValues(t, expected, actual)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("reading child", func(t *testing.T) {
|
|
|
|
// {"f":"qwerty","f1":"blabla","f3":[2,4,6],"f2":{"field":1},"f4":[{"field":10},{"field":1024}]}
|
2023-11-23 17:11:43 +03:00
|
|
|
src := "85A166A6717765727479A26631A6626C61626C61A2663393020406A2663281A56669656C6401A266349281A56669656C640A81A56669656C64CD0400"
|
2023-09-11 16:52:12 +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},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := hex.DecodeString(src)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rdr := meta.NewMsgpackAssocReader(bytes.NewReader(data))
|
|
|
|
|
|
|
|
var actual TestChild
|
|
|
|
readErr := actual.Read(rdr)
|
|
|
|
require.NoError(t, readErr)
|
|
|
|
require.EqualValues(t, expected, actual)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("fail reading parent as array", func(t *testing.T) {
|
2023-11-23 17:11:43 +03:00
|
|
|
// ["blabla",[1],[2,4,6],[[10],[1024]],null]
|
|
|
|
src := "95A6626C61626C6191019302040692910A91CD0400C0"
|
2023-09-11 16:52:12 +03:00
|
|
|
|
|
|
|
expected := TestParent{}
|
|
|
|
|
|
|
|
data, err := hex.DecodeString(src)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rdr := meta.NewMsgpackAssocReader(bytes.NewReader(data))
|
|
|
|
|
|
|
|
var actual TestParent
|
|
|
|
readErr := actual.Read(rdr)
|
|
|
|
require.ErrorContains(t, readErr, "field `` is not a map")
|
|
|
|
require.EqualValues(t, expected, actual)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("fail reading parent as array with maps", func(t *testing.T) {
|
2023-11-23 17:11:43 +03:00
|
|
|
// ["blabla",{"field":1},[2,4,6],[{"field":10},{"field":1024}],null]
|
|
|
|
src := "95A6626C61626C6181A56669656C6401930204069281A56669656C640A81A56669656C64CD0400C0"
|
2023-09-11 16:52:12 +03:00
|
|
|
|
|
|
|
expected := TestParent{}
|
|
|
|
|
|
|
|
data, err := hex.DecodeString(src)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rdr := meta.NewMsgpackAssocReader(bytes.NewReader(data))
|
|
|
|
|
|
|
|
var actual TestParent
|
|
|
|
readErr := actual.Read(rdr)
|
|
|
|
require.ErrorContains(t, readErr, "field `` is not a map")
|
|
|
|
require.EqualValues(t, expected, actual)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("fail reading child as array", func(t *testing.T) {
|
2023-11-23 17:11:43 +03:00
|
|
|
// ["blabla",[1],[2,4,6],[[10],[1024]],null,"qwerty"]
|
|
|
|
src := "96A6626C61626C6191019302040692910A91CD0400C0A6717765727479"
|
2023-09-11 16:52:12 +03:00
|
|
|
|
|
|
|
expected := TestChild{}
|
|
|
|
|
|
|
|
data, err := hex.DecodeString(src)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rdr := meta.NewMsgpackAssocReader(bytes.NewReader(data))
|
|
|
|
|
|
|
|
var actual TestChild
|
|
|
|
readErr := actual.Read(rdr)
|
|
|
|
require.ErrorContains(t, readErr, "field `` is not a map")
|
|
|
|
require.EqualValues(t, expected, actual)
|
|
|
|
})
|
|
|
|
}
|