forked from reo7sp/go-tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect_test.go
120 lines (106 loc) · 3.17 KB
/
connect_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
package tarantool
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConnect(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
box, err := NewBox("", nil)
require.NoError(err)
defer box.Close()
conn, err := Connect(box.Addr(), nil)
require.NoError(err)
defer conn.Close()
assert.NotEqual(conn.greeting.Version, 0)
}
func TestMapIndexDescription(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
config := `
local s = box.schema.space.create('tester', {id = 42})
s:create_index('tester_id', {
parts = {
{field = 1, type = 'number', is_nullable = false},
},
})
local t = s:insert({1})
`
box, err := NewBox(config, nil)
require.NoError(err)
defer box.Close()
conn, err := Connect(box.Addr(), nil)
require.NoError(err)
defer conn.Close()
pkFields, ok := conn.GetPrimaryKeyFields("tester")
require.True(ok)
assert.ElementsMatch(pkFields, []int{0})
}
func TestDefaultSpace(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
config := `
local s = box.schema.space.create('tester', {id = 42})
s:create_index('tester_id', {
type = 'hash',
parts = {1, 'NUM'}
})
local t = s:insert({1})
`
box, err := NewBox(config, nil)
require.NoError(err)
defer box.Close()
conn, err := Connect(box.Addr(), &Options{
DefaultSpace: "tester",
})
require.NoError(err)
defer conn.Close()
tuples, err := conn.Execute(&Select{
Key: 1,
Index: "tester_id",
})
require.NoError(err)
assert.Equal([][]interface{}{{int64(1)}}, tuples)
}
func TestConnectOptionsDSN(t *testing.T) {
assert := assert.New(t)
tt := []struct {
uri string
user string
pass string
scheme string
host string
space string
err error
}{
// for backward compatibility
{"unix://127.0.0.1", "", "", "tcp", "127.0.0.1", "", nil},
// scheme, host, user, pass
{"tcp://127.0.0.1", "", "", "tcp", "127.0.0.1", "", nil},
{"//127.0.0.1", "", "", "tcp", "127.0.0.1", "", nil},
{"127.0.0.1", "", "", "tcp", "127.0.0.1", "", nil},
{"tcp://user:[email protected]:8000", "user", "pass", "tcp", "127.0.0.1:8000", "", nil},
{"127.0.0.1:8000", "", "", "tcp", "127.0.0.1:8000", "", nil},
{"user:[email protected]:8000", "user", "pass", "tcp", "127.0.0.1:8000", "", nil},
// path (defaultSpace)
{"127.0.0.1/", "", "", "tcp", "127.0.0.1", "", ErrEmptyDefaultSpace},
{"127.0.0.1/tester", "", "", "tcp", "127.0.0.1", "tester", nil},
// no errors due to disabled checks
{"127.0.0.1/tester/1", "", "", "tcp", "127.0.0.1", "tester/1", nil},
{"127.0.0.1/tester%20two", "", "", "tcp", "127.0.0.1", "tester two", nil},
{"127.0.0.1/tester%2Ctwo", "", "", "tcp", "127.0.0.1", "tester,two", nil},
}
for tc, item := range tt {
dsn, opts, err := parseOptions(item.uri, Options{})
assert.Equal(item.err, err, "case %v (err)", tc+1)
if err != nil {
continue
}
assert.Equal(item.scheme, dsn.Scheme, "case %v (scheme)", tc+1)
assert.Equal(item.host, dsn.Host, "case %v (host)", tc+1)
assert.Equal(item.user, opts.User, "case %v (user)", tc+1)
assert.Equal(item.pass, opts.Password, "case %v (password)", tc+1)
assert.Equal(item.space, opts.DefaultSpace, "case %v (space)", tc+1)
}
}