-
Notifications
You must be signed in to change notification settings - Fork 44
/
sqlhooks_interface_test.go
150 lines (126 loc) · 3.84 KB
/
sqlhooks_interface_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package sqlhooks
import (
"context"
"database/sql/driver"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var interfaceTestCases = []struct {
name string
expectedInterfaces []interface{}
}{
{"Basic", []interface{}{(*driver.Conn)(nil)}},
{"Execer", []interface{}{(*driver.Execer)(nil)}},
{"ExecerContext", []interface{}{(*driver.ExecerContext)(nil)}},
{"Queryer", []interface{}{(*driver.QueryerContext)(nil)}},
{"QueryerContext", []interface{}{(*driver.QueryerContext)(nil)}},
{"ExecerQueryerContext", []interface{}{
(*driver.ExecerContext)(nil),
(*driver.QueryerContext)(nil)}},
}
type fakeDriver struct{}
func (d *fakeDriver) Open(dsn string) (driver.Conn, error) {
switch dsn {
case "Basic":
return &struct{ *FakeConnBasic }{}, nil
case "Execer":
return &struct {
*FakeConnBasic
*FakeConnExecer
}{}, nil
case "ExecerContext":
return &struct {
*FakeConnBasic
*FakeConnExecerContext
}{}, nil
case "Queryer":
return &struct {
*FakeConnBasic
*FakeConnQueryer
}{}, nil
case "QueryerContext":
return &struct {
*FakeConnBasic
*FakeConnQueryerContext
}{}, nil
case "ExecerQueryerContext":
return &struct {
*FakeConnBasic
*FakeConnExecerContext
*FakeConnQueryerContext
}{}, nil
case "ExecerQueryerContextSessionResetter":
return &struct {
*FakeConnBasic
*FakeConnExecer
*FakeConnQueryer
*FakeConnSessionResetter
}{}, nil
case "NonConnBeginTx":
return &FakeConnUnsupported{}, nil
}
return nil, errors.New("Fake driver not implemented")
}
// Conn implements a database/sql.driver.Conn
type FakeConnBasic struct{}
func (*FakeConnBasic) Prepare(query string) (driver.Stmt, error) {
return nil, errors.New("Not implemented")
}
func (*FakeConnBasic) Close() error {
return errors.New("Not implemented")
}
func (*FakeConnBasic) Begin() (driver.Tx, error) {
return nil, errors.New("Not implemented")
}
func (*FakeConnBasic) BeginTx(context.Context, driver.TxOptions) (driver.Tx, error) {
return nil, errors.New("Not implemented")
}
type FakeConnExecer struct{}
func (*FakeConnExecer) Exec(query string, args []driver.Value) (driver.Result, error) {
return nil, errors.New("Not implemented")
}
type FakeConnExecerContext struct{}
func (*FakeConnExecerContext) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
return nil, errors.New("Not implemented")
}
type FakeConnQueryer struct{}
func (*FakeConnQueryer) Query(query string, args []driver.Value) (driver.Rows, error) {
return nil, errors.New("Not implemented")
}
type FakeConnQueryerContext struct{}
func (*FakeConnQueryerContext) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
return nil, errors.New("Not implemented")
}
type FakeConnSessionResetter struct{}
func (*FakeConnSessionResetter) ResetSession(ctx context.Context) error {
return errors.New("Not implemented")
}
// FakeConnUnsupported implements a database/sql.driver.Conn but doesn't implement
// driver.ConnBeginTx.
type FakeConnUnsupported struct{}
func (*FakeConnUnsupported) Prepare(query string) (driver.Stmt, error) {
return nil, errors.New("Not implemented")
}
func (*FakeConnUnsupported) Close() error {
return errors.New("Not implemented")
}
func (*FakeConnUnsupported) Begin() (driver.Tx, error) {
return nil, errors.New("Not implemented")
}
func TestInterfaces(t *testing.T) {
drv := Wrap(&fakeDriver{}, &testHooks{})
for _, c := range interfaceTestCases {
conn, err := drv.Open(c.name)
require.NoErrorf(t, err, "Driver name %s", c.name)
for _, i := range c.expectedInterfaces {
assert.Implements(t, i, conn)
}
}
}
func TestUnsupportedDrivers(t *testing.T) {
drv := Wrap(&fakeDriver{}, &testHooks{})
_, err := drv.Open("NonConnBeginTx")
require.EqualError(t, err, "driver must implement driver.ConnBeginTx")
}