-
Notifications
You must be signed in to change notification settings - Fork 4
/
uuid_test.go
106 lines (90 loc) · 2.45 KB
/
uuid_test.go
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
package uuidmsgpack
import (
"bytes"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"gopkg.in/vmihailenco/msgpack.v2"
"gopkg.in/vmihailenco/msgpack.v2/codes"
"testing"
)
func TestEncodeDecode(t *testing.T) {
id := uuid.New()
buf := bytes.NewBuffer(nil)
encoder := msgpack.NewEncoder(buf)
err := encoder.Encode(id)
if err != nil {
assert.NoError(t, err)
}
reader := bytes.NewReader(buf.Bytes())
decoder := msgpack.NewDecoder(reader)
var decodedId uuid.UUID
err = decoder.Decode(&decodedId)
assert.NoError(t, err)
assert.Equal(t, id.String(), decodedId.String())
}
type customStruct struct {
a int
Uuid uuid.UUID
b string
}
func TestEncodeDecodeCustomStruct(t *testing.T) {
obj := customStruct{Uuid: uuid.New(), a: 1, b: "adsa"}
buf := bytes.NewBuffer(nil)
encoder := msgpack.NewEncoder(buf)
err := encoder.Encode(obj)
if err != nil {
assert.NoError(t, err)
}
reader := bytes.NewReader(buf.Bytes())
decoder := msgpack.NewDecoder(reader)
var decodedObj customStruct
err = decoder.Decode(&decodedObj)
assert.NoError(t, err)
assert.Equal(t, obj.Uuid.String(), decodedObj.Uuid.String())
}
func TestErrorCantReadBytes(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
reader := bytes.NewReader(buf.Bytes())
decoder := msgpack.NewDecoder(reader)
var decodedId uuid.UUID
err := decoder.Decode(&decodedId)
assert.EqualError(t, err, "can't read bytes: EOF")
}
func TestErrorInvalidBytesCount(t *testing.T) {
buf := bytes.NewBuffer([]byte{0x7d})
reader := bytes.NewReader(buf.Bytes())
decoder := msgpack.NewDecoder(reader)
var decodedId uuid.UUID
err := decoder.Decode(&decodedId)
assert.EqualError(t, err, "invalid bytes count 1 instead of 18")
}
func TestErrorWrongExtLen(t *testing.T) {
buf := bytes.NewBuffer([]byte{
0x7d, 0x44, 0x48, 0x40,
0x9d, 0xc0,
0x11, 0xd1,
0xb2, 0x45,
0x5f, 0xfd, 0xce, 0x74, 0xfa, 0xd2,
0xd3, 0xd4,
})
reader := bytes.NewReader(buf.Bytes())
decoder := msgpack.NewDecoder(reader)
var decodedId uuid.UUID
err := decoder.Decode(&decodedId)
assert.EqualError(t, err, "wrong ext len '125'")
}
func TestErrorWrongExtId(t *testing.T) {
buf := bytes.NewBuffer([]byte{
codes.FixExt16, 0x44, 0x48, 0x40,
0x9d, 0xc0,
0x11, 0xd1,
0xb2, 0x45,
0x5f, 0xfd, 0xce, 0x74, 0xfa, 0xd2,
0xd3, 0xd4,
})
reader := bytes.NewReader(buf.Bytes())
decoder := msgpack.NewDecoder(reader)
var decodedId uuid.UUID
err := decoder.Decode(&decodedId)
assert.EqualError(t, err, "wrong ext id '68'")
}