Repository navigation
Expand file tree
/
Copy pathfakedb_test.go
More file actions
94 lines (76 loc) · 2.34 KB
/
Copy pathfakedb_test.go
File metadata and controls
94 lines (76 loc) · 2.34 KB
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
package dbmeta
import (
"database/sql"
"database/sql/driver"
"io"
"sync"
)
// A fake driver, replaying recorded rows. D28 makes this the shape of every
// root module test: the package needs no database driver and no container, and
// it can cover a server version whose container image no longer starts.
//
// database/sql/driver is in the standard library, so this costs no dependency.
// replay is one recorded answer.
type replay struct {
cols []string
rows [][]driver.Value
err error
}
var (
replayMu sync.Mutex
replays = map[string]replay{}
)
// record registers the answer the fake driver gives for a statement.
func record(query string, cols []string, rows [][]driver.Value, err error) {
replayMu.Lock()
defer replayMu.Unlock()
replays[query] = replay{cols: cols, rows: rows, err: err}
}
func init() {
sql.Register("dbmetafake", fakeDriver{})
}
type fakeDriver struct{}
func (fakeDriver) Open(string) (driver.Conn, error) { return fakeConn{}, nil }
type fakeConn struct{}
func (c fakeConn) Prepare(q string) (driver.Stmt, error) { return fakeStmt{q: q}, nil }
func (c fakeConn) Close() error { return nil }
func (c fakeConn) Begin() (driver.Tx, error) { return nil, io.EOF }
type fakeStmt struct{ q string }
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, io.EOF }
func (s fakeStmt) Query(_ []driver.Value) (driver.Rows, error) {
replayMu.Lock()
r, ok := replays[s.q]
replayMu.Unlock()
if !ok {
return nil, io.ErrUnexpectedEOF
}
if r.err != nil {
return nil, r.err
}
return &fakeRows{cols: r.cols, rows: r.rows}, nil
}
type fakeRows struct {
cols []string
rows [][]driver.Value
i int
done bool
}
func (r *fakeRows) Columns() []string { return r.cols }
// Close records that the rows were released, which is how a test checks that
// stopping an iterator early gives the connection back.
func (r *fakeRows) Close() error {
r.done = true
return nil
}
func (r *fakeRows) Next(dest []driver.Value) error {
if r.i >= len(r.rows) {
return io.EOF
}
copy(dest, r.rows[r.i])
r.i++
return nil
}
// openFake returns a handle backed by the fake driver.
func openFake() (*sql.DB, error) { return sql.Open("dbmetafake", "") }