forked from xelabs/go-mysqlstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathok_test.go
More file actions
106 lines (88 loc) · 1.74 KB
/
Copy pathok_test.go
File metadata and controls
106 lines (88 loc) · 1.74 KB
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
/*
* go-mysqlstack
* xelabs.org
*
* Copyright (c) XeLabs
* GPL License
*
*/
package proto
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/xelabs/go-mysqlstack/common"
)
func TestOK(t *testing.T) {
{
buff := common.NewBuffer(32)
// header
buff.WriteU8(0x00)
// affected_rows
buff.WriteLenEncode(uint64(3))
// last_insert_id
buff.WriteLenEncode(uint64(40000000000))
// status_flags
buff.WriteU16(0x01)
// warnings
buff.WriteU16(0x02)
want := &OK{}
want.AffectedRows = 3
want.LastInsertID = 40000000000
want.StatusFlags = 1
want.Warnings = 2
got, err := UnPackOK(buff.Datas())
assert.Nil(t, err)
assert.Equal(t, want, got)
}
{
want := &OK{}
want.AffectedRows = 3
want.LastInsertID = 40000000000
want.StatusFlags = 1
want.Warnings = 2
datas := PackOK(want)
got, err := UnPackOK(datas)
assert.Nil(t, err)
assert.Equal(t, want, got)
}
}
func TestOKUnPackError(t *testing.T) {
// header error
{
buff := common.NewBuffer(32)
// header
buff.WriteU8(0x99)
_, err := UnPackOK(buff.Datas())
assert.NotNil(t, err)
}
// NULL
f0 := func(buff *common.Buffer) {
}
// Write OK header.
f1 := func(buff *common.Buffer) {
buff.WriteU8(0x00)
}
// Write AffectedRows.
f2 := func(buff *common.Buffer) {
buff.WriteLenEncode(uint64(3))
}
// Write LastInsertID.
f3 := func(buff *common.Buffer) {
buff.WriteLenEncode(uint64(3))
}
// Write Status.
f4 := func(buff *common.Buffer) {
buff.WriteU16(0x01)
}
buff := common.NewBuffer(32)
fs := []func(buff *common.Buffer){f0, f1, f2, f3, f4}
for i := 0; i < len(fs); i++ {
_, err := UnPackOK(buff.Datas())
assert.NotNil(t, err)
fs[i](buff)
}
{
_, err := UnPackOK(buff.Datas())
assert.NotNil(t, err)
}
}