/
driver_mock.go
201 lines (179 loc) · 6.13 KB
/
driver_mock.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
package godb
import (
"database/sql/driver"
)
// DriverRowsMock defines a new driver rows mock
// It provides callback functions for each method to allow custom behavior during testing.
type DriverRowsMock struct {
Error error
CallbackColumns func() []string
CallbackClose func() error
CallbackNext func(dest []driver.Value) error
}
// Columns returns the column names of the rows.
// If CallbackColumns is defined, it calls the callback function.
// Otherwise, it returns an empty slice.
func (drm *DriverRowsMock) Columns() []string {
if drm.CallbackColumns != nil {
return drm.CallbackColumns()
}
return []string{}
}
// Close closes the rows.
// If CallbackClose is defined, it calls the callback function.
// Otherwise, it returns the error defined in the struct.
func (drm *DriverRowsMock) Close() error {
if drm.CallbackClose != nil {
return drm.CallbackClose()
}
return drm.Error
}
// Next retrieves the next row of the result set.
// If CallbackNext is defined, it calls the callback function.
// Otherwise, it returns the error defined in the struct.
func (drm *DriverRowsMock) Next(dest []driver.Value) error {
if drm.CallbackNext != nil {
return drm.CallbackNext(dest)
}
return drm.Error
}
// DriverResultMock defines a new driver result mock
type DriverResultMock struct {
Error error
CallbackLastInsertId func() (int64, error)
CallbackRowsAffected func() (int64, error)
}
// LastInsertId returns the last inserted row id.
// If CallbackLastInsertId is defined, it calls the callback function.
// Otherwise, it returns 0 and the error defined in the struct.
func (drm *DriverResultMock) LastInsertId() (int64, error) {
if drm.CallbackLastInsertId != nil {
return drm.CallbackLastInsertId()
}
return 0, drm.Error
}
// RowsAffected returns the number of rows affected by the query.
// If CallbackRowsAffected is defined, it calls the callback function.
// Otherwise, it returns 0 and the error defined in the struct.
func (drm *DriverResultMock) RowsAffected() (int64, error) {
if drm.CallbackRowsAffected != nil {
return drm.CallbackRowsAffected()
}
return 0, drm.Error
}
// DriverTxMock defines a new driver transaction mock
type DriverTxMock struct {
Error error
CallbackCommit func() error
CallbackRollback func() error
}
// Commit commits the transaction.
// If CallbackCommit is defined, it calls the callback function.
// Otherwise, it returns the error defined in the struct.
func (dtxm *DriverTxMock) Commit() error {
if dtxm.CallbackCommit != nil {
return dtxm.CallbackCommit()
}
return dtxm.Error
}
// Rollback rolls back the transaction.
// If CallbackRollback is defined, it calls the callback function.
// Otherwise, it returns the error defined in the struct.
func (dtxm *DriverTxMock) Rollback() error {
if dtxm.CallbackRollback != nil {
return dtxm.CallbackRollback()
}
return dtxm.Error
}
// DriverStmtMock defines a new driver statement mock
type DriverStmtMock struct {
Error error
CallbackClose func() error
CallbackNumInput func() int
CallbackExec func(args []driver.Value) (driver.Result, error)
CallbackQuery func(args []driver.Value) (driver.Rows, error)
}
// Close closes the statement.
// If CallbackClose is defined, it calls the callback function.
// Otherwise, it returns the error defined in the struct.
func (dsm *DriverStmtMock) Close() error {
if dsm.CallbackClose != nil {
return dsm.CallbackClose()
}
return dsm.Error
}
// NumInput returns the number of placeholder parameters in the statement.
// If CallbackNumInput is defined, it calls the callback function.
// Otherwise, it returns 0.
func (dsm *DriverStmtMock) NumInput() int {
if dsm.CallbackNumInput != nil {
return dsm.CallbackNumInput()
}
return 0
}
// Exec executes a query that doesn't return rows.
// If CallbackExec is defined, it calls the callback function.
// Otherwise, it returns a new DriverResultMock and the error defined in the struct.
func (dsm *DriverStmtMock) Exec(args []driver.Value) (driver.Result, error) {
if dsm.CallbackExec != nil {
return dsm.CallbackExec(args)
}
return &DriverResultMock{}, dsm.Error
}
// Query executes a query that may return rows.
// If CallbackQuery is defined, it calls the callback function.
// Otherwise, it returns a new DriverRowsMock and the error defined in the struct.
func (dsm *DriverStmtMock) Query(args []driver.Value) (driver.Rows, error) {
if dsm.CallbackQuery != nil {
return dsm.CallbackQuery(args)
}
return &DriverRowsMock{}, dsm.Error
}
// DriverConnMock defines a new driver connection mock
type DriverConnMock struct {
Error error
CallbackPrepare func(query string) (driver.Stmt, error)
CallbackClose func() error
CallbackBegin func() (driver.Tx, error)
}
// Prepare prepares a statement for execution.
// If CallbackPrepare is defined, it calls the callback function.
// Otherwise, it returns a new DriverStmtMock and the error defined in the struct.
func (dcm *DriverConnMock) Prepare(query string) (driver.Stmt, error) {
if dcm.CallbackPrepare != nil {
return dcm.CallbackPrepare(query)
}
return &DriverStmtMock{}, dcm.Error
}
// Close closes the connection.
// If CallbackClose is defined, it calls the callback function.
// Otherwise, it returns the error defined in the struct.
func (dcm *DriverConnMock) Close() error {
if dcm.CallbackClose != nil {
return dcm.CallbackClose()
}
return dcm.Error
}
// Begin starts a new transaction.
// If CallbackBegin is defined, it calls the callback function.
// Otherwise, it returns a new DriverTxMock and the error defined in the struct.
func (dcm *DriverConnMock) Begin() (driver.Tx, error) {
if dcm.CallbackBegin != nil {
return dcm.CallbackBegin()
}
return &DriverTxMock{}, dcm.Error
}
// DriverMock defines a new driver mock
type DriverMock struct {
Error error
CallbackOpen func(name string) (driver.Conn, error)
}
// Open opens a new connection.
// If CallbackOpen is defined, it calls the callback function.
// Otherwise, it returns a new DriverConnMock and the error defined in the struct.
func (dm *DriverMock) Open(name string) (driver.Conn, error) {
if dm.CallbackOpen != nil {
return dm.CallbackOpen(name)
}
return &DriverConnMock{}, dm.Error
}