@@ -3,10 +3,10 @@ package sqlmock
33import (
44 "database/sql/driver"
55 "fmt"
6- "reflect"
76 "regexp"
87 "strings"
98 "sync"
9+ "time"
1010)
1111
1212// an expectation interface
@@ -54,6 +54,7 @@ func (e *ExpectedClose) String() string {
5454// returned by *Sqlmock.ExpectBegin.
5555type ExpectedBegin struct {
5656 commonExpectation
57+ delay time.Duration
5758}
5859
5960// WillReturnError allows to set an error for *sql.DB.Begin action
@@ -71,6 +72,13 @@ func (e *ExpectedBegin) String() string {
7172 return msg
7273}
7374
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+
7482// ExpectedCommit is used to manage *sql.Tx.Commit expectation
7583// returned by *Sqlmock.ExpectCommit.
7684type ExpectedCommit struct {
@@ -118,7 +126,8 @@ func (e *ExpectedRollback) String() string {
118126// Returned by *Sqlmock.ExpectQuery.
119127type ExpectedQuery struct {
120128 queryBasedExpectation
121- rows driver.Rows
129+ rows driver.Rows
130+ delay time.Duration
122131}
123132
124133// WithArgs will match given expected args to actual database query arguments.
@@ -135,10 +144,10 @@ func (e *ExpectedQuery) WillReturnError(err error) *ExpectedQuery {
135144 return e
136145}
137146
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
142151 return e
143152}
144153
@@ -158,12 +167,7 @@ func (e *ExpectedQuery) String() string {
158167 }
159168
160169 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 )
167171 }
168172
169173 if e .err != nil {
@@ -178,6 +182,7 @@ func (e *ExpectedQuery) String() string {
178182type ExpectedExec struct {
179183 queryBasedExpectation
180184 result driver.Result
185+ delay time.Duration
181186}
182187
183188// WithArgs will match given expected args to actual database exec operation arguments.
@@ -194,6 +199,13 @@ func (e *ExpectedExec) WillReturnError(err error) *ExpectedExec {
194199 return e
195200}
196201
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+
197209// String returns string representation
198210func (e * ExpectedExec ) String () string {
199211 msg := "ExpectedExec => expecting Exec which:"
@@ -244,6 +256,7 @@ type ExpectedPrepare struct {
244256 sqlRegex * regexp.Regexp
245257 statement driver.Stmt
246258 closeErr error
259+ delay time.Duration
247260}
248261
249262// 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 {
258271 return e
259272}
260273
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+
261281// ExpectQuery allows to expect Query() or QueryRow() on this prepared statement.
262282// this method is convenient in order to prevent duplicating sql query string matching.
263283func (e * ExpectedPrepare ) ExpectQuery () * ExpectedQuery {
@@ -300,7 +320,7 @@ type queryBasedExpectation struct {
300320 args []driver.Value
301321}
302322
303- func (e * queryBasedExpectation ) attemptMatch (sql string , args []driver. Value ) (err error ) {
323+ func (e * queryBasedExpectation ) attemptMatch (sql string , args []namedValue ) (err error ) {
304324 if ! e .queryMatches (sql ) {
305325 return fmt .Errorf (`could not match sql: "%s" with expected regexp "%s"` , sql , e .sqlRegex .String ())
306326 }
@@ -322,37 +342,3 @@ func (e *queryBasedExpectation) attemptMatch(sql string, args []driver.Value) (e
322342func (e * queryBasedExpectation ) queryMatches (sql string ) bool {
323343 return e .sqlRegex .MatchString (sql )
324344}
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