Repository navigation
Expand file tree
/
Copy pathconnect_test.go
More file actions
136 lines (121 loc) · 3.97 KB
/
Copy pathconnect_test.go
File metadata and controls
136 lines (121 loc) · 3.97 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package ok
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseQuery(t *testing.T) {
if database, table, columns := parseQuery(`INSERT inTO database.table ( a, b,s ,x) Values ()`); assert.Equal(t, "database", database) {
if assert.Equal(t, "table", table) {
assert.Equal(t, []string{"a", "b", "s", "x"}, columns)
}
}
if database, table, columns := parseQuery(`INSERT INTO db.table
(
a,
b,
s ,x)
Values ()`); assert.Equal(t, "db", database) {
if assert.Equal(t, "table", table) {
assert.Equal(t, []string{"a", "b", "s", "x"}, columns)
}
}
}
func TestColumnTypes(t *testing.T) {
conn := Connect(t, "tcp://127.0.0.1:9000?debug=0")
if version, err := conn.Version(); assert.NoError(t, err) && !version.Less(&Version{18, 0, 0}) {
if columnTypes, err := conn.(*clickhouse).columnTypes("system", "tables", []string{"database", "engine"}); assert.NoError(t, err) {
assert.Equal(t, []string{"String", "String"}, columnTypes)
}
}
}
func TestVersion(t *testing.T) {
if assert.True(t, (&Version{1, 2, 3}).Equal(&Version{1, 2, 3})) {
}
if assert.True(t, (&Version{1, 2, 3}).Less(&Version{1, 2, 4})) {
assert.True(t, (&Version{1, 2, 3}).Less(&Version{2, 0, 0}))
assert.True(t, (&Version{2, 0, 3}).Less(&Version{2, 1, 0}))
}
if assert.False(t, (&Version{2, 2, 3}).Less(&Version{1, 2, 4})) {
assert.False(t, (&Version{2, 2, 3}).Less(&Version{2, 0, 0}))
assert.False(t, (&Version{2, 1, 3}).Less(&Version{2, 1, 0}))
}
clickhouse := Connect(t, "tcp://127.0.0.1:9000?debug=0")
if version, err := clickhouse.Version(); assert.NoError(t, err) {
switch {
case version.Less(&Version{18, 0, 0}):
t.Logf("old version: %s", version)
case version.Less(&Version{19, 0, 0}):
t.Logf("version 18 X: %s", version)
case version.Less(&Version{20, 0, 0}):
t.Logf("version 19 X: %s", version)
default:
t.Logf("version: %s", version)
}
}
}
func TestBase(t *testing.T) {
clickhouse := Connect(t, "tcp://127.0.0.1:9000?debug=0")
if databases, err := clickhouse.ShowDatabases(); assert.NoError(t, err) {
if assert.True(t, len(databases) != 0) {
var exists bool
for _, database := range databases {
if database == "system" {
exists = true
break
}
}
assert.True(t, exists)
}
}
if tables, err := clickhouse.ShowTables("system"); assert.NoError(t, err) {
if assert.True(t, len(tables) != 0) {
var exists bool
for _, table := range tables {
if table == "settings" {
exists = true
break
}
}
assert.True(t, exists)
}
}
if exists := clickhouse.DatabaseExists("system"); assert.True(t, exists) {
assert.False(t, clickhouse.DatabaseExists("not-exists"))
}
if exists := clickhouse.TableExists("system", "settings"); assert.True(t, exists) {
assert.False(t, clickhouse.TableExists("system", "not-exists"))
}
}
func TestDictionary(t *testing.T) {
clickhouse := Connect(t, "tcp://127.0.0.1:9000?debug=0")
if exists := clickhouse.DictionaryExists("dictionary"); exists {
assert.True(t, clickhouse.ReloadDictionary("dictionary"))
}
}
func TestExtractCreateDatabase(t *testing.T) {
assets := map[string]string{
"CREATE DATABASE test;": "test",
"CREATE DATABASE test": "test",
"CREATE DATABASE IF NOT EXISTS test": "test",
"create \n database test": "test",
"CREATE TABLE db.table": "",
}
for src, expected := range assets {
assert.Equal(t, expected, extractCreateDatabase(src))
}
}
func TestExtractCreateTable(t *testing.T) {
assets := map[string][]string{
"CREATE TABLE table": []string{"", "table"},
"CREATE TABLE db.table": []string{"db", "table"},
"CREATE TABLE IF NOT exists db.table": []string{"db", "table"},
"CREATE TABLE IF NOT exists db.table(": []string{"db", "table"},
"CREATE TABLE IF NOT exists db.table (": []string{"db", "table"},
}
for src, expected := range assets {
database, table := extractCreateTable(src)
{
assert.Equal(t, expected, []string{database, table})
}
}
}