forked from dimkus/go-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport_test.go
78 lines (63 loc) · 1.96 KB
/
transport_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
package clickhouse
import (
"fmt"
"github.com/stretchr/testify/assert"
"io/ioutil"
"mime"
"mime/multipart"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
type TestHandler struct {
Result string
}
func (h *TestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/tab-separated-values; charset=UTF-8")
fmt.Fprint(w, h.Result)
}
func TestExec(t *testing.T) {
handler := &TestHandler{Result: "1 2.5 clickid68235\n2 -0.14 clickidsdkjhj44"}
server := httptest.NewServer(handler)
defer server.Close()
transport := HttpTransport{}
conn := Conn{Host: server.URL, transport: transport}
q := NewQuery("SELECT * FROM testdata")
resp, err := transport.Exec(&conn, q, false)
assert.Equal(t, nil, err)
assert.Equal(t, handler.Result, resp)
}
func TestExecReadOnly(t *testing.T) {
handler := &TestHandler{Result: "1 2.5 clickid68235\n2 -0.14 clickidsdkjhj44"}
server := httptest.NewServer(handler)
defer server.Close()
transport := HttpTransport{}
conn := Conn{Host: server.URL, transport: transport}
q := NewQuery(url.QueryEscape("SELECT * FROM testdata"))
query := prepareHttp(q.Stmt, q.args)
query = "?query=" + url.QueryEscape(query)
resp, err := transport.Exec(&conn, q, true)
assert.Equal(t, nil, err)
assert.Equal(t, handler.Result, resp)
}
func TestPrepareHttp(t *testing.T) {
p := prepareHttp("SELECT * FROM table WHERE key = ?", []interface{}{"test"})
assert.Equal(t, "SELECT * FROM table WHERE key = 'test'", p)
}
func TestPrepareHttpArray(t *testing.T) {
p := prepareHttp("INSERT INTO table (arr) VALUES (?)", Row{Array{"val1", "val2"}})
assert.Equal(t, "INSERT INTO table (arr) VALUES (['val1','val2'])", p)
}
func BenchmarkPrepareHttp(b *testing.B) {
params := strings.Repeat("(?,?,?,?,?,?,?,?)", 1000)
args := make([]interface{}, 8000)
for i := 0; i < 8000; i++ {
args[i] = "test"
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
prepareHttp("INSERT INTO t VALUES "+params, args)
}
}