forked from viciious/go-tarantool
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheval_test.go
62 lines (51 loc) · 1.47 KB
/
eval_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
package tarantool
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func schemeGrantUserEval(username string) string {
scheme := `
box.schema.user.grant('{username}', 'execute', 'universe')
`
return strings.Replace(scheme, "{username}", username, -1)
}
func TestEvalPackUnpack(t *testing.T) {
q := &Eval{Expression: "return 2+2", Tuple: []interface{}{"test"}}
// check unpack
buf, err := q.MarshalMsg(nil)
require.NoError(t, err)
qa := &Eval{}
_, err = qa.UnmarshalMsg(buf)
require.NoError(t, err)
assert.Equal(t, q, qa)
}
func TestEvalExecute(t *testing.T) {
require := require.New(t)
assert := assert.New(t)
user := "guest"
config := schemeGrantUserEval(user)
expr := "local arg = {...} return box.cfg.listen, box.session.user(), arg[1], arg[2], arg"
args := []interface{}{"one", "two"}
q := &Eval{Expression: expr, Tuple: args}
box, err := NewBox(config, &BoxOptions{})
require.NoError(err)
defer box.Close()
tnt, err := Connect(box.Listen, &Options{})
require.NoError(err)
res, err := tnt.Execute(q)
require.NoError(err)
require.Len(res, 5)
assert.EqualValues(box.Listen, res[0][0])
assert.EqualValues(user, res[1][0])
assert.EqualValues(args[0], res[2][0])
assert.EqualValues(args[1], res[3][0])
assert.EqualValues(args, res[4])
}
func BenchmarkEvalPack(b *testing.B) {
buf := make([]byte, 0)
for i := 0; i < b.N; i++ {
buf, _ = (&Eval{Expression: "return 2+2"}).MarshalMsg(buf[:0])
}
}