forked from dimkus/go-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_test.go
107 lines (89 loc) · 2.48 KB
/
helpers_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
package clickhouse
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestNewHttpTransport(t *testing.T) {
tr := NewHttpTransport()
assert.IsType(t, HttpTransport{}, tr)
}
func TestNewQuery(t *testing.T) {
stmt := "SELECT * FROM table WHERE ?"
q := NewQuery(stmt, 1)
assert.Equal(t, stmt, q.Stmt)
assert.Equal(t, []interface{}{1}, q.args)
}
func TestBuildInsert(t *testing.T) {
var (
q Query
err error
)
q, err = BuildInsert("test", Columns{"col1", "col2"}, Row{"val1", "val2"})
assert.Equal(t, "INSERT INTO test (col1,col2) VALUES (?,?)", q.Stmt)
assert.Equal(t, []interface{}{"val1", "val2"}, q.args)
assert.NoError(t, err)
q, err = BuildInsert("test", Columns{"col1", "col2"}, Row{"val1"})
assert.Equal(t, "", q.Stmt)
assert.Error(t, err)
}
func TestBuildInsertArray(t *testing.T) {
var (
q Query
err error
)
q, err = BuildInsert("test", Columns{"col1", "col2"}, Row{"val1", Array{"val2", "val3"}})
assert.Equal(t, "INSERT INTO test (col1,col2) VALUES (?,?)", q.Stmt)
assert.Equal(t, []interface{}{"val1", Array{"val2", "val3"}}, q.args)
assert.NoError(t, err)
}
func TestNewMultiInsert(t *testing.T) {
var (
q Query
err error
)
q, err = BuildMultiInsert("test", Columns{"col1", "col2"}, Rows{
Row{"val1", "val2"},
Row{"val3", "val4"},
})
assert.Equal(t, "INSERT INTO test (col1,col2) VALUES (?,?),(?,?)", q.Stmt)
assert.Equal(t, []interface{}{"val1", "val2", "val3", "val4"}, q.args)
assert.NoError(t, err)
q, err = BuildMultiInsert("test", Columns{"col1", "col2"}, Rows{
Row{"val1", "val2"},
Row{"val3"},
})
assert.Equal(t, "", q.Stmt)
assert.Error(t, err)
//Test empty insert
q, err = BuildMultiInsert("test", Columns{}, Rows{})
assert.Equal(t, "", q.Stmt)
assert.Error(t, err)
}
func BenchmarkNewInsert(b *testing.B) {
for i := 0; i < b.N; i++ {
BuildInsert("test", Columns{"col1", "col2"}, Row{"val1", "val2"})
}
}
func getRows(n int, r Row) Rows {
res := make(Rows, n)
for i := 0; i < n; i++ {
res[i] = r
}
return res
}
func BenchmarkNewMultiInsert100(b *testing.B) {
columns := Columns{"col1", "col2", "col3", "col4"}
rows := getRows(100, Row{"val1", "val2", "val3", "val4"})
b.ResetTimer()
for i := 0; i < b.N; i++ {
BuildMultiInsert("test", columns, rows)
}
}
func BenchmarkNewMultiInsert1000(b *testing.B) {
columns := Columns{"col1", "col2", "col3", "col4"}
rows := getRows(1000, Row{"val1", "val2", "val3", "val4"})
b.ResetTimer()
for i := 0; i < b.N; i++ {
BuildMultiInsert("test", columns, rows)
}
}