2014-02-05 17:21:07 +03:00
|
|
|
package sqlmock
|
|
|
|
|
|
|
|
import (
|
2014-05-29 17:11:13 +03:00
|
|
|
"database/sql/driver"
|
2015-08-26 15:59:28 +02:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2015-08-26 13:28:01 +02:00
|
|
|
"sync"
|
2017-02-06 14:38:57 +02:00
|
|
|
"time"
|
2014-02-05 17:21:07 +03:00
|
|
|
)
|
|
|
|
|
2014-02-07 09:58:27 +03:00
|
|
|
// an expectation interface
|
2014-02-05 17:21:07 +03:00
|
|
|
type expectation interface {
|
2014-05-29 17:11:13 +03:00
|
|
|
fulfilled() bool
|
2015-08-26 13:28:01 +02:00
|
|
|
Lock()
|
|
|
|
Unlock()
|
2015-08-26 15:59:28 +02:00
|
|
|
String() string
|
2014-02-05 17:21:07 +03:00
|
|
|
}
|
|
|
|
|
2014-02-07 09:58:27 +03:00
|
|
|
// common expectation struct
|
|
|
|
// satisfies the expectation interface
|
2014-02-05 17:21:07 +03:00
|
|
|
type commonExpectation struct {
|
2015-08-26 13:28:01 +02:00
|
|
|
sync.Mutex
|
2014-05-29 17:11:13 +03:00
|
|
|
triggered bool
|
|
|
|
err error
|
2014-02-05 17:21:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *commonExpectation) fulfilled() bool {
|
2014-05-29 17:11:13 +03:00
|
|
|
return e.triggered
|
2014-02-05 17:21:07 +03:00
|
|
|
}
|
|
|
|
|
2015-07-17 12:14:30 +02:00
|
|
|
// ExpectedClose is used to manage *sql.DB.Close expectation
|
|
|
|
// returned by *Sqlmock.ExpectClose.
|
|
|
|
type ExpectedClose struct {
|
|
|
|
commonExpectation
|
|
|
|
}
|
|
|
|
|
|
|
|
// WillReturnError allows to set an error for *sql.DB.Close action
|
|
|
|
func (e *ExpectedClose) WillReturnError(err error) *ExpectedClose {
|
|
|
|
e.err = err
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2015-08-26 15:59:28 +02:00
|
|
|
// String returns string representation
|
|
|
|
func (e *ExpectedClose) String() string {
|
|
|
|
msg := "ExpectedClose => expecting database Close"
|
|
|
|
if e.err != nil {
|
|
|
|
msg += fmt.Sprintf(", which should return error: %s", e.err)
|
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2015-07-17 12:14:30 +02:00
|
|
|
// ExpectedBegin is used to manage *sql.DB.Begin expectation
|
|
|
|
// returned by *Sqlmock.ExpectBegin.
|
|
|
|
type ExpectedBegin struct {
|
|
|
|
commonExpectation
|
2017-02-06 14:38:57 +02:00
|
|
|
delay time.Duration
|
2015-07-17 12:14:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// WillReturnError allows to set an error for *sql.DB.Begin action
|
|
|
|
func (e *ExpectedBegin) WillReturnError(err error) *ExpectedBegin {
|
|
|
|
e.err = err
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2015-08-26 15:59:28 +02:00
|
|
|
// String returns string representation
|
|
|
|
func (e *ExpectedBegin) String() string {
|
|
|
|
msg := "ExpectedBegin => expecting database transaction Begin"
|
|
|
|
if e.err != nil {
|
|
|
|
msg += fmt.Sprintf(", which should return error: %s", e.err)
|
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2017-02-06 14:38:57 +02:00
|
|
|
// WillDelayFor allows to specify duration for which it will delay
|
|
|
|
// result. May be used together with Context
|
|
|
|
func (e *ExpectedBegin) WillDelayFor(duration time.Duration) *ExpectedBegin {
|
|
|
|
e.delay = duration
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2015-07-17 12:14:30 +02:00
|
|
|
// ExpectedCommit is used to manage *sql.Tx.Commit expectation
|
|
|
|
// returned by *Sqlmock.ExpectCommit.
|
|
|
|
type ExpectedCommit struct {
|
|
|
|
commonExpectation
|
|
|
|
}
|
|
|
|
|
|
|
|
// WillReturnError allows to set an error for *sql.Tx.Close action
|
|
|
|
func (e *ExpectedCommit) WillReturnError(err error) *ExpectedCommit {
|
|
|
|
e.err = err
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2015-08-26 15:59:28 +02:00
|
|
|
// String returns string representation
|
|
|
|
func (e *ExpectedCommit) String() string {
|
|
|
|
msg := "ExpectedCommit => expecting transaction Commit"
|
|
|
|
if e.err != nil {
|
|
|
|
msg += fmt.Sprintf(", which should return error: %s", e.err)
|
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2015-07-17 12:14:30 +02:00
|
|
|
// ExpectedRollback is used to manage *sql.Tx.Rollback expectation
|
|
|
|
// returned by *Sqlmock.ExpectRollback.
|
|
|
|
type ExpectedRollback struct {
|
|
|
|
commonExpectation
|
|
|
|
}
|
|
|
|
|
|
|
|
// WillReturnError allows to set an error for *sql.Tx.Rollback action
|
|
|
|
func (e *ExpectedRollback) WillReturnError(err error) *ExpectedRollback {
|
|
|
|
e.err = err
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2015-08-26 15:59:28 +02:00
|
|
|
// String returns string representation
|
|
|
|
func (e *ExpectedRollback) String() string {
|
|
|
|
msg := "ExpectedRollback => expecting transaction Rollback"
|
|
|
|
if e.err != nil {
|
|
|
|
msg += fmt.Sprintf(", which should return error: %s", e.err)
|
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2015-07-17 12:14:30 +02:00
|
|
|
// ExpectedQuery is used to manage *sql.DB.Query, *dql.DB.QueryRow, *sql.Tx.Query,
|
|
|
|
// *sql.Tx.QueryRow, *sql.Stmt.Query or *sql.Stmt.QueryRow expectations.
|
|
|
|
// Returned by *Sqlmock.ExpectQuery.
|
|
|
|
type ExpectedQuery struct {
|
|
|
|
queryBasedExpectation
|
2018-12-11 17:38:12 +02:00
|
|
|
rows driver.Rows
|
|
|
|
delay time.Duration
|
|
|
|
rowsMustBeClosed bool
|
|
|
|
rowsWereClosed bool
|
2015-07-17 12:14:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithArgs will match given expected args to actual database query arguments.
|
|
|
|
// if at least one argument does not match, it will return an error. For specific
|
|
|
|
// arguments an sqlmock.Argument interface can be used to match an argument.
|
|
|
|
func (e *ExpectedQuery) WithArgs(args ...driver.Value) *ExpectedQuery {
|
|
|
|
e.args = args
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2018-12-11 17:38:12 +02:00
|
|
|
// RowsWillBeClosed expects this query rows to be closed.
|
|
|
|
func (e *ExpectedQuery) RowsWillBeClosed() *ExpectedQuery {
|
|
|
|
e.rowsMustBeClosed = true
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2015-07-17 12:14:30 +02:00
|
|
|
// WillReturnError allows to set an error for expected database query
|
|
|
|
func (e *ExpectedQuery) WillReturnError(err error) *ExpectedQuery {
|
|
|
|
e.err = err
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-02-06 14:38:57 +02:00
|
|
|
// WillDelayFor allows to specify duration for which it will delay
|
|
|
|
// result. May be used together with Context
|
|
|
|
func (e *ExpectedQuery) WillDelayFor(duration time.Duration) *ExpectedQuery {
|
|
|
|
e.delay = duration
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2015-08-26 15:59:28 +02:00
|
|
|
// String returns string representation
|
|
|
|
func (e *ExpectedQuery) String() string {
|
2017-04-26 08:56:02 +02:00
|
|
|
msg := "ExpectedQuery => expecting Query, QueryContext or QueryRow which:"
|
2018-12-11 14:56:33 +02:00
|
|
|
msg += "\n - matches sql: '" + e.expectSQL + "'"
|
2015-08-26 15:59:28 +02:00
|
|
|
|
|
|
|
if len(e.args) == 0 {
|
|
|
|
msg += "\n - is without arguments"
|
|
|
|
} else {
|
|
|
|
msg += "\n - is with arguments:\n"
|
|
|
|
for i, arg := range e.args {
|
|
|
|
msg += fmt.Sprintf(" %d - %+v\n", i, arg)
|
|
|
|
}
|
|
|
|
msg = strings.TrimSpace(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.rows != nil {
|
2017-02-16 22:33:12 +02:00
|
|
|
msg += fmt.Sprintf("\n - %s", e.rows)
|
2015-08-26 15:59:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if e.err != nil {
|
|
|
|
msg += fmt.Sprintf("\n - should return error: %s", e.err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2015-07-17 12:14:30 +02:00
|
|
|
// ExpectedExec is used to manage *sql.DB.Exec, *sql.Tx.Exec or *sql.Stmt.Exec expectations.
|
|
|
|
// Returned by *Sqlmock.ExpectExec.
|
|
|
|
type ExpectedExec struct {
|
|
|
|
queryBasedExpectation
|
|
|
|
result driver.Result
|
2017-02-06 14:38:57 +02:00
|
|
|
delay time.Duration
|
2015-07-17 12:14:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithArgs will match given expected args to actual database exec operation arguments.
|
|
|
|
// if at least one argument does not match, it will return an error. For specific
|
|
|
|
// arguments an sqlmock.Argument interface can be used to match an argument.
|
|
|
|
func (e *ExpectedExec) WithArgs(args ...driver.Value) *ExpectedExec {
|
|
|
|
e.args = args
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// WillReturnError allows to set an error for expected database exec action
|
|
|
|
func (e *ExpectedExec) WillReturnError(err error) *ExpectedExec {
|
|
|
|
e.err = err
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-02-06 14:38:57 +02:00
|
|
|
// WillDelayFor allows to specify duration for which it will delay
|
|
|
|
// result. May be used together with Context
|
|
|
|
func (e *ExpectedExec) WillDelayFor(duration time.Duration) *ExpectedExec {
|
|
|
|
e.delay = duration
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2015-08-26 15:59:28 +02:00
|
|
|
// String returns string representation
|
|
|
|
func (e *ExpectedExec) String() string {
|
2017-04-26 08:56:02 +02:00
|
|
|
msg := "ExpectedExec => expecting Exec or ExecContext which:"
|
2018-12-11 14:56:33 +02:00
|
|
|
msg += "\n - matches sql: '" + e.expectSQL + "'"
|
2015-08-26 15:59:28 +02:00
|
|
|
|
|
|
|
if len(e.args) == 0 {
|
|
|
|
msg += "\n - is without arguments"
|
|
|
|
} else {
|
|
|
|
msg += "\n - is with arguments:\n"
|
|
|
|
var margs []string
|
|
|
|
for i, arg := range e.args {
|
|
|
|
margs = append(margs, fmt.Sprintf(" %d - %+v", i, arg))
|
|
|
|
}
|
|
|
|
msg += strings.Join(margs, "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.result != nil {
|
|
|
|
res, _ := e.result.(*result)
|
|
|
|
msg += "\n - should return Result having:"
|
|
|
|
msg += fmt.Sprintf("\n LastInsertId: %d", res.insertID)
|
|
|
|
msg += fmt.Sprintf("\n RowsAffected: %d", res.rowsAffected)
|
|
|
|
if res.err != nil {
|
|
|
|
msg += fmt.Sprintf("\n Error: %s", res.err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.err != nil {
|
|
|
|
msg += fmt.Sprintf("\n - should return error: %s", e.err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2015-07-17 12:14:30 +02:00
|
|
|
// WillReturnResult arranges for an expected Exec() to return a particular
|
|
|
|
// result, there is sqlmock.NewResult(lastInsertID int64, affectedRows int64) method
|
|
|
|
// to build a corresponding result. Or if actions needs to be tested against errors
|
|
|
|
// sqlmock.NewErrorResult(err error) to return a given error.
|
|
|
|
func (e *ExpectedExec) WillReturnResult(result driver.Result) *ExpectedExec {
|
|
|
|
e.result = result
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExpectedPrepare is used to manage *sql.DB.Prepare or *sql.Tx.Prepare expectations.
|
|
|
|
// Returned by *Sqlmock.ExpectPrepare.
|
|
|
|
type ExpectedPrepare struct {
|
|
|
|
commonExpectation
|
2017-09-01 09:24:02 +02:00
|
|
|
mock *sqlmock
|
2018-12-11 14:56:33 +02:00
|
|
|
expectSQL string
|
2017-09-01 09:24:02 +02:00
|
|
|
statement driver.Stmt
|
|
|
|
closeErr error
|
|
|
|
mustBeClosed bool
|
|
|
|
wasClosed bool
|
|
|
|
delay time.Duration
|
2015-07-17 12:14:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// WillReturnError allows to set an error for the expected *sql.DB.Prepare or *sql.Tx.Prepare action.
|
|
|
|
func (e *ExpectedPrepare) WillReturnError(err error) *ExpectedPrepare {
|
2014-05-29 17:11:13 +03:00
|
|
|
e.err = err
|
2015-07-17 12:14:30 +02:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-08-03 03:09:34 +02:00
|
|
|
// WillReturnCloseError allows to set an error for this prepared statement Close action
|
2015-07-17 12:14:30 +02:00
|
|
|
func (e *ExpectedPrepare) WillReturnCloseError(err error) *ExpectedPrepare {
|
|
|
|
e.closeErr = err
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-02-06 14:38:57 +02:00
|
|
|
// WillDelayFor allows to specify duration for which it will delay
|
|
|
|
// result. May be used together with Context
|
|
|
|
func (e *ExpectedPrepare) WillDelayFor(duration time.Duration) *ExpectedPrepare {
|
|
|
|
e.delay = duration
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-09-01 09:24:02 +02:00
|
|
|
// WillBeClosed expects this prepared statement to
|
|
|
|
// be closed.
|
|
|
|
func (e *ExpectedPrepare) WillBeClosed() *ExpectedPrepare {
|
|
|
|
e.mustBeClosed = true
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2015-07-17 12:14:30 +02:00
|
|
|
// ExpectQuery allows to expect Query() or QueryRow() on this prepared statement.
|
2019-02-18 19:42:21 +02:00
|
|
|
// This method is convenient in order to prevent duplicating sql query string matching.
|
2015-07-17 12:14:30 +02:00
|
|
|
func (e *ExpectedPrepare) ExpectQuery() *ExpectedQuery {
|
|
|
|
eq := &ExpectedQuery{}
|
2018-12-11 14:56:33 +02:00
|
|
|
eq.expectSQL = e.expectSQL
|
2018-08-06 22:29:24 +02:00
|
|
|
eq.converter = e.mock.converter
|
2015-07-17 12:14:30 +02:00
|
|
|
e.mock.expected = append(e.mock.expected, eq)
|
|
|
|
return eq
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExpectExec allows to expect Exec() on this prepared statement.
|
2019-02-18 19:42:21 +02:00
|
|
|
// This method is convenient in order to prevent duplicating sql query string matching.
|
2015-07-17 12:14:30 +02:00
|
|
|
func (e *ExpectedPrepare) ExpectExec() *ExpectedExec {
|
|
|
|
eq := &ExpectedExec{}
|
2018-12-11 14:56:33 +02:00
|
|
|
eq.expectSQL = e.expectSQL
|
2018-08-06 22:29:24 +02:00
|
|
|
eq.converter = e.mock.converter
|
2015-07-17 12:14:30 +02:00
|
|
|
e.mock.expected = append(e.mock.expected, eq)
|
|
|
|
return eq
|
2014-02-05 17:21:07 +03:00
|
|
|
}
|
|
|
|
|
2015-08-26 15:59:28 +02:00
|
|
|
// String returns string representation
|
|
|
|
func (e *ExpectedPrepare) String() string {
|
|
|
|
msg := "ExpectedPrepare => expecting Prepare statement which:"
|
2018-12-11 14:56:33 +02:00
|
|
|
msg += "\n - matches sql: '" + e.expectSQL + "'"
|
2015-08-26 15:59:28 +02:00
|
|
|
|
|
|
|
if e.err != nil {
|
|
|
|
msg += fmt.Sprintf("\n - should return error: %s", e.err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.closeErr != nil {
|
|
|
|
msg += fmt.Sprintf("\n - should return error on Close: %s", e.closeErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2014-02-05 17:21:07 +03:00
|
|
|
// query based expectation
|
2014-02-07 09:58:27 +03:00
|
|
|
// adds a query matching logic
|
2014-02-05 17:21:07 +03:00
|
|
|
type queryBasedExpectation struct {
|
2014-05-29 17:11:13 +03:00
|
|
|
commonExpectation
|
2018-12-11 14:56:33 +02:00
|
|
|
expectSQL string
|
2018-08-06 22:29:24 +02:00
|
|
|
converter driver.ValueConverter
|
|
|
|
args []driver.Value
|
2014-02-05 17:21:07 +03:00
|
|
|
}
|
|
|
|
|
2018-12-11 14:56:33 +02:00
|
|
|
func (e *queryBasedExpectation) attemptArgMatch(args []namedValue) (err error) {
|
2016-02-23 11:14:34 +02:00
|
|
|
// catch panic
|
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
_, ok := e.(error)
|
|
|
|
if !ok {
|
|
|
|
err = fmt.Errorf(e.(string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-08-26 13:28:01 +02:00
|
|
|
|
2016-02-23 11:14:34 +02:00
|
|
|
err = e.argsMatches(args)
|
2015-08-26 13:28:01 +02:00
|
|
|
return
|
|
|
|
}
|