@@ -3,10 +3,10 @@ package sqlmock
3
3
import (
4
4
"database/sql/driver"
5
5
"fmt"
6
- "reflect"
7
6
"regexp"
8
7
"strings"
9
8
"sync"
9
+ "time"
10
10
)
11
11
12
12
// an expectation interface
@@ -54,6 +54,7 @@ func (e *ExpectedClose) String() string {
54
54
// returned by *Sqlmock.ExpectBegin.
55
55
type ExpectedBegin struct {
56
56
commonExpectation
57
+ delay time.Duration
57
58
}
58
59
59
60
// WillReturnError allows to set an error for *sql.DB.Begin action
@@ -71,6 +72,13 @@ func (e *ExpectedBegin) String() string {
71
72
return msg
72
73
}
73
74
75
+ // WillDelayFor allows to specify duration for which it will delay
76
+ // result. May be used together with Context
77
+ func (e * ExpectedBegin ) WillDelayFor (duration time.Duration ) * ExpectedBegin {
78
+ e .delay = duration
79
+ return e
80
+ }
81
+
74
82
// ExpectedCommit is used to manage *sql.Tx.Commit expectation
75
83
// returned by *Sqlmock.ExpectCommit.
76
84
type ExpectedCommit struct {
@@ -118,7 +126,8 @@ func (e *ExpectedRollback) String() string {
118
126
// Returned by *Sqlmock.ExpectQuery.
119
127
type ExpectedQuery struct {
120
128
queryBasedExpectation
121
- rows driver.Rows
129
+ rows driver.Rows
130
+ delay time.Duration
122
131
}
123
132
124
133
// WithArgs will match given expected args to actual database query arguments.
@@ -135,10 +144,10 @@ func (e *ExpectedQuery) WillReturnError(err error) *ExpectedQuery {
135
144
return e
136
145
}
137
146
138
- // WillReturnRows specifies the set of resulting rows that will be returned
139
- // by the triggered query
140
- func (e * ExpectedQuery ) WillReturnRows ( rows driver. Rows ) * ExpectedQuery {
141
- e .rows = rows
147
+ // WillDelayFor allows to specify duration for which it will delay
148
+ // result. May be used together with Context
149
+ func (e * ExpectedQuery ) WillDelayFor ( duration time. Duration ) * ExpectedQuery {
150
+ e .delay = duration
142
151
return e
143
152
}
144
153
@@ -158,12 +167,7 @@ func (e *ExpectedQuery) String() string {
158
167
}
159
168
160
169
if e .rows != nil {
161
- msg += "\n - should return rows:\n "
162
- rs , _ := e .rows .(* rows )
163
- for i , row := range rs .rows {
164
- msg += fmt .Sprintf (" %d - %+v\n " , i , row )
165
- }
166
- msg = strings .TrimSpace (msg )
170
+ msg += fmt .Sprintf ("\n - %s" , e .rows )
167
171
}
168
172
169
173
if e .err != nil {
@@ -178,6 +182,7 @@ func (e *ExpectedQuery) String() string {
178
182
type ExpectedExec struct {
179
183
queryBasedExpectation
180
184
result driver.Result
185
+ delay time.Duration
181
186
}
182
187
183
188
// WithArgs will match given expected args to actual database exec operation arguments.
@@ -194,6 +199,13 @@ func (e *ExpectedExec) WillReturnError(err error) *ExpectedExec {
194
199
return e
195
200
}
196
201
202
+ // WillDelayFor allows to specify duration for which it will delay
203
+ // result. May be used together with Context
204
+ func (e * ExpectedExec ) WillDelayFor (duration time.Duration ) * ExpectedExec {
205
+ e .delay = duration
206
+ return e
207
+ }
208
+
197
209
// String returns string representation
198
210
func (e * ExpectedExec ) String () string {
199
211
msg := "ExpectedExec => expecting Exec which:"
@@ -244,6 +256,7 @@ type ExpectedPrepare struct {
244
256
sqlRegex * regexp.Regexp
245
257
statement driver.Stmt
246
258
closeErr error
259
+ delay time.Duration
247
260
}
248
261
249
262
// WillReturnError allows to set an error for the expected *sql.DB.Prepare or *sql.Tx.Prepare action.
@@ -258,6 +271,13 @@ func (e *ExpectedPrepare) WillReturnCloseError(err error) *ExpectedPrepare {
258
271
return e
259
272
}
260
273
274
+ // WillDelayFor allows to specify duration for which it will delay
275
+ // result. May be used together with Context
276
+ func (e * ExpectedPrepare ) WillDelayFor (duration time.Duration ) * ExpectedPrepare {
277
+ e .delay = duration
278
+ return e
279
+ }
280
+
261
281
// ExpectQuery allows to expect Query() or QueryRow() on this prepared statement.
262
282
// this method is convenient in order to prevent duplicating sql query string matching.
263
283
func (e * ExpectedPrepare ) ExpectQuery () * ExpectedQuery {
@@ -300,7 +320,7 @@ type queryBasedExpectation struct {
300
320
args []driver.Value
301
321
}
302
322
303
- func (e * queryBasedExpectation ) attemptMatch (sql string , args []driver. Value ) (err error ) {
323
+ func (e * queryBasedExpectation ) attemptMatch (sql string , args []namedValue ) (err error ) {
304
324
if ! e .queryMatches (sql ) {
305
325
return fmt .Errorf (`could not match sql: "%s" with expected regexp "%s"` , sql , e .sqlRegex .String ())
306
326
}
@@ -322,37 +342,3 @@ func (e *queryBasedExpectation) attemptMatch(sql string, args []driver.Value) (e
322
342
func (e * queryBasedExpectation ) queryMatches (sql string ) bool {
323
343
return e .sqlRegex .MatchString (sql )
324
344
}
325
-
326
- func (e * queryBasedExpectation ) argsMatches (args []driver.Value ) error {
327
- if nil == e .args {
328
- return nil
329
- }
330
- if len (args ) != len (e .args ) {
331
- return fmt .Errorf ("expected %d, but got %d arguments" , len (e .args ), len (args ))
332
- }
333
- for k , v := range args {
334
- // custom argument matcher
335
- matcher , ok := e .args [k ].(Argument )
336
- if ok {
337
- if ! matcher .Match (v ) {
338
- return fmt .Errorf ("matcher %T could not match %d argument %T - %+v" , matcher , k , args [k ], args [k ])
339
- }
340
- continue
341
- }
342
-
343
- // convert to driver converter
344
- darg , err := driver .DefaultParameterConverter .ConvertValue (e .args [k ])
345
- if err != nil {
346
- return fmt .Errorf ("could not convert %d argument %T - %+v to driver value: %s" , k , e .args [k ], e .args [k ], err )
347
- }
348
-
349
- if ! driver .IsValue (darg ) {
350
- return fmt .Errorf ("argument %d: non-subset type %T returned from Value" , k , darg )
351
- }
352
-
353
- if ! reflect .DeepEqual (darg , args [k ]) {
354
- return fmt .Errorf ("argument %d expected [%T - %+v] does not match actual [%T - %+v]" , k , darg , darg , args [k ], args [k ])
355
- }
356
- }
357
- return nil
358
- }
0 commit comments