forked from reo7sp/go-tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upsert_test.go
136 lines (115 loc) · 2.23 KB
/
upsert_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
package tarantool
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestUpsert(t *testing.T) {
assert := assert.New(t)
tarantoolConfig := `
local s = box.schema.space.create('tester')
s:create_index('primary', {
type = 'hash',
parts = {1, 'NUM'}
})
local t = s:insert({1, 'First record', 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(connectOptions *Options, query *Select, expected [][]interface{}) {
conn, err := box.Connect(connectOptions)
assert.NoError(err)
assert.NotNil(conn)
defer conn.Close()
data, err := conn.Execute(query)
if assert.NoError(err) {
assert.Equal(expected, data)
}
}
// test update
data, err := conn.Execute(&Upsert{
Space: "tester",
Tuple: []interface{}{1},
Set: []Operator{
&OpAdd{
Field: 2,
Argument: 17,
},
&OpAssign{
Field: 1,
Argument: "Hello World",
},
},
})
if assert.NoError(err) {
assert.Equal([][]interface{}{}, data)
}
// check update
do(nil,
&Select{
Space: "tester",
Key: 1,
},
[][]interface{}{
{int64(1), "Hello World", int64(32)},
},
)
// test insert
data, err = conn.Execute(&Upsert{
Space: "tester",
Tuple: []interface{}{2, "Second", 16},
Set: []Operator{
&OpAdd{
Field: 2,
Argument: 17,
},
&OpAssign{
Field: 1,
Argument: "Hello World",
},
},
})
if assert.NoError(err) {
assert.Equal([][]interface{}{}, data)
}
// check insert
do(nil,
&Select{
Space: "tester",
Key: 2,
},
[][]interface{}{
{int64(2), "Second", int64(16)},
},
)
}
func BenchmarkUpsertPack(b *testing.B) {
buf := make([]byte, 0)
for i := 0; i < b.N; i++ {
buf, _ = (&Upsert{
Space: 1,
Tuple: []interface{}{1},
Set: []Operator{
&OpAdd{
Field: 2,
Argument: 17,
},
&OpAssign{
Field: 1,
Argument: "Hello World",
},
},
}).MarshalMsg(buf[:0])
}
}