forked from reo7sp/go-tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_test.go
85 lines (67 loc) · 1.63 KB
/
insert_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
package tarantool
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestInsert(t *testing.T) {
assert := assert.New(t)
tarantoolConfig := `
local s = box.schema.space.create('tester', {id = 42})
s:create_index('primary', {
type = 'hash',
parts = {1, 'NUM'}
})
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(query *Insert) ([][]interface{}, error) {
var err error
var buf []byte
buf, err = query.packMsg(conn.packData, buf)
if assert.NoError(err) {
var query2 = &Insert{}
_, err = query2.UnmarshalMsg(buf)
if assert.NoError(err) {
assert.Equal(uint(42), query2.Space)
assert.Equal(query.Tuple, query2.Tuple)
} else {
return nil, err
}
} else {
return nil, err
}
return conn.Execute(query)
}
data, err := do(&Insert{
Space: "tester",
Tuple: []interface{}{int64(4), "Hello"},
})
if assert.NoError(err) {
assert.Equal([][]interface{}{{int64(4), "Hello"}}, data)
}
_, err = do(&Insert{
Space: "tester",
Tuple: []interface{}{int64(4), "World"},
})
if assert.Error(err) {
assert.Contains(err.Error(), "Duplicate key exists")
}
}
func BenchmarkInsertPack(b *testing.B) {
buf := make([]byte, 0)
for i := 0; i < b.N; i++ {
buf, _ = (&Insert{Tuple: []interface{}{3, "Hello world"}}).MarshalMsg(buf[:0])
}
}