forked from mailru/go-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
117 lines (108 loc) · 3.81 KB
/
config_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
package clickhouse
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestParseDSN(t *testing.T) {
dsn := "http://username:password@localhost:8123/test?timeout=1s&idle_timeout=2s&read_timeout=3s" +
"&write_timeout=4s&location=Local&max_execution_time=10&debug=1&kill_query=1&kill_query_timeout=5s"
cfg, err := ParseDSN(dsn)
if assert.NoError(t, err) {
assert.Equal(t, "username", cfg.User)
assert.Equal(t, "password", cfg.Password)
assert.Equal(t, "http", cfg.Scheme)
assert.Equal(t, "localhost:8123", cfg.Host)
assert.Equal(t, "test", cfg.Database)
assert.Equal(t, time.Second, cfg.Timeout)
assert.Equal(t, 2*time.Second, cfg.IdleTimeout)
assert.Equal(t, 3*time.Second, cfg.ReadTimeout)
assert.Equal(t, 4*time.Second, cfg.WriteTimeout)
assert.Equal(t, 5*time.Second, cfg.KillQueryTimeout)
assert.Equal(t, time.Local, cfg.Location)
assert.True(t, cfg.Debug)
assert.True(t, cfg.KillQueryOnErr)
assert.Equal(t, map[string]string{"max_execution_time": "10"}, cfg.Params)
}
}
func TestDefaultConfig(t *testing.T) {
cfg := NewConfig()
assert.Equal(t, "http", cfg.Scheme)
assert.Equal(t, "localhost:8123", cfg.Host)
assert.Empty(t, cfg.Database)
assert.Empty(t, cfg.User)
assert.Empty(t, cfg.Password)
assert.False(t, cfg.Debug)
assert.False(t, cfg.KillQueryOnErr)
assert.Equal(t, time.UTC, cfg.Location)
assert.EqualValues(t, 0, cfg.ReadTimeout)
assert.EqualValues(t, 0, cfg.WriteTimeout)
assert.EqualValues(t, 0, cfg.KillQueryTimeout)
assert.Equal(t, time.Hour, cfg.IdleTimeout)
assert.Empty(t, cfg.Params)
dsn := cfg.FormatDSN()
expected := "http://localhost:8123/?idle_timeout=1h0m0s"
assert.Equal(t, expected, dsn)
}
func TestParseWrongDSN(t *testing.T) {
testCases := []string{
"http://localhost:8123/?database=test",
"http://localhost:8123/?default_format=Native",
"http://localhost:8123/?query=SELECT%201",
}
for _, tc := range testCases {
_, err := ParseDSN(tc)
assert.Error(t, err)
}
}
func TestFormatDSN(t *testing.T) {
dsn := "http://username:password@localhost:8123/test?timeout=1s&idle_timeout=2s&read_timeout=3s" +
"&write_timeout=4s&location=Europe%2FMoscow&max_execution_time=10&debug=1&kill_query=1&kill_query_timeout=5s"
cfg, err := ParseDSN(dsn)
if assert.NoError(t, err) {
dsn2 := cfg.FormatDSN()
assert.Equal(t, len(dsn), len(dsn2))
assert.Contains(t, dsn2, "http://username:password@localhost:8123/test?")
assert.Contains(t, dsn2, "timeout=1s")
assert.Contains(t, dsn2, "idle_timeout=2s")
assert.Contains(t, dsn2, "read_timeout=3s")
assert.Contains(t, dsn2, "write_timeout=4s")
assert.Contains(t, dsn2, "location=Europe%2FMoscow")
assert.Contains(t, dsn2, "max_execution_time=10")
assert.Contains(t, dsn2, "debug=1")
assert.Contains(t, dsn2, "kill_query=1")
assert.Contains(t, dsn2, "kill_query_timeout=5s")
}
}
func TestConfigURL(t *testing.T) {
dsn := "http://username:password@localhost:8123/test?timeout=1s&idle_timeout=2s&read_timeout=3s" +
"&write_timeout=4s&location=Local&max_execution_time=10"
cfg, err := ParseDSN(dsn)
if assert.NoError(t, err) {
u1 := cfg.url(nil, true).String()
assert.Equal(t, "http://username:password@localhost:8123/test?max_execution_time=10", u1)
u2 := cfg.url(map[string]string{"default_format": "Native"}, false).String()
assert.Contains(t, u2, "http://username:password@localhost:8123/?")
assert.Contains(t, u2, "default_format=Native")
assert.Contains(t, u2, "database=test")
}
}
func TestDefaultPort(t *testing.T) {
testCases := []struct {
in string
out string
}{
{"http://localhost/test", "http://localhost:8123/test"},
{"http://[de:ad:be:ef::ca:fe]/test", "http://[de:ad:be:ef::ca:fe]:8123/test"},
}
var (
cfg *Config
err error
)
for _, tc := range testCases {
cfg, err = ParseDSN(tc.in)
if assert.NoError(t, err) {
assert.Equal(t, tc.out, cfg.url(nil, true).String())
}
}
}