-
Notifications
You must be signed in to change notification settings - Fork 27
/
fakedb_test.go
405 lines (339 loc) · 8.95 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
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
package proxy
import (
"bytes"
"context"
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"io"
"sync"
)
type fakeConnOption struct {
// name is the name of database
Name string
// ConnType enhances the driver implementation
ConnType string
// call of Exec will fail if failExec is true
FailExec bool
// call of Query will fail if failQuery is true
FailQuery bool
}
type fakeDriver struct {
mu sync.Mutex
dbs map[string]*fakeDB
}
type fakeDB struct {
mu sync.Mutex
log *bytes.Buffer
}
// fakeConn is minimum implementation of driver.Conn
type fakeConn struct {
db *fakeDB
opt *fakeConnOption
}
// fakeConnExt implements Execer and Queryer
type fakeConnExt fakeConn
// fakeConnCtx is fakeConn with context
type fakeConnCtx fakeConn
type fakeTx struct {
db *fakeDB
opt *fakeConnOption
}
// fakeStmt is minimum implementation of driver.Stmt
type fakeStmt struct {
db *fakeDB
opt *fakeConnOption
}
// fakeStmtExt implements ColumnConverter
type fakeStmtExt fakeStmt
// fakeStmtCtx is fakeStmt with context
type fakeStmtCtx fakeStmt
type fakeRows struct{}
var fdriver = &fakeDriver{}
var _ driver.Driver = &fakeDriver{}
var _ driver.Conn = &fakeConn{}
var _ driver.Conn = &fakeConnExt{}
var _ driver.Execer = &fakeConnExt{}
var _ driver.Queryer = &fakeConnExt{}
var _ driver.Conn = &fakeConnCtx{}
var _ driver.Execer = &fakeConnCtx{}
var _ driver.ExecerContext = &fakeConnCtx{}
var _ driver.Queryer = &fakeConnCtx{}
var _ driver.QueryerContext = &fakeConnCtx{}
var _ driver.ConnBeginTx = &fakeConnCtx{}
var _ driver.ConnPrepareContext = &fakeConnCtx{}
var _ driver.Pinger = &fakeConnCtx{}
var _ driver.Tx = &fakeTx{}
var _ driver.Stmt = &fakeStmt{}
var _ driver.Stmt = &fakeStmtExt{}
var _ driver.ColumnConverter = &fakeStmtExt{}
var _ driver.Stmt = &fakeStmtCtx{}
var _ driver.ColumnConverter = &fakeStmtCtx{}
var _ driver.StmtExecContext = &fakeStmtCtx{}
var _ driver.StmtQueryContext = &fakeStmtCtx{}
var _ driver.Rows = &fakeRows{}
func init() {
sql.Register("fakedb", fdriver)
}
func (d *fakeDriver) Open(name string) (driver.Conn, error) {
d.mu.Lock()
defer d.mu.Unlock()
var opt fakeConnOption
err := json.Unmarshal([]byte(name), &opt)
if err != nil {
return nil, err
}
db, ok := d.dbs[opt.Name]
if !ok {
db = &fakeDB{
log: &bytes.Buffer{},
}
if d.dbs == nil {
d.dbs = make(map[string]*fakeDB)
}
d.dbs[name] = db
}
var conn driver.Conn
switch opt.ConnType {
case "", "fakeConn":
conn = &fakeConn{
db: db,
opt: &opt,
}
case "fakeConnExt":
conn = &fakeConnExt{
db: db,
opt: &opt,
}
case "fakeConnCtx":
conn = &fakeConnCtx{
db: db,
opt: &opt,
}
default:
return nil, errors.New("known ConnType")
}
return conn, nil
}
func (d *fakeDriver) DB(name string) *fakeDB {
d.mu.Lock()
defer d.mu.Unlock()
return d.dbs[name]
}
// Log write the params to the log.
func (db *fakeDB) Log(params ...interface{}) {
db.mu.Lock()
defer db.mu.Unlock()
fmt.Fprintln(db.log, params...)
}
// LogToString converts the log into string.
func (db *fakeDB) LogToString() string {
db.mu.Lock()
defer db.mu.Unlock()
return db.log.String()
}
func (c *fakeConn) Prepare(query string) (driver.Stmt, error) {
c.db.Log("[Conn.Prepare]", query)
return &fakeStmt{
db: c.db,
opt: c.opt,
}, nil
}
func (c *fakeConn) Close() error {
return nil
}
func (c *fakeConn) Begin() (driver.Tx, error) {
c.db.Log("[Conn.Begin]")
return &fakeTx{
db: c.db,
opt: c.opt,
}, nil
}
func (c *fakeConnExt) Prepare(query string) (driver.Stmt, error) {
c.db.Log("[Conn.Prepare]", query)
return &fakeStmtExt{
db: c.db,
opt: c.opt,
}, nil
}
func (c *fakeConnExt) Close() error {
return nil
}
func (c *fakeConnExt) Begin() (driver.Tx, error) {
c.db.Log("[Conn.Begin]")
return &fakeTx{
db: c.db,
opt: c.opt,
}, nil
}
func (c *fakeConnExt) Exec(query string, args []driver.Value) (driver.Result, error) {
c.db.Log("[Conn.Exec]", query, convertValuesToString(args))
if c.opt.FailExec {
c.db.Log("[Conn.Exec]", "ERROR!")
return nil, errors.New("Exec failed")
}
return nil, nil
}
func (c *fakeConnExt) Query(query string, args []driver.Value) (driver.Rows, error) {
c.db.Log("[Conn.Query]", query, convertValuesToString(args))
if c.opt.FailQuery {
c.db.Log("[Conn.Query]", "ERROR!")
return nil, errors.New("Query failed")
}
return &fakeRows{}, nil
}
func (c *fakeConnCtx) Ping(ctx context.Context) error {
c.db.Log("[Conn.Ping]")
return nil
}
func (c *fakeConnCtx) Prepare(query string) (driver.Stmt, error) {
panic("not supported")
}
func (c *fakeConnCtx) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
c.db.Log("[Conn.PrepareContext]", query)
return &fakeStmtCtx{
db: c.db,
opt: c.opt,
}, nil
}
func (c *fakeConnCtx) Close() error {
return nil
}
func (c *fakeConnCtx) Begin() (driver.Tx, error) {
panic("not supported")
}
func (c *fakeConnCtx) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
c.db.Log("[Conn.BeginTx]")
return &fakeTx{
db: c.db,
opt: c.opt,
}, nil
}
func (c *fakeConnCtx) Exec(query string, args []driver.Value) (driver.Result, error) {
panic("not supported")
}
func (c *fakeConnCtx) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
c.db.Log("[Conn.ExecContext]", query, convertNamedValuesToString(args))
if c.opt.FailExec {
c.db.Log("[Conn.ExecContext]", "ERROR!")
return nil, errors.New("Exec failed")
}
return nil, nil
}
func (c *fakeConnCtx) Query(query string, args []driver.Value) (driver.Rows, error) {
panic("not supported")
}
func (c *fakeConnCtx) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
c.db.Log("[Conn.QueryContext]", query, convertNamedValuesToString(args))
if c.opt.FailQuery {
c.db.Log("[Conn.QueryContext]", "ERROR!")
return nil, errors.New("Query failed")
}
return &fakeRows{}, nil
}
func (tx *fakeTx) Commit() error {
tx.db.Log("[Tx.Commit]")
return nil
}
func (tx *fakeTx) Rollback() error {
tx.db.Log("[Tx.Rollback]")
return nil
}
func (stmt *fakeStmt) Close() error {
stmt.db.Log("[Stmt.Close]")
return nil
}
func (stmt *fakeStmt) NumInput() int {
return -1 // fakeDriver doesn't know its number of placeholders
}
func (stmt *fakeStmt) Exec(args []driver.Value) (driver.Result, error) {
stmt.db.Log("[Stmt.Exec]", convertValuesToString(args))
if stmt.opt.FailExec {
stmt.db.Log("[Stmt.Exec]", "ERROR!")
return nil, errors.New("Exec failed")
}
return nil, nil
}
func (stmt *fakeStmt) Query(args []driver.Value) (driver.Rows, error) {
stmt.db.Log("[Stmt.Query]", convertValuesToString(args))
if stmt.opt.FailQuery {
stmt.db.Log("[Stmt.Query]", "ERROR!")
return nil, errors.New("Query failed")
}
return &fakeRows{}, nil
}
func (stmt *fakeStmtExt) Close() error {
stmt.db.Log("[Stmt.Close]")
return nil
}
func (stmt *fakeStmtExt) NumInput() int {
return -1 // fakeDriver doesn't know its number of placeholders
}
func (stmt *fakeStmtExt) Exec(args []driver.Value) (driver.Result, error) {
return (*fakeStmt)(stmt).Exec(args)
}
func (stmt *fakeStmtExt) Query(args []driver.Value) (driver.Rows, error) {
return (*fakeStmt)(stmt).Query(args)
}
func (stmt *fakeStmtExt) ColumnConverter(idx int) driver.ValueConverter {
stmt.db.Log("[Stmt.ColumnConverter]", idx)
return driver.DefaultParameterConverter
}
func (stmt *fakeStmtCtx) Close() error {
stmt.db.Log("[Stmt.Close]")
return nil
}
func (stmt *fakeStmtCtx) NumInput() int {
return -1 // fakeDriver doesn't know its number of placeholders
}
func (stmt *fakeStmtCtx) Exec(args []driver.Value) (driver.Result, error) {
panic("not supported")
}
func (stmt *fakeStmtCtx) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
stmt.db.Log("[Conn.ExecContext]", convertNamedValuesToString(args))
if stmt.opt.FailExec {
stmt.db.Log("[Conn.ExecContext]", "ERROR!")
return nil, errors.New("Exec failed")
}
return nil, nil
}
func (stmt *fakeStmtCtx) Query(args []driver.Value) (driver.Rows, error) {
panic("not supported")
}
func (stmt *fakeStmtCtx) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
stmt.db.Log("[Conn.QueryContext]", convertNamedValuesToString(args))
if stmt.opt.FailQuery {
stmt.db.Log("[Conn.QueryContext]", "ERROR!")
return nil, errors.New("Query failed")
}
return &fakeRows{}, nil
}
func (stmt *fakeStmtCtx) ColumnConverter(idx int) driver.ValueConverter {
stmt.db.Log("[Stmt.ColumnConverter]", idx)
return driver.DefaultParameterConverter
}
func (rows *fakeRows) Columns() []string {
return []string{"id"}
}
func (rows *fakeRows) Close() error {
return nil
}
func (rows *fakeRows) Next(dest []driver.Value) error {
return io.EOF
}
func convertValuesToString(args []driver.Value) string {
buf := new(bytes.Buffer)
for _, arg := range args {
fmt.Fprintf(buf, " %#v", arg)
}
return buf.String()
}
func convertNamedValuesToString(args []driver.NamedValue) string {
buf := new(bytes.Buffer)
for _, arg := range args {
fmt.Fprintf(buf, " %#v", arg)
}
return buf.String()
}