-
Notifications
You must be signed in to change notification settings - Fork 0
/
clickhousetest_test.go
95 lines (79 loc) · 1.9 KB
/
clickhousetest_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
package clickhousetest
import (
"context"
"os/exec"
"testing"
)
var (
createTable = `
CREATE TABLE instruments (
token UInt64,
timestamp DateTime('Asia/Kolkata'),
price Int64,
)
ENGINE = ReplacingMergeTree()
ORDER BY (token, timestamp);
`
)
type Result struct {
DbName string `ch:"db_name"`
}
func TestStart(t *testing.T) {
server, err := Start(context.Background(), Options{})
defer server.Stop()
if err != nil {
t.Fatalf(err.Error())
}
}
func TestStartNoExec(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cmdStr := `docker run -p 127.0.0.1:8123:8123 -p 127.0.0.1:9000:9000
--ulimit nofile=262144:262144 -d clickhouse/clickhouse-server:latest`
cmd := exec.CommandContext(ctx, "/bin/sh", "-c", cmdStr)
err := cmd.Start()
if err != nil {
t.Errorf("could not start docker container : %v", err)
}
server, err := Start(context.Background(), Options{NoExec: true})
defer server.Stop()
if err != nil {
t.Errorf("could not start server : %v", err)
}
conn, err := server.NewDatabase(ctx)
if err != nil {
t.Errorf("could not create new db : %v", err)
}
var rs []Result
err = conn.Select(ctx, &rs, "SELECT currentDatabase() AS db_name;")
if err != nil {
t.Errorf("could not select current db : %v", err)
}
t.Logf("connected to new db : %s", rs)
err = conn.Exec(ctx, createTable)
if err != nil {
t.Errorf("could not create table : %v", err)
}
}
func TestNewDatabase(t *testing.T) {
ctx := context.Background()
server, err := Start(ctx, Options{})
if err != nil {
t.Fatalf(err.Error())
}
defer server.Stop()
conn, err := server.NewDatabase(ctx)
if err != nil {
t.Fatalf(err.Error())
}
var rs []Result
err = conn.Select(ctx, &rs, "SELECT currentDatabase() AS db_name;")
if err != nil {
t.Fatalf(err.Error())
}
t.Logf("connected to new db : %s", rs)
err = conn.Exec(ctx, createTable)
if err != nil {
t.Fatalf(err.Error())
}
}