-
Notifications
You must be signed in to change notification settings - Fork 52
/
client_test.go
116 lines (99 loc) · 2.49 KB
/
client_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
package ch
import (
"context"
"errors"
"os"
"testing"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
"github.com/ClickHouse/ch-go/cht"
"github.com/ClickHouse/ch-go/internal/gold"
"github.com/ClickHouse/ch-go/proto"
)
func TestMain(m *testing.M) {
// Explicitly registering flags for golden files.
gold.Init()
os.Exit(m.Run())
}
func ConnOpt(t testing.TB, opt Options) *Client {
t.Helper()
ctx := context.Background()
server := cht.New(t)
if opt.Logger == nil {
opt.Logger = zaptest.NewLogger(t)
}
opt.Address = server.TCP
client, err := Dial(ctx, opt)
require.NoError(t, err)
t.Log("Connected", client.ServerInfo())
t.Cleanup(func() {
require.NoError(t, client.Close())
})
return client
}
func Conn(t testing.TB) *Client {
return ConnOpt(t, Options{})
}
func SkipNoFeature(t *testing.T, client *Client, feature proto.Feature) {
if !client.ServerInfo().Has(feature) {
t.Skipf("Skipping (feature %q not supported)", feature)
}
}
func TestDial(t *testing.T) {
t.Run("Ok", func(t *testing.T) {
conn := Conn(t)
require.NoError(t, conn.Ping(context.Background()))
})
t.Run("Closed", func(t *testing.T) {
ctx := context.Background()
server := cht.New(t)
conn, err := Dial(ctx, Options{
Address: server.TCP,
})
require.NoError(t, err)
require.NoError(t, conn.Ping(ctx))
require.NoError(t, conn.Close())
require.ErrorIs(t, conn.Ping(ctx), ErrClosed)
require.ErrorIs(t, conn.Do(ctx, Query{}), ErrClosed)
})
t.Run("DatabaseNotFound", func(t *testing.T) {
ctx := context.Background()
server := cht.New(t)
client, err := Dial(ctx, Options{
Address: server.TCP,
Database: "bad",
})
if IsErr(err, proto.ErrUnknownDatabase) {
t.Skip("got error during handshake")
}
require.NoError(t, err)
err = client.Do(ctx, Query{
Body: "SELECT 1",
Result: discardResult(),
})
require.True(t, IsErr(err, proto.ErrUnknownDatabase))
})
}
func TestExceptionUnwrap(t *testing.T) {
flat := &Exception{
Code: proto.ErrReadonly,
Name: "foo",
Message: "bar",
Next: nil,
}
if !errors.Is(flat, proto.ErrReadonly) {
t.Fatal("flat exception must be the error code it represents")
}
nested := &Exception{
Code: proto.ErrAborted,
Name: "foo",
Message: "bar",
Next: []Exception{*flat},
}
if !errors.Is(nested, proto.ErrAborted) {
t.Fatal("nested exception must be the error code it represents")
}
if !errors.Is(nested, proto.ErrReadonly) {
t.Fatal("nested exception must be the error code it wraps")
}
}