-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdriver_test.go
More file actions
456 lines (400 loc) · 10.5 KB
/
driver_test.go
File metadata and controls
456 lines (400 loc) · 10.5 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package dbstats
import (
"database/sql"
"database/sql/driver"
"errors"
"io"
"testing"
"time"
)
var (
hook *fakeHook
fake *fakeDriver
queryer *fakeDriver
execer *fakeDriver
execerQueryer *fakeDriver
stats Driver
queryerStats Driver
execerStats Driver
execerQueryerStats Driver
queryerCalled bool
execerCalled bool
useColumnConverter bool
columnCoverterCalled bool
connOpenErr error
connCloseErr error
)
func init() {
hook = &fakeHook{}
fake = &fakeDriver{}
queryer = &fakeDriver{isQueryer: true}
execer = &fakeDriver{isExecer: true}
execerQueryer = &fakeDriver{isQueryer: true, isExecer: true}
stats = New(fake.Open)
queryerStats = New(queryer.Open)
execerStats = New(execer.Open)
execerQueryerStats = New(execerQueryer.Open)
stats.AddHook(hook)
queryerStats.AddHook(hook)
execerStats.AddHook(hook)
execerQueryerStats.AddHook(hook)
sql.Register("fakeStats", stats)
sql.Register("fakeQueryerStats", queryerStats)
sql.Register("fakeExecerStats", execerStats)
sql.Register("fakeExecerQueryerStats", execerQueryerStats)
}
func reset() {
fake.openNames = nil
queryerCalled = false
execerCalled = false
useColumnConverter = false
columnCoverterCalled = false
connOpenErr = nil
connCloseErr = nil
hook.reset()
}
type fakeDriver struct {
openNames []string
isQueryer bool
isExecer bool
}
func (d *fakeDriver) Open(name string) (driver.Conn, error) {
d.openNames = append(d.openNames, name)
if connOpenErr != nil {
return nil, connOpenErr
}
if d.isExecer && d.isQueryer {
return &fakeExecerQueryer{}, nil
} else if d.isQueryer {
return &fakeQueryer{}, nil
} else if d.isExecer {
return &fakeExecer{}, nil
}
return &fakeConn{}, nil
}
type fakeConn struct{}
func (c *fakeConn) Prepare(query string) (driver.Stmt, error) {
if useColumnConverter {
return &fakeColumnCoverter{}, nil
}
return &fakeStmt{}, nil
}
func (c *fakeConn) Close() error {
return connCloseErr
}
func (c *fakeConn) Begin() (driver.Tx, error) {
return &fakeTx{}, nil
}
type fakeQueryer struct{ fakeConn }
func (q *fakeQueryer) Query(query string, args []driver.Value) (driver.Rows, error) {
queryerCalled = true
return &fakeRows{}, nil
}
type fakeExecer struct{ fakeConn }
func (e *fakeExecer) Exec(query string, args []driver.Value) (driver.Result, error) {
execerCalled = true
return &fakeResult{}, nil
}
type fakeExecerQueryer struct{ fakeConn }
func (q *fakeExecerQueryer) Query(query string, args []driver.Value) (driver.Rows, error) {
queryerCalled = true
return &fakeRows{}, nil
}
func (e *fakeExecerQueryer) Exec(query string, args []driver.Value) (driver.Result, error) {
execerCalled = true
return &fakeResult{}, nil
}
type fakeTx struct{}
func (t *fakeTx) Commit() error {
return nil
}
func (t *fakeTx) Rollback() error {
return nil
}
type fakeStmt struct{}
func (s *fakeStmt) Close() error {
return nil
}
func (s *fakeStmt) NumInput() int {
return 1
}
func (s *fakeStmt) Exec(args []driver.Value) (driver.Result, error) {
return &fakeResult{}, nil
}
func (s *fakeStmt) Query(args []driver.Value) (driver.Rows, error) {
return &fakeRows{}, nil
}
type passthroughValueConverter struct{}
func (vc passthroughValueConverter) ConvertValue(v interface{}) (driver.Value, error) {
return driver.Value(v), nil
}
type fakeColumnCoverter struct{ fakeStmt }
func (vc *fakeColumnCoverter) ColumnConverter(idx int) driver.ValueConverter {
columnCoverterCalled = true
return passthroughValueConverter{}
}
type fakeRows struct {
rows int
}
func (r *fakeRows) Columns() []string {
return []string{"c0", "c1"}
}
func (r *fakeRows) Close() error {
return nil
}
func (r *fakeRows) Next(dest []driver.Value) error {
if r.rows > 0 {
return io.EOF
}
dest[0] = int64(42)
dest[1] = false
r.rows++
return nil
}
type fakeResult struct{}
func (r *fakeResult) LastInsertId() (int64, error) {
return 1, nil
}
func (r *fakeResult) RowsAffected() (int64, error) {
return 2, nil
}
type fakeHook struct {
connOpenedCount int
connClosedCount int
stmtPreparedCount int
stmtClosedCount int
txBeganCount int
txCommitedCount int
txRolledbackCount int
queriedCount int
execedCount int
rowIteratedCount int
numErr int
}
func (h *fakeHook) reset() {
h.connOpenedCount = 0
h.connClosedCount = 0
h.stmtPreparedCount = 0
h.stmtClosedCount = 0
h.txBeganCount = 0
h.txCommitedCount = 0
h.txRolledbackCount = 0
h.queriedCount = 0
h.execedCount = 0
h.rowIteratedCount = 0
h.numErr = 0
}
func (h *fakeHook) ConnOpened(err error) {
h.connOpenedCount++
if err != nil {
h.numErr++
}
}
func (h *fakeHook) ConnClosed(err error) {
h.connClosedCount++
if err != nil {
h.numErr++
}
}
func (h *fakeHook) StmtPrepared(query string, err error) {
h.stmtPreparedCount++
}
func (h *fakeHook) StmtClosed(err error) {
h.stmtClosedCount++
}
func (h *fakeHook) TxBegan(err error) {
h.txBeganCount++
}
func (h *fakeHook) TxCommitted(err error) {
h.txCommitedCount++
}
func (h *fakeHook) TxRolledback(err error) {
h.txRolledbackCount++
}
func (h *fakeHook) Queried(d time.Duration, query string, err error) {
h.queriedCount++
}
func (h *fakeHook) Execed(d time.Duration, query string, err error) {
h.execedCount++
}
func (h *fakeHook) RowIterated(err error) {
h.rowIteratedCount++
}
func TestDriverHandlerValueConverterCorrectly(t *testing.T) {
reset()
useColumnConverter = true
db, _ := sql.Open("fakeStats", "")
defer db.Close()
stmt, _ := db.Prepare("SELECT c0, c1 FROM my_table WHERE myvar=?")
defer stmt.Close()
rows, err := stmt.Query(int64(1))
if err != nil {
t.Fatalf("Failed to Query: %v", err)
}
defer rows.Close()
if !columnCoverterCalled {
t.Errorf("expected ColumnConverter interface to be called")
}
}
func TestDriverHandlesExecerQueryerCorrectly(t *testing.T) {
reset()
db, _ := sql.Open("fakeExecerQueryerStats", "")
defer db.Close()
db.Exec("UPDATE my_table SET myvar=?", 1)
db.Query("SELECT c0, c1 FROM my_table WHERE myvar=?", 1)
if !execerCalled {
t.Errorf("Expected Execer interface to be called")
}
if !queryerCalled {
t.Errorf("Expected Queryer interface to be called")
}
if hook.execedCount != 1 {
t.Errorf("Expected Execed to be called 1 time, got %d", hook.execedCount)
}
if hook.queriedCount != 1 {
t.Errorf("Expected Queried to be called 1 time, got %d", hook.queriedCount)
}
s, _ := db.Prepare("SELECT * FROM my_table WHERE id=?")
s.Close()
if hook.stmtPreparedCount != 1 {
t.Errorf("Expected StatementPrepared to be called 1 time, got %d", hook.stmtPreparedCount)
}
}
func TestDriverHandlesExecerCorrectly(t *testing.T) {
reset()
db, _ := sql.Open("fakeExecerStats", "")
defer db.Close()
_, err := db.Exec("UPDATE my_table SET myvar=?", 1)
switch {
case err != nil:
t.Errorf("Exec returned error: %v", err)
case !execerCalled:
t.Errorf("Expected execer.Exec to be called")
case hook.execedCount != 1:
t.Errorf("Expected Execed to be called 1 time, got %d", hook.execedCount)
}
}
func TestDriverHandlesQueryerCorrectly(t *testing.T) {
reset()
db, _ := sql.Open("fakeQueryerStats", "")
defer db.Close()
rows, err := db.Query("SELECT c0, c1 FROM my_table WHERE myvar=?", 1)
switch {
case err != nil:
t.Errorf("Query returned error: %v", err)
case !queryerCalled:
t.Errorf("Expected Queryer.Query to be called")
case hook.queriedCount != 1:
t.Errorf("Expected Queried to be called 1 time, got %d", hook.queriedCount)
}
rows.Close()
}
func TestDriverKeepsTxStats(t *testing.T) {
reset()
db, _ := sql.Open("fakeStats", "")
defer db.Close()
tx, _ := db.Begin()
tx2, _ := db.Begin()
if hook.txBeganCount != 2 {
t.Errorf("Expected TxBegan to be called 2 times, got %d", hook.txBeganCount)
}
tx2.Rollback()
if hook.txRolledbackCount != 1 {
t.Errorf("Expected TxRolledback to be called 1 time, got %d", hook.txRolledbackCount)
}
tx.Commit()
if hook.txCommitedCount != 1 {
t.Errorf("Expected TxCommitted to be called 1 time, got %d", hook.txCommitedCount)
}
}
func TestDriverKeepsStmtStats(t *testing.T) {
reset()
db, _ := sql.Open("fakeStats", "")
defer db.Close()
stmt, _ := db.Prepare("SELECT now()")
if hook.stmtPreparedCount != 1 {
t.Errorf("Expected StmtPrepared to be called 1 time, got %d", hook.stmtPreparedCount)
}
stmt.Exec(1)
if hook.execedCount != 1 {
t.Errorf("Expected Execed to be called 1 time, got %d", hook.queriedCount)
}
rows, _ := stmt.Query(1)
if hook.queriedCount != 1 {
t.Errorf("Expected Queried to be called 1 time, got %d", hook.queriedCount)
}
rowCount := 0
for rows.Next() {
rowCount++
}
if hook.rowIteratedCount != 1 {
t.Errorf("Expected RowIteratred to be called 1 time, got %d", hook.queriedCount)
}
rows.Close()
stmt.Close()
if hook.stmtClosedCount != 1 {
t.Errorf("Expected StmtClosed to be called 1 time, got %d", hook.stmtClosedCount)
}
}
func TestDriverFowardsToWrapped(t *testing.T) {
reset()
params := "my params"
db, _ := sql.Open("fakeStats", params)
defer db.Close()
err := db.Ping()
switch {
case err != nil:
t.Errorf("Ping returned error: %v", err)
case len(fake.openNames) == 0:
t.Errorf("Open request did not get forwarded to fakeDriver")
case fake.openNames[0] != params:
t.Errorf("Did not pass params correctly to fakeDriver: %q!=%q", fake.openNames[0], params)
}
}
func TestDriverKeepsConnectionStats(t *testing.T) {
reset()
db, _ := sql.Open("fakeStats", "")
db.SetMaxIdleConns(10)
db.Ping()
if hook.connOpenedCount != 1 {
t.Errorf("Expected hook to have ConnOpened called 1 time, got %d", hook.connOpenedCount)
}
db.Close()
if hook.connClosedCount != 1 {
t.Errorf("Expected hook to have ConnClosed called 1 time, got %d", hook.connClosedCount)
}
}
type errDriver struct{}
func (d *errDriver) Open(name string) (driver.Conn, error) {
return nil, connOpenErr
}
func TestOpenReturnsErr(t *testing.T) {
reset()
myErr := errors.New("failed to open connection")
connOpenErr = myErr
db, _ := sql.Open("fakeStats", "")
defer db.Close()
err := db.Ping()
switch {
case err != myErr:
t.Errorf("Expected error to be returned")
case hook.connOpenedCount != 1:
t.Errorf("Expected ConnOpen to be called")
case hook.numErr == 0:
t.Errorf("Expected error to be passed to hook")
}
}
func TestConnCloseReturnsErr(t *testing.T) {
reset()
myErr := errors.New("failed to open connection")
connCloseErr = myErr
db, _ := sql.Open("fakeStats", "")
db.Ping()
db.Close()
switch {
case hook.connClosedCount != 1:
t.Errorf("Expected ConnOpen to be called")
case hook.numErr == 0:
t.Errorf("Expected error to be passed to hook")
}
}