forked from reo7sp/go-tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
call_test.go
110 lines (89 loc) · 2.27 KB
/
call_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
package tarantool
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCall(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
box.schema.func.create('sel_all', {if_not_exists = true})
box.schema.func.create('sel_name', {if_not_exists = true})
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})
`
box, err := NewBox(tarantoolConfig, nil)
if !assert.NoError(err) {
return
}
defer box.Close()
do := func(connectOptions *Options, query *Call, 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 = &Call{}
_, 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,
&Call{
Name: "sel_all",
},
[][]interface{}{
{int64(1), "First record"},
{int64(2), "Music"},
{int64(3), "Length", int64(93)},
},
)
// call sel_name with params
do(nil,
&Call{
Name: "sel_name",
Tuple: []interface{}{int64(2), "Music"},
},
[][]interface{}{
{int64(2), "Music"},
},
)
}
func BenchmarkCallPack(b *testing.B) {
buf := make([]byte, 0)
for i := 0; i < b.N; i++ {
buf, _ = (&Call{Name: "sel_all"}).MarshalMsg(buf[:0])
}
}