forked from viciious/go-tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_test.go
141 lines (125 loc) · 2.73 KB
/
update_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
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
package tarantool
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestUpdate(t *testing.T) {
assert := assert.New(t)
tarantoolConfig := `
local s = box.schema.space.create('tester')
s:create_index('primary', {
type = 'hash',
parts = {1, 'NUM'}
})
s:create_index('secondary', {
type = 'hash',
parts = {1, 'NUM', 2, 'STR'}
})
local t = s:insert({1, 'First record', 15})
s:insert({2, 'Test', 15})
box.schema.user.create('writer', {password = 'writer'})
box.schema.user.grant('writer', 'write', 'space', 'tester')
`
box, err := NewBox(tarantoolConfig, nil)
if !assert.NoError(err) {
return
}
defer box.Close()
conn, err := box.Connect(&Options{
User: "writer",
Password: "writer",
})
assert.NoError(err)
assert.NotNil(conn)
defer conn.Close()
do := func(conn *Connection, query *Update, expected [][]interface{}) {
var err error
var buf []byte
buf, err = query.packMsg(conn.packData, buf)
if assert.NoError(err) {
var query2 = &Update{}
_, err = query2.UnmarshalMsg(buf)
if assert.NoError(err) {
assert.Equal(uint(512), query2.Space)
if query.Key != nil {
switch query.Key.(type) {
case int:
assert.Equal(query.Key, query2.Key)
default:
assert.Equal(query.Key, query2.Key)
}
}
if query.KeyTuple != nil {
assert.Equal(query.KeyTuple, query2.KeyTuple)
}
if query.Index != nil {
switch query.Index.(type) {
case string:
assert.Equal(conn.packData.indexMap[512][query.Index.(string)], uint64(query2.Index.(uint)))
default:
assert.Equal(query.Index, query2.Index)
}
}
assert.Equal(query.Set, query2.Set)
}
}
data, err := conn.Execute(query)
if assert.NoError(err) {
assert.Equal(expected, data)
}
}
do(conn, &Update{
Space: "tester",
Index: "primary",
Key: int64(1),
Set: []Operator{
&OpAdd{
Field: 2,
Argument: 17,
},
&OpAssign{
Field: 1,
Argument: "Hello World",
},
}},
[][]interface{}{
{int64(1), "Hello World", int64(32)},
})
do(conn, &Update{
Space: "tester",
Index: "secondary",
KeyTuple: []interface{}{int64(2), "Test"},
Set: []Operator{
&OpInsert{
Before: 2,
Argument: "Hello World",
},
&OpSub{
Field: 3,
Argument: 57,
},
}},
[][]interface{}{
{int64(2), "Test", "Hello World", int64(-42)},
})
}
func BenchmarkUpdatePack(b *testing.B) {
buf := make([]byte, 0)
for i := 0; i < b.N; i++ {
buf, _ = (&Update{
Space: 1,
Index: 0,
Key: 1,
Set: []Operator{
&OpAdd{
Field: 2,
Argument: 17,
},
&OpAssign{
Field: 1,
Argument: "Hello World",
},
},
}).MarshalMsg(buf[:0])
}
}