-
Notifications
You must be signed in to change notification settings - Fork 13
/
call17_test.go
261 lines (220 loc) · 4.53 KB
/
call17_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package tarantool
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCall17(t *testing.T) {
assert := assert.New(t)
tarantoolConfig := `
local s = box.schema.space.create('tester', {id = 42})
s:create_index('tester_id', {
type = 'hash',
parts = {1, 'NUM'}
})
s:create_index('tester_name', {
type = 'hash',
parts = {2, 'STR'}
})
s:create_index('id_name', {
type = 'hash',
parts = {1, 'NUM', 2, 'STR'},
unique = true
})
local t = s:insert({1, 'First record'})
t = s:insert({2, 'Music'})
t = s:insert({3, 'Length', 93})
function sel_all()
return box.space.tester:select{}
end
function sel_name(tester_id, name)
return box.space.tester.index.id_name:select{tester_id, name}
end
function call_case_1()
return 1
end
function call_case_2()
return 1, 2, 3
end
function call_case_3()
return true
end
function call_case_4()
return nil
end
function call_case_5()
return {}
end
function call_case_6()
return {1}
end
function call_case_7()
return {1, 2, 3}
end
function call_case_8()
return {1, 2, 3}, {'a', 'b', 'c'}, {true, false}
end
function call_case_9()
return {key1 = 'value1', key2 = 'value2'}
end
local number_of_extra_cases = 9
box.schema.func.create('sel_all', {if_not_exists = true})
box.schema.func.create('sel_name', {if_not_exists = true})
for i = 1, number_of_extra_cases do
box.schema.func.create('call_case_'..i, {if_not_exists = true})
end
box.schema.user.grant('guest', 'execute', 'function', 'sel_all', {if_not_exists = true})
box.schema.user.grant('guest', 'execute', 'function', 'sel_name', {if_not_exists = true})
for i = 1, number_of_extra_cases do
box.schema.user.grant('guest', 'execute', 'function', 'call_case_'..i, {if_not_exists = true})
end
`
box, err := NewBox(tarantoolConfig, nil)
if !assert.NoError(err) {
return
}
defer box.Close()
ver, err := box.Version()
if !assert.NoError(err) {
return
}
if strings.HasPrefix(ver, "1.6") {
t.Skip("requires tarantool >= 1.7.2")
}
do := func(connectOptions *Options, query *Call17, expected [][]interface{}) {
var buf []byte
conn, err := box.Connect(connectOptions)
assert.NoError(err)
assert.NotNil(conn)
defer conn.Close()
buf, err = query.MarshalMsg(nil)
if assert.NoError(err) {
var query2 = &Call17{}
_, err = query2.UnmarshalMsg(buf)
if assert.NoError(err) {
assert.Equal(query.Name, query2.Name)
assert.Equal(query.Tuple, query2.Tuple)
}
}
data, err := conn.Execute(query)
if assert.NoError(err) {
assert.Equal(expected, data)
}
}
// call sel_all without params
do(nil,
&Call17{
Name: "sel_all",
},
[][]interface{}{
{
[]interface{}{int64(1), "First record"},
[]interface{}{int64(2), "Music"},
[]interface{}{int64(3), "Length", int64(93)},
},
},
)
// call sel_name with params
do(nil,
&Call17{
Name: "sel_name",
Tuple: []interface{}{int64(2), "Music"},
},
[][]interface{}{
{
[]interface{}{int64(2), "Music"},
},
},
)
// For stored procedures the result is returned in the same way as eval (in certain cases).
// Note that returning arrays (also an empty table) is a special case.
// scalar 1
do(nil,
&Call17{
Name: "call_case_1",
},
[][]interface{}{
{int64(1)},
},
)
// multiple scalars
do(nil,
&Call17{
Name: "call_case_2",
},
[][]interface{}{
{int64(1)}, {int64(2)}, {int64(3)},
},
)
// scalar true
do(nil,
&Call17{
Name: "call_case_3",
},
[][]interface{}{
{true},
},
)
// scalar nil
do(nil,
&Call17{
Name: "call_case_4",
},
[][]interface{}{
{nil},
},
)
// empty table
do(nil,
&Call17{
Name: "call_case_5",
},
[][]interface{}{
{},
},
)
// array with len 1 (similar to case 1)
do(nil,
&Call17{
Name: "call_case_6",
},
[][]interface{}{
{int64(1)},
},
)
// single array with len 3
do(nil,
&Call17{
Name: "call_case_7",
},
[][]interface{}{
{int64(1), int64(2), int64(3)},
},
)
// multiple arrays
do(nil,
&Call17{
Name: "call_case_8",
},
[][]interface{}{
{int64(1), int64(2), int64(3)},
{"a", "b", "c"},
{true, false},
},
)
// map with string keys
do(nil,
&Call17{
Name: "call_case_9",
},
[][]interface{}{
{map[string]interface{}{"key1": "value1", "key2": "value2"}},
},
)
}
func BenchmarkCall17Pack(b *testing.B) {
buf := make([]byte, 0)
for i := 0; i < b.N; i++ {
buf, _ = (&Call17{Name: "sel_all"}).MarshalMsg(buf[:0])
}
}