-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver_test.go
164 lines (132 loc) · 6.08 KB
/
driver_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
package dbwrap
import (
"context"
"database/sql/driver"
"errors"
"io"
"reflect"
"testing"
)
var errDummy = errors.New("dummy")
type stubRows struct{}
func (stubRows) Columns() []string { return []string{"dummy"} }
func (stubRows) Close() error { return errDummy }
func (stubRows) Next([]driver.Value) error { return errDummy }
func (stubRows) HasNextResultSet() bool { return true }
func (stubRows) NextResultSet() error { return errDummy }
func (stubRows) ColumnTypeScanType(int) reflect.Type { return reflect.TypeOf(stubRows{}) }
func (stubRows) ColumnTypeDatabaseTypeName(index int) string { return "dummy" }
func (stubRows) ColumnTypeLength(index int) (length int64, ok bool) { return 1, true }
func (stubRows) ColumnTypeNullable(index int) (nullable, ok bool) { return true, true }
func (stubRows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) {
return 1, 1, true
}
func TestWrappingTransparency(t *testing.T) {
var (
ctx = context.Background()
oRows = &stubRows{}
options, _ = prepareOptions([]Option{
WithInterceptor(func(ctx context.Context, operation Operation, statement string, args []driver.NamedValue) (context.Context, string, []driver.NamedValue) {
return ctx, statement, args
}),
WithMiddleware(func(ctx context.Context, operation Operation, statement string, args []driver.NamedValue) (nCtx context.Context, onFinish func(error)) {
return ctx, nil
}),
})
wRows = wrapRows(ctx, oRows, options)
)
if want, have := oRows.Columns(), wRows.Columns(); len(want) != len(have) {
t.Errorf("rows.Column want: %v, have: %v", want, have)
}
if want, have := oRows.Close(), wRows.Close(); want != have {
t.Errorf("rows.Close want: %v, have: %v", want, have)
}
if want, have := oRows.Next(nil), wRows.Next(nil); want != have {
t.Errorf("rows.Next want: %v, have: %v", want, have)
}
if want, have := oRows.HasNextResultSet(), wRows.(driver.RowsNextResultSet).HasNextResultSet(); want != have {
t.Errorf("rows.HasNextResultSet want: %t, have: %t", want, have)
}
if want, have := oRows.NextResultSet(), wRows.(driver.RowsNextResultSet).NextResultSet(); want != have {
t.Errorf("rows.NextResultSet want: %v, have: %v", want, have)
}
if want, have := oRows.ColumnTypeScanType(1), wRows.(driver.RowsColumnTypeScanType).ColumnTypeScanType(1); want != have {
t.Errorf("rows.ColumnTypeScanType want: %v, have: %v", want, have)
}
if want, have := oRows.ColumnTypeDatabaseTypeName(1),
wRows.(driver.RowsColumnTypeDatabaseTypeName).ColumnTypeDatabaseTypeName(1); want != have {
t.Errorf("rows.ColumnTypeDatabaseTypeName want: %s, have: %s", want, have)
}
oLength, oOk := oRows.ColumnTypeLength(1)
wLength, wOk := wRows.(driver.RowsColumnTypeLength).ColumnTypeLength(1)
if oLength != wLength || oOk != wOk {
t.Errorf("rows.ColumnTypeLength want: %d:%t, have %d:%t", oLength, oOk, wLength, wOk)
}
oNullable, oOk := oRows.ColumnTypeNullable(1)
wNullable, wOk := wRows.(driver.RowsColumnTypeNullable).ColumnTypeNullable(1)
if oNullable != wNullable || oOk != wOk {
t.Errorf("rows.ColumnTypeNullable want: %t:%t, have %t:%t", oNullable, oOk, wNullable, wOk)
}
oPrecision, oScale, oOk := oRows.ColumnTypePrecisionScale(1)
wPrecision, wScale, wOk := wRows.(driver.RowsColumnTypePrecisionScale).ColumnTypePrecisionScale(1)
if oPrecision != wPrecision || oScale != wScale || oOk != wOk {
t.Errorf("rows.ColumnTypePrecisionScale want: %d:%d:%t, have %d:%d:%t",
oPrecision, oScale, oOk, wPrecision, wScale, wOk)
}
}
func TestWrappingFallback(t *testing.T) {
var (
ctx = context.Background()
oRows = struct{ driver.Rows }{&stubRows{}}
options, _ = prepareOptions([]Option{
WithInterceptor(func(ctx context.Context, operation Operation, statement string,
args []driver.NamedValue,
) (context.Context, string, []driver.NamedValue) {
return ctx, statement, args
}),
WithMiddleware(func(ctx context.Context, operation Operation, statement string,
args []driver.NamedValue,
) (nCtx context.Context, onFinish func(error)) {
return ctx, nil
}),
})
wRows = wrapRows(ctx, oRows, options)
)
if want, have := oRows.Columns(), wRows.Columns(); len(want) != len(have) {
t.Errorf("rows.Column want: %v, have: %v", want, have)
}
if want, have := oRows.Close(), wRows.Close(); want != have {
t.Errorf("rows.Close want: %v, have: %v", want, have)
}
if want, have := oRows.Next(nil), wRows.Next(nil); want != have {
t.Errorf("rows.Next want: %v, have: %v", want, have)
}
if want, have := false, wRows.(driver.RowsNextResultSet).HasNextResultSet(); want != have {
t.Errorf("rows.HasNextResultSet want: %t, have: %t", want, have)
}
if want, have := io.EOF, wRows.(driver.RowsNextResultSet).NextResultSet(); want != have {
t.Errorf("rows.NextResultSet want: %v, have: %v", want, have)
}
if _, ok := wRows.(driver.RowsColumnTypeScanType); ok {
t.Error("rows.ColumnTypeScanType unexpected interface implementation found")
}
if want, have := "", wRows.(driver.RowsColumnTypeDatabaseTypeName).ColumnTypeDatabaseTypeName(1); want != have {
t.Errorf("rows.ColumnTypeDatabaseTypeName want: %s, have: %s", want, have)
}
oLength, oOk := int64(0), false
wLength, wOk := wRows.(driver.RowsColumnTypeLength).ColumnTypeLength(1)
if oLength != wLength || oOk != wOk {
t.Errorf("rows.ColumnTypeLength want: %d:%t, have %d:%t", oLength, oOk, wLength, wOk)
}
oNullable, oOk := false, false
wNullable, wOk := wRows.(driver.RowsColumnTypeNullable).ColumnTypeNullable(1)
if oNullable != wNullable || oOk != wOk {
t.Errorf("rows.ColumnTypeNullable want: %t:%t, have %t:%t", oNullable, oOk, wNullable, wOk)
}
oPrecision, oScale, oOk := int64(0), int64(0), false
wPrecision, wScale, wOk := wRows.(driver.RowsColumnTypePrecisionScale).ColumnTypePrecisionScale(1)
if oPrecision != wPrecision || oScale != wScale || oOk != wOk {
t.Errorf("rows.ColumnTypePrecisionScale want: %d:%d:%t, have %d:%d:%t",
oPrecision, oScale, oOk, wPrecision, wScale, wOk)
}
}