-
Notifications
You must be signed in to change notification settings - Fork 31
/
fakedb_test.go
244 lines (195 loc) · 5.9 KB
/
fakedb_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
package sqlmw
import (
"context"
"database/sql/driver"
"fmt"
"io"
"reflect"
)
type fakeDriver struct {
conn driver.Conn
}
func (d *fakeDriver) Open(_ string) (driver.Conn, error) {
return d.conn, nil
}
type fakeTx struct{}
func (f fakeTx) Commit() error { return nil }
func (f fakeTx) Rollback() error { return nil }
type fakeStmt struct {
rows driver.Rows
called bool // nolint:structcheck // ignore unused warning, it is accessed via reflection
}
type fakeStmtWithCheckNamedValue struct {
fakeStmt
}
type fakeStmtWithoutCheckNamedValue struct {
fakeStmt
}
func (s fakeStmt) Close() error {
return nil
}
func (s fakeStmt) NumInput() int {
return 1
}
func (s fakeStmt) Exec(_ []driver.Value) (driver.Result, error) {
return nil, nil
}
func (s fakeStmt) Query(_ []driver.Value) (driver.Rows, error) {
return s.rows, nil
}
func (s fakeStmt) QueryContext(_ context.Context, _ []driver.NamedValue) (driver.Rows, error) {
return s.rows, nil
}
func (s *fakeStmtWithCheckNamedValue) CheckNamedValue(_ *driver.NamedValue) (err error) {
s.called = true
return
}
type fakeRows struct {
con *fakeConn
vals [][]driver.Value
closeCalled bool // nolint:structcheck,unused // ignore unused warning, it is accessed via reflection
nextCalled bool // nolint:structcheck,unused // ignore unused warning, it is accessed via reflection
//These are here so that we can check things have not been called
hasNextResultSetCalled bool // nolint:structcheck,unused // ignore unused warning, it is accessed via reflection
nextResultSetCalled bool // nolint:structcheck,unused // ignore unused warning, it is accessed via reflection
columnTypeDatabaseNameCalled bool // nolint:structcheck,unused // ignore unused warning, it is accessed via reflection
columnTypeLengthCalled bool // nolint:structcheck,unused // ignore unused warning, it is accessed via reflection
columnTypePrecisionScaleCalled bool // nolint:structcheck,unused // ignore unused warning, it is accessed via reflection
columnTypeNullable bool // nolint:structcheck,unused // ignore unused warning, it is accessed via reflection
columnTypeScanTypeCalled bool // nolint:structcheck,unused // ignore unused warning, it is accessed via reflection
}
func (r *fakeRows) Close() error {
r.con.rowsCloseCalled = true
r.closeCalled = true
return nil
}
func (r *fakeRows) Columns() []string {
if len(r.vals) == 0 {
return nil
}
var cols []string
for i := range r.vals[0] {
cols = append(cols, fmt.Sprintf("col%d", i))
}
return cols
}
func (r *fakeRows) Next(dest []driver.Value) error {
r.nextCalled = true
if len(r.vals) == 0 {
return io.EOF
}
copy(dest, r.vals[0])
r.vals = r.vals[1:]
return nil
}
type fakeWithRowsNextResultSet struct {
r *fakeRows
}
func (f *fakeWithRowsNextResultSet) HasNextResultSet() bool {
f.r.hasNextResultSetCalled = true
return false
}
func (f *fakeWithRowsNextResultSet) NextResultSet() error {
f.r.nextResultSetCalled = true
return nil
}
type fakeWithColumnTypeDatabaseName struct {
r *fakeRows
names []string
}
func (f *fakeWithColumnTypeDatabaseName) ColumnTypeDatabaseTypeName(index int) string {
f.r.columnTypeDatabaseNameCalled = true
return f.names[index]
}
type fakeWithColumnTypeLength struct {
r *fakeRows
lengths []int64
bools []bool
}
func (f *fakeWithColumnTypeLength) ColumnTypeLength(index int) (length int64, ok bool) {
f.r.columnTypeLengthCalled = true
return f.lengths[index], f.bools[index]
}
type fakeWithColumnTypePrecisionScale struct {
r *fakeRows
precisions, scales []int64
bools []bool
}
func (f *fakeWithColumnTypePrecisionScale) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) {
f.r.columnTypePrecisionScaleCalled = true
return f.precisions[index], f.scales[index], f.bools[index]
}
type fakeWithColumnTypeNullable struct {
r *fakeRows
nullables []bool
oks []bool
}
func (f *fakeWithColumnTypeNullable) ColumnTypeNullable(index int) (nullable, ok bool) {
f.r.columnTypeNullable = true
return f.nullables[index], f.oks[index]
}
type fakeWithColumnTypeScanType struct {
r *fakeRows
scanTypes []reflect.Type
}
func (f *fakeWithColumnTypeScanType) ColumnTypeScanType(index int) reflect.Type {
f.r.columnTypeScanTypeCalled = true
return f.scanTypes[index]
}
type fakeRowsLikeMysql struct {
fakeRows
fakeWithRowsNextResultSet
fakeWithColumnTypeDatabaseName
fakeWithColumnTypePrecisionScale
fakeWithColumnTypeNullable
fakeWithColumnTypeScanType
}
// The set of interfaces support by pgx and sqlite3
type fakeRowsLikePgx struct {
fakeRows
fakeWithColumnTypeDatabaseName
fakeWithColumnTypeLength
fakeWithColumnTypePrecisionScale
fakeWithColumnTypeNullable
fakeWithColumnTypeScanType
}
type fakeConn struct {
called bool // nolint:structcheck // ignore unused warning, it is accessed via reflection
rowsCloseCalled bool
stmt driver.Stmt
tx driver.Tx
}
type fakeConnWithCheckNamedValue struct {
fakeConn
}
type fakeConnWithoutCheckNamedValue struct {
fakeConn
}
func (c *fakeConn) Prepare(_ string) (driver.Stmt, error) {
return nil, nil
}
func (c *fakeConn) PrepareContext(_ context.Context, _ string) (driver.Stmt, error) {
return c.stmt, nil
}
func (c *fakeConn) ExecContext(_ context.Context, _ string, _ []driver.NamedValue) (driver.Result, error) {
return nil, nil
}
func (c *fakeConn) Close() error { return nil }
func (c *fakeConn) Begin() (driver.Tx, error) { return c.tx, nil }
func (c *fakeConn) QueryContext(_ context.Context, _ string, nvs []driver.NamedValue) (driver.Rows, error) {
if c.stmt == nil {
return &fakeRows{con: c}, nil
}
var args []driver.Value
for _, nv := range nvs {
args = append(args, nv.Value)
}
return c.stmt.Query(args)
}
func (c *fakeConnWithCheckNamedValue) CheckNamedValue(_ *driver.NamedValue) (err error) {
c.called = true
return
}
type fakeInterceptor struct {
NullInterceptor
}