-
Notifications
You must be signed in to change notification settings - Fork 31
/
stmt_test.go
101 lines (79 loc) · 1.98 KB
/
stmt_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
package sqlmw
import (
"context"
"database/sql"
"database/sql/driver"
"testing"
)
type stmtCtxKey string
const (
stmtRowContextKey stmtCtxKey = "rowcontext"
stmtRowContextValue string = "rowvalue"
)
type stmtTestInterceptor struct {
T *testing.T
RowsNextValid bool
RowsCloseValid bool
NullInterceptor
}
func (i *stmtTestInterceptor) StmtQueryContext(ctx context.Context, stmt driver.StmtQueryContext, _ string, args []driver.NamedValue) (context.Context, driver.Rows, error) {
ctx = context.WithValue(ctx, stmtRowContextKey, stmtRowContextValue)
r, err := stmt.QueryContext(ctx, args)
return ctx, r, err
}
func (i *stmtTestInterceptor) RowsNext(ctx context.Context, rows driver.Rows, dest []driver.Value) error {
if ctx.Value(stmtRowContextKey) == stmtRowContextValue {
i.RowsNextValid = true
}
i.T.Log(ctx)
return rows.Next(dest)
}
func (i *stmtTestInterceptor) RowsClose(ctx context.Context, rows driver.Rows) error {
if ctx.Value(stmtRowContextKey) == stmtRowContextValue {
i.RowsCloseValid = true
}
i.T.Log(ctx)
return rows.Close()
}
func TestStmtQueryContext_PassWrappedRowContext(t *testing.T) {
driverName := driverName(t)
con := &fakeConn{}
fakeStmt := &fakeStmt{
rows: &fakeRows{
con: con,
vals: [][]driver.Value{{}},
},
}
con.stmt = fakeStmt
ti := &stmtTestInterceptor{T: t}
sql.Register(
driverName,
Driver(&fakeDriver{conn: con}, ti),
)
db, err := sql.Open(driverName, "")
if err != nil {
t.Fatalf("Failed to open: %v", err)
}
t.Cleanup(func() {
if err := db.Close(); err != nil {
t.Errorf("Failed to close db: %v", err)
}
})
stmt, err := db.PrepareContext(context.Background(), "")
if err != nil {
t.Fatalf("Prepare failed: %s", err)
}
rows, err := stmt.Query("")
if err != nil {
t.Fatalf("Stmt query failed: %s", err)
}
rows.Next()
rows.Close()
stmt.Close()
if !ti.RowsNextValid {
t.Error("RowsNext context not valid")
}
if !ti.RowsCloseValid {
t.Error("RowsClose context not valid")
}
}