-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn_test.go
564 lines (444 loc) · 14.5 KB
/
conn_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
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
package unixodbc
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"github.com/golang/mock/gomock"
"github.com/ninthclowd/unixodbc/internal/cache"
"github.com/ninthclowd/unixodbc/internal/mocks"
"github.com/ninthclowd/unixodbc/internal/odbc"
"testing"
"time"
)
func testDBConnection(t *testing.T, cacheSize int) (ctrl *gomock.Controller, conn *sql.Conn, mockConn *mocks.MockConnection) {
ctrl = gomock.NewController(t)
mockEnv := mocks.NewMockEnvironment(ctrl)
mockEnv.EXPECT().SetVersion(odbc.Version380).Return(nil).Times(1)
mockEnv.EXPECT().SetPoolOption(odbc.PoolOff).Return(nil).Times(1)
connString := "connString"
db := sql.OpenDB(&Connector{
ConnectionString: StaticConnStr(connString),
StatementCacheSize: cacheSize,
odbcEnvironment: mockEnv,
})
ctx := context.Background()
mockConn = mocks.NewMockConnection(ctrl)
mockConn.EXPECT().SetAutoCommit(true).Return(nil).Times(1)
mockEnv.EXPECT().Connect(gomock.Any(), connString).Return(mockConn, nil)
var err error
conn, err = db.Conn(ctx)
if err != nil {
t.Fatalf("unable to open connection: %v", err)
}
return
}
func testConnection(t *testing.T, cacheSize int) (ctrl *gomock.Controller, conn *Connection, mockConn *mocks.MockConnection) {
ctrl = gomock.NewController(t)
mockConn = mocks.NewMockConnection(ctrl)
conn = &Connection{
odbcConnection: mockConn,
cachedStatements: cache.NewLRU[PreparedStatement](cacheSize, onCachePurged),
uncachedStatements: map[*PreparedStatement]bool{},
}
return
}
func TestConnection_IsValid(t *testing.T) {
ctrl, conn, mockConn := testConnection(t, 1)
defer ctrl.Finish()
mockConn.EXPECT().Ping().Return(nil).Times(1)
if conn.IsValid() != true {
t.Fatal("expected IsValid to be true if the Ping is successful")
}
mockConn.EXPECT().Ping().Return(odbc.ErrConnectionDead).Times(1)
if conn.IsValid() != false {
t.Fatal("expected IsValid to be false if the Ping fails")
}
}
func TestConnection_Ping(t *testing.T) {
type Test struct {
Description string
HasConnection bool
PingError error
WantError error
}
tests := []Test{
{
Description: "it should return ErrBadConn if Ping returns ErrConnectionDead",
WantError: driver.ErrBadConn,
PingError: odbc.ErrConnectionDead,
HasConnection: true,
},
{
Description: "it should return ErrBadConn if no connection",
WantError: driver.ErrBadConn,
HasConnection: false,
},
{
Description: "it should return no error if ping is successful",
WantError: nil,
PingError: nil,
HasConnection: true,
},
}
for _, test := range tests {
t.Run(test.Description, func(t *testing.T) {
ctrl, conn, mockConn := testConnection(t, 1)
defer ctrl.Finish()
mockConn.EXPECT().Ping().Return(test.PingError).AnyTimes()
if !test.HasConnection {
conn.odbcConnection = nil
}
gotErr := conn.Ping(context.Background())
if !errors.Is(gotErr, test.WantError) {
t.Fatalf("expected error %v, got %v", test.WantError, gotErr)
}
})
}
}
func TestConnection_CheckNamedValue(t *testing.T) {
type Test struct {
Description string
Value any
ShouldReject bool
}
tests := []Test{
{
Description: "int8",
Value: int8(1),
ShouldReject: false,
},
{
Description: "int16",
Value: int16(1),
ShouldReject: false,
},
{
Description: "int32",
Value: int32(1),
ShouldReject: false,
},
{
Description: "int64",
Value: int64(1),
ShouldReject: false,
},
{
Description: "string",
Value: "string",
ShouldReject: false,
},
{
Description: "boolean",
Value: true,
ShouldReject: false,
},
{
Description: "nil",
Value: nil,
ShouldReject: false,
},
{
Description: "time",
Value: time.Now(),
ShouldReject: false,
},
{
Description: "[]byte",
Value: []byte{1, 2, 3},
ShouldReject: false,
},
{
Description: "struct",
Value: struct{}{},
ShouldReject: true,
},
}
for _, test := range tests {
t.Run(test.Description, func(t *testing.T) {
ctrl, conn, _ := testConnection(t, 1)
defer ctrl.Finish()
gotErr := conn.CheckNamedValue(&driver.NamedValue{
Name: "Col",
Ordinal: 0,
Value: test.Value,
})
if test.ShouldReject {
if !errors.Is(gotErr, driver.ErrRemoveArgument) {
t.Fatal("expected ErrRemoveArgument")
}
} else if gotErr != nil {
t.Fatalf("expected no error but got %v", gotErr)
}
})
}
}
func TestConnection_Close(t *testing.T) {
ctrl, conn, mockConn := testConnection(t, 1)
defer ctrl.Finish()
mockConn.EXPECT().Close().Return(nil).Times(1)
err := conn.Close()
if err != nil {
t.Errorf("expected no error but got %v", err)
}
if conn.odbcConnection != nil {
t.Error("expected connection to be closed")
}
}
func TestConnection_BeginTx(t *testing.T) {
ctrl, conn, mockConn := testConnection(t, 1)
defer ctrl.Finish()
mockConn.EXPECT().SetIsolationLevel(odbc.LevelReadCommitted).Return(nil).Times(1)
mockConn.EXPECT().SetReadOnlyMode(odbc.ModeReadOnly).Return(nil).Times(1)
mockConn.EXPECT().SetAutoCommit(false).Return(nil).Times(1)
tx, err := conn.BeginTx(context.Background(), driver.TxOptions{
Isolation: driver.IsolationLevel(sql.LevelReadCommitted),
ReadOnly: true,
})
if err != nil {
t.Errorf("expected no error but got %v", err)
}
if tx == nil {
t.Errorf("expected a transaction but got nil")
}
mockConn.EXPECT().Commit().Return(nil).Times(1)
mockConn.EXPECT().SetAutoCommit(true).Return(nil).Times(1)
err = tx.Commit()
if err != nil {
t.Errorf("expected no error from commit but got %v", err)
}
}
func TestConnection_PrepareContext(t *testing.T) {
ctrl, conn, mockConn := testConnection(t, 1)
defer ctrl.Finish()
q := "SELECT * FROM foo WHERE bar = ?"
ctx := context.Background()
mockStmt1 := mocks.NewMockStatement(ctrl)
mockStmt1.EXPECT().Prepare(gomock.Any(), q).Return(nil).Times(1)
mockStmt1.EXPECT().NumParams().Return(1, nil).Times(1)
mockConn.EXPECT().Statement().Return(mockStmt1, nil).Times(1)
stmt1, err := conn.PrepareContext(ctx, q)
if err != nil {
t.Fatalf("expected no error from prepareContext but got %v", err)
}
ps, ok := stmt1.(*PreparedStatement)
if !ok {
t.Fatalf("expected a statement to be returnedbut got %v", err)
}
if ps.odbcStatement != mockStmt1 {
t.Errorf("expected statement to be %v but got %v", mockStmt1, ps.odbcStatement)
}
if gotNumInput := ps.NumInput(); gotNumInput != 1 {
t.Errorf("expected num input to be %v but got %v", 1, gotNumInput)
}
//since prepared statement caching is on, the statement shouldn't be closed
stmt1.Close()
//parameters should be reset when the statement is reused
mockStmt1.EXPECT().ResetParams().Times(1)
stmt2, err := conn.PrepareContext(ctx, q)
if err != nil {
t.Fatalf("expected no error from prepareContext but got %v", err)
}
if stmt1 != stmt2 {
t.Fatalf("expected the second statement to be the same but got %v", stmt2)
}
//since prepared statement caching is on, the statement shouldn't be closed
stmt2.Close()
q3 := "SELECT * FROM all"
mockStmt2 := mocks.NewMockStatement(ctrl)
mockStmt2.EXPECT().Prepare(gomock.Any(), q3).Return(nil).Times(1)
mockStmt2.EXPECT().NumParams().Return(1, nil).Times(1)
mockConn.EXPECT().Statement().Return(mockStmt2, nil).Times(1)
//first statement should be evicted from cache when the next statement is created
mockStmt1.EXPECT().Close().Return(nil).Times(1)
stmt3, err := conn.PrepareContext(ctx, q3)
if err != nil {
t.Fatalf("expected no error from prepareContext but got %v", err)
}
if ps, ok := stmt3.(*PreparedStatement); !ok {
t.Fatalf("expected a statement to be returnedbut got %v", err)
} else if ps.odbcStatement != mockStmt2 {
t.Errorf("expected statement to be %v but got %v", mockStmt2, ps.odbcStatement)
}
//first statement should be evicted from cache when the next statement is created
mockStmt2.EXPECT().Close().Return(nil).Times(1)
mockConn.EXPECT().Close().Return(nil).Times(1)
conn.Close()
}
func TestConnection_ExecContext(t *testing.T) {
ctrl, conn, mockConn := testConnection(t, 1)
defer ctrl.Finish()
q := "SELECT * FROM foo WHERE bar = ?"
ctx := context.Background()
mockStmt := mocks.NewMockStatement(ctrl)
mockConn.EXPECT().Statement().Return(mockStmt, nil).Times(1)
mockStmt.EXPECT().BindParams("Value").Return(nil).Times(1)
mockStmt.EXPECT().Close().Return(nil).Times(1)
mockStmt.EXPECT().ExecDirect(gomock.Any(), q).Return(nil).Times(1)
_, err := conn.ExecContext(ctx, q, []driver.NamedValue{
{Name: "", Ordinal: 1, Value: "Value"},
})
if err != nil {
t.Fatalf("expected no error but got %v", err)
}
}
func TestConnection_QueryContext(t *testing.T) {
ctrl, conn, mockConn := testConnection(t, 1)
defer ctrl.Finish()
q := "SELECT * FROM foo WHERE bar = ?"
ctx := context.Background()
mockStmt := mocks.NewMockStatement(ctrl)
mockConn.EXPECT().Statement().Return(mockStmt, nil).Times(1)
mockStmt.EXPECT().BindParams("Value").Return(nil).Times(1)
mockStmt.EXPECT().ExecDirect(gomock.Any(), q).Return(nil).Times(1)
mockRS := mocks.NewMockRecordSet(ctrl)
mockStmt.EXPECT().RecordSet().Return(mockRS, nil).Times(1)
gotRows, err := conn.QueryContext(ctx, q, []driver.NamedValue{
{Name: "", Ordinal: 1, Value: "Value"},
})
if err != nil {
t.Fatalf("expected no error but got %v", err)
}
r, ok := gotRows.(*Rows)
if !ok {
t.Fatalf("unexpected rows result. got %v", gotRows)
}
if r.closeStmtOnRSClose != mockStmt {
t.Errorf("stmt not populated on rows. got %v", gotRows)
}
if r.odbcRecordset != mockRS {
t.Errorf("recordset not populated on Rows. got %v", gotRows)
}
}
func TestConnection_Close_Cache(t *testing.T) {
ctrl, conn, mockConn := testConnection(t, 1)
defer ctrl.Finish()
q := "SELECT * FROM foo WHERE bar = ?"
ctx := context.Background()
mockStmt1 := mocks.NewMockStatement(ctrl)
mockStmt1.EXPECT().Prepare(gomock.Any(), q).Return(nil).Times(1)
mockStmt1.EXPECT().NumParams().Return(1, nil).Times(1)
mockConn.EXPECT().Statement().Return(mockStmt1, nil).Times(1)
stmt1, err := conn.PrepareContext(ctx, q)
if err != nil {
t.Fatalf("expected no error from prepareContext but got %v", err)
}
ps, ok := stmt1.(*PreparedStatement)
if !ok {
t.Fatalf("expected a statement to be returnedbut got %v", err)
}
if ps.odbcStatement != mockStmt1 {
t.Errorf("expected statement to be %v but got %v", mockStmt1, ps.odbcStatement)
}
if gotNumInput := ps.NumInput(); gotNumInput != 1 {
t.Errorf("expected num input to be %v but got %v", 1, gotNumInput)
}
//statement should be closed when the connection is closed
mockStmt1.EXPECT().Close().Return(nil).Times(1)
mockConn.EXPECT().Close().Return(nil).Times(1)
conn.Close()
}
func TestConnection_Close_No_Cache(t *testing.T) {
ctrl, conn, mockConn := testConnection(t, 0)
defer ctrl.Finish()
q := "SELECT * FROM foo WHERE bar = ?"
ctx := context.Background()
mockStmt1 := mocks.NewMockStatement(ctrl)
mockStmt1.EXPECT().Prepare(gomock.Any(), q).Return(nil).Times(1)
mockStmt1.EXPECT().NumParams().Return(1, nil).Times(1)
mockConn.EXPECT().Statement().Return(mockStmt1, nil).Times(1)
stmt1, err := conn.PrepareContext(ctx, q)
if err != nil {
t.Fatalf("expected no error from prepareContext but got %v", err)
}
ps, ok := stmt1.(*PreparedStatement)
if !ok {
t.Fatalf("expected a statement to be returnedbut got %v", err)
}
if ps.odbcStatement != mockStmt1 {
t.Errorf("expected statement to be %v but got %v", mockStmt1, ps.odbcStatement)
}
if gotNumInput := ps.NumInput(); gotNumInput != 1 {
t.Errorf("expected num input to be %v but got %v", 1, gotNumInput)
}
//statement should be closed when the connection is closed
mockStmt1.EXPECT().Close().Return(nil).Times(1)
mockConn.EXPECT().Close().Return(nil).Times(1)
conn.Close()
}
func TestConnection_ResetSession_Cache(t *testing.T) {
ctrl, conn, mockConn := testConnection(t, 1)
defer ctrl.Finish()
q := "SELECT * FROM foo WHERE bar = ?"
ctx := context.Background()
mockStmt1 := mocks.NewMockStatement(ctrl)
mockStmt1.EXPECT().Prepare(gomock.Any(), q).Return(nil).Times(1)
mockStmt1.EXPECT().NumParams().Return(1, nil).Times(1)
mockConn.EXPECT().Statement().Return(mockStmt1, nil).Times(1)
stmt1, err := conn.PrepareContext(ctx, q)
if err != nil {
t.Fatalf("expected no error from prepareContext but got %v", err)
}
ps, ok := stmt1.(*PreparedStatement)
if !ok {
t.Fatalf("expected a statement to be returnedbut got %v", err)
}
if ps.odbcStatement != mockStmt1 {
t.Errorf("expected statement to be %v but got %v", mockStmt1, ps.odbcStatement)
}
if gotNumInput := ps.NumInput(); gotNumInput != 1 {
t.Errorf("expected num input to be %v but got %v", 1, gotNumInput)
}
err = conn.ResetSession(ctx)
if err != nil {
t.Fatalf("expected no error from ResetSession but got %v", err)
}
mockStmt1.EXPECT().ResetParams().Times(1)
stmt2, err := conn.PrepareContext(ctx, q)
if err != nil {
t.Fatalf("expected no error from prepareContext but got %v", err)
}
ps2, ok := stmt2.(*PreparedStatement)
if !ok {
t.Fatalf("expected a statement to be returned but got %v", err)
}
if ps2.odbcStatement != mockStmt1 {
t.Error("expected returned statement to be cached after resetting the session")
}
//statement should be closed when the connection is closed
mockStmt1.EXPECT().Close().Return(nil).Times(1)
mockConn.EXPECT().Close().Return(nil).Times(1)
conn.Close()
}
func TestConnection_ResetSession_No_Cache(t *testing.T) {
ctrl, conn, mockConn := testConnection(t, 0)
defer ctrl.Finish()
q := "SELECT * FROM foo WHERE bar = ?"
ctx := context.Background()
mockStmt1 := mocks.NewMockStatement(ctrl)
mockStmt1.EXPECT().Prepare(gomock.Any(), q).Return(nil).Times(1)
mockStmt1.EXPECT().NumParams().Return(1, nil).Times(1)
mockConn.EXPECT().Statement().Return(mockStmt1, nil).Times(1)
stmt1, err := conn.PrepareContext(ctx, q)
if err != nil {
t.Fatalf("expected no error from prepareContext but got %v", err)
}
ps, ok := stmt1.(*PreparedStatement)
if !ok {
t.Fatalf("expected a statement to be returnedbut got %v", err)
}
if ps.odbcStatement != mockStmt1 {
t.Errorf("expected statement to be %v but got %v", mockStmt1, ps.odbcStatement)
}
if gotNumInput := ps.NumInput(); gotNumInput != 1 {
t.Errorf("expected num input to be %v but got %v", 1, gotNumInput)
}
//statement should be closed when the session is reset if there is no cache
mockStmt1.EXPECT().Close().Return(nil).Times(1)
err = conn.ResetSession(ctx)
if err != nil {
t.Fatalf("expected no error from ResetSession but got %v", err)
}
mockConn.EXPECT().Close().Return(nil).Times(1)
conn.Close()
}