Documentation ¶
Overview ¶
Package sqlmock is a mock library implementing sql driver. Which has one and only purpose - to simulate any sql driver behavior in tests, without needing a real database connection. It helps to maintain correct **TDD** workflow.
It does not require any modifications to your source code in order to test and mock database operations. Supports concurrency and multiple database mocking.
The driver allows to mock any sql driver method behavior.
Package sqlmock is a mock library implementing sql driver. Which has one and only purpose - to simulate any sql driver behavior in tests, without needing a real database connection. It helps to maintain correct **TDD** workflow.
It does not require any modifications to your source code in order to test and mock database operations. Supports concurrency and multiple database mocking.
The driver allows to mock any sql driver method behavior.
Package sqlmock is a mock library implementing sql driver. Which has one and only purpose - to simulate any sql driver behavior in tests, without needing a real database connection. It helps to maintain correct **TDD** workflow.
It does not require any modifications to your source code in order to test and mock database operations. Supports concurrency and multiple database mocking.
The driver allows to mock any sql driver method behavior.
Example ¶
// Open new mock database db, mock, err := New() if err != nil { fmt.Println("error creating mock database") return } // columns to be used for result columns := []string{"id", "status"} // expect transaction begin mock.ExpectBegin() // expect query to fetch order, match it with regexp mock.ExpectSql(nil, "SELECT (.+) FROM orders (.+) FOR UPDATE"). WithArgs(1). WillReturnRows(NewRows(columns).AddRow(1, 1)) // expect transaction rollback, since order status is "cancelled" mock.ExpectRollback() // run the cancel order function someOrderID := 1 // call a function which executes expected database operations err = cancelOrder(db, someOrderID) if err != nil { fmt.Printf("unexpected error: %s", err) return } // ensure all expectations have been met if err = mock.ExpectationsWereMet(); err != nil { fmt.Printf("unmet expectation error: %s", err) }
Output:
Index ¶
- Variables
- func DefaultDsn() string
- func MonitorPingsOption(monitorPings bool) func(*sqlmock) error
- func NewErrorResult(err error) driver.Result
- func NewResult(lastInsertID int64, rowsAffected int64) driver.Result
- func QueryMatcherOption(queryMatcher QueryMatcher) func(*sqlmock) error
- func ValueConverterOption(converter driver.ValueConverter) func(*sqlmock) error
- type Common
- type ExpectedBegin
- type ExpectedClose
- type ExpectedCommit
- type ExpectedOperation
- type ExpectedPing
- type ExpectedPrepare
- func (e *ExpectedPrepare) String() string
- func (e *ExpectedPrepare) WillBeClosed() *ExpectedPrepare
- func (e *ExpectedPrepare) WillDelayFor(duration time.Duration) *ExpectedPrepare
- func (e *ExpectedPrepare) WillReturnCloseError(err error) *ExpectedPrepare
- func (e *ExpectedPrepare) WillReturnError(err error) *ExpectedPrepare
- type ExpectedRollback
- type ExpectedSql
- func (e *ExpectedSql) RowsWillBeClosed() *ExpectedSql
- func (e *ExpectedSql) String() string
- func (e *ExpectedSql) WillDelayFor(duration time.Duration) *ExpectedSql
- func (e *ExpectedSql) WillReturnError(err error) *ExpectedSql
- func (e *ExpectedSql) WillReturnResult(result driver.Result) *ExpectedSql
- func (e *ExpectedSql) WillReturnRows(rows ...*Rows) *ExpectedSql
- func (e *ExpectedSql) WithArgs(args ...driver.Value) *ExpectedSql
- func (e *ExpectedSql) WithArgsCheck(checkArgs func(args []driver.Value) error) *ExpectedSql
- type MatchFunc
- type Matcher
- type QueryMatcher
- type QueryMatcherFunc
- type Rows
- type Sqlmock
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrCancelled = errors.New("canceling query due to user request")
ErrCancelled defines an error value, which can be expected in case of such cancellation error.
Functions ¶
func DefaultDsn ¶
func DefaultDsn() string
func MonitorPingsOption ¶
MonitorPingsOption determines whether calls to Ping on the driver should be observed and mocked.
If true is passed, we will check these calls were expected. Expectations can be registered using the ExpectPing() method on the mock.
If false is passed or this option is omitted, calls to Ping will not be considered when determining expectations and calls to ExpectPing will have no effect.
func NewErrorResult ¶
NewErrorResult creates a new sql driver Result which returns an error given for both interface methods
Example ¶
db, mock, _ := New() result := NewErrorResult(fmt.Errorf("some error")) mock.ExpectSql(nil, "^INSERT (.+)").WillReturnResult(result) res, _ := db.Exec("INSERT something") _, err := res.LastInsertId() fmt.Println(err)
Output: some error
func QueryMatcherOption ¶
func QueryMatcherOption(queryMatcher QueryMatcher) func(*sqlmock) error
QueryMatcherOption allows to customize SQL query matcher and match SQL query strings in more sophisticated ways. The default QueryMatcher is QueryMatcherRegexp.
func ValueConverterOption ¶
func ValueConverterOption(converter driver.ValueConverter) func(*sqlmock) error
ValueConverterOption allows to create a sqlmock connection with a custom ValueConverter to support drivers with special data types.
Types ¶
type Common ¶
type Common interface { // ExpectClose queues an expectation for this database // action to be triggered. the *ExpectedClose allows // to mock database response ExpectClose() *ExpectedClose // ExpectationsWereMet checks whether all queued expectations // were met in order. If any of them was not met - an error is returned. ExpectationsWereMet() error // ExpectPrepare expects Prepare() to be called with expectedSQL query. // the *ExpectedPrepare allows to mock database response. // Note that you may expect Query() or Exec() on the *ExpectedPrepare // statement to prevent repeating expectedSQL ExpectPrepare(expectedSQL string) *ExpectedPrepare // ExpectBegin expects *sql.DB.Begin to be called. // the *ExpectedBegin allows to mock database response ExpectBegin() *ExpectedBegin // ExpectCommit expects *sql.Tx.Commit to be called. // the *ExpectedCommit allows to mock database response ExpectCommit() *ExpectedCommit // ExpectRollback expects *sql.Tx.Rollback to be called. // the *ExpectedRollback allows to mock database response ExpectRollback() *ExpectedRollback // ExpectPing expected *sql.DB.Ping to be called. // the *ExpectedPing allows to mock database response // // Ping support only exists in the SQL library in Go 1.8 and above. // ExpectPing in Go <=1.7 will return an ExpectedPing but not register // any expectations. // // You must enable pings using MonitorPingsOption for this to register // any expectations. ExpectPing() *ExpectedPing // MatchExpectationsInOrder gives an option whether to match all // expectations in the order they were set or not. // // By default it is set to - true. But if you use goroutines // to parallelize your query executation, that option may // be handy. // // This option may be turned on anytime during tests. As soon // as it is switched to false, expectations will be matched // in any order. Or otherwise if switched to true, any unmatched // expectations will be expected in order MatchExpectationsInOrder(bool) // NewRows allows Rows to be created from a // sql driver.Value slice or from the CSV string and // to be used as sql driver.Rows. NewRows(columns []string) *Rows ExpectSql(expectedOpt Matcher, expectedSQL string) *ExpectedSql }
Common interface serves to create expectations for any kind of database action in order to mock and test real database behavior.
type ExpectedBegin ¶
type ExpectedBegin struct {
// contains filtered or unexported fields
}
ExpectedBegin is used to manage *sql.DB.Begin expectation returned by *Sqlmock.ExpectBegin.
func (*ExpectedBegin) String ¶
func (e *ExpectedBegin) String() string
String returns string representation
func (*ExpectedBegin) WillDelayFor ¶
func (e *ExpectedBegin) WillDelayFor(duration time.Duration) *ExpectedBegin
WillDelayFor allows to specify duration for which it will delay result. May be used together with Context
func (*ExpectedBegin) WillReturnError ¶
func (e *ExpectedBegin) WillReturnError(err error) *ExpectedBegin
WillReturnError allows to set an error for *sql.DB.Begin action
type ExpectedClose ¶
type ExpectedClose struct {
// contains filtered or unexported fields
}
ExpectedClose is used to manage *sql.DB.Close expectation returned by *Sqlmock.ExpectClose.
func (*ExpectedClose) String ¶
func (e *ExpectedClose) String() string
String returns string representation
func (*ExpectedClose) WillReturnError ¶
func (e *ExpectedClose) WillReturnError(err error) *ExpectedClose
WillReturnError allows to set an error for *sql.DB.Close action
type ExpectedCommit ¶
type ExpectedCommit struct {
// contains filtered or unexported fields
}
ExpectedCommit is used to manage *sql.Tx.Commit expectation returned by *Sqlmock.ExpectCommit.
func (*ExpectedCommit) String ¶
func (e *ExpectedCommit) String() string
String returns string representation
func (*ExpectedCommit) WillReturnError ¶
func (e *ExpectedCommit) WillReturnError(err error) *ExpectedCommit
WillReturnError allows to set an error for *sql.Tx.Close action
type ExpectedOperation ¶
type ExpectedOperation struct {
// contains filtered or unexported fields
}
func (*ExpectedOperation) String ¶
func (e *ExpectedOperation) String() string
String returns string representation
func (*ExpectedOperation) WillReturnError ¶
func (e *ExpectedOperation) WillReturnError(err error) *ExpectedOperation
WillReturnError allows to set an error for *sql.DB.Begin action
type ExpectedPing ¶
type ExpectedPing struct {
// contains filtered or unexported fields
}
ExpectedPing is used to manage *sql.DB.Ping expectations. Returned by *Sqlmock.ExpectPing.
func (*ExpectedPing) String ¶
func (e *ExpectedPing) String() string
String returns string representation
func (*ExpectedPing) WillDelayFor ¶
func (e *ExpectedPing) WillDelayFor(duration time.Duration) *ExpectedPing
WillDelayFor allows to specify duration for which it will delay result. May be used together with Context.
func (*ExpectedPing) WillReturnError ¶
func (e *ExpectedPing) WillReturnError(err error) *ExpectedPing
WillReturnError allows to set an error for expected database ping
type ExpectedPrepare ¶
type ExpectedPrepare struct {
// contains filtered or unexported fields
}
ExpectedPrepare is used to manage *sql.DB.Prepare or *sql.Tx.Prepare expectations. Returned by *Sqlmock.WithPrepare.
func (*ExpectedPrepare) String ¶
func (e *ExpectedPrepare) String() string
String returns string representation
func (*ExpectedPrepare) WillBeClosed ¶
func (e *ExpectedPrepare) WillBeClosed() *ExpectedPrepare
WillBeClosed expects this prepared statement to be closed.
func (*ExpectedPrepare) WillDelayFor ¶
func (e *ExpectedPrepare) WillDelayFor(duration time.Duration) *ExpectedPrepare
WillDelayFor allows to specify duration for which it will delay result. May be used together with Context
func (*ExpectedPrepare) WillReturnCloseError ¶
func (e *ExpectedPrepare) WillReturnCloseError(err error) *ExpectedPrepare
WillReturnCloseError allows to set an error for this prepared statement Close action
func (*ExpectedPrepare) WillReturnError ¶
func (e *ExpectedPrepare) WillReturnError(err error) *ExpectedPrepare
WillReturnError allows to set an error for the expected *sql.DB.Prepare or *sql.Tx.Prepare action.
type ExpectedRollback ¶
type ExpectedRollback struct {
// contains filtered or unexported fields
}
ExpectedRollback is used to manage *sql.Tx.Rollback expectation returned by *Sqlmock.ExpectRollback.
func (*ExpectedRollback) String ¶
func (e *ExpectedRollback) String() string
String returns string representation
func (*ExpectedRollback) WillReturnError ¶
func (e *ExpectedRollback) WillReturnError(err error) *ExpectedRollback
WillReturnError allows to set an error for *sql.Tx.Rollback action
type ExpectedSql ¶
type ExpectedSql struct {
// contains filtered or unexported fields
}
ExpectedSql 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.
func (*ExpectedSql) RowsWillBeClosed ¶
func (e *ExpectedSql) RowsWillBeClosed() *ExpectedSql
RowsWillBeClosed expects this query rows to be closed.
func (*ExpectedSql) String ¶
func (e *ExpectedSql) String() string
String returns string representation
func (*ExpectedSql) WillDelayFor ¶
func (e *ExpectedSql) WillDelayFor(duration time.Duration) *ExpectedSql
WillDelayFor allows to specify duration for which it will delay result. May be used together with Context
func (*ExpectedSql) WillReturnError ¶
func (e *ExpectedSql) WillReturnError(err error) *ExpectedSql
WillReturnError allows to set an error for expected database query
func (*ExpectedSql) WillReturnResult ¶
func (e *ExpectedSql) WillReturnResult(result driver.Result) *ExpectedSql
func (*ExpectedSql) WillReturnRows ¶
func (e *ExpectedSql) WillReturnRows(rows ...*Rows) *ExpectedSql
func (*ExpectedSql) WithArgs ¶
func (e *ExpectedSql) WithArgs(args ...driver.Value) *ExpectedSql
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.Matcher interface can be used to match an argument.
func (*ExpectedSql) WithArgsCheck ¶
func (e *ExpectedSql) WithArgsCheck(checkArgs func(args []driver.Value) error) *ExpectedSql
WithArgsCheck match sql args
type Matcher ¶
Matcher interface allows to match any argument in specific way when used with Expected expectations.
type QueryMatcher ¶
type QueryMatcher interface { // Match expected SQL query string without whitespace to // actual SQL. Match(expectedSQL, actualSQL string) error }
QueryMatcher is an SQL query string matcher interface, which can be used to customize validation of SQL query strings. As an example, external library could be used to build and validate SQL ast, columns selected.
sqlmock can be customized to implement a different QueryMatcher configured through an option when sqlmock.New or sqlmock.NewWithDSN is called, default QueryMatcher is QueryMatcherRegexp.
Example ¶
// configure to use case sensitive SQL query matcher // instead of default regular expression matcher db, mock, err := New(QueryMatcherOption(QueryMatcherEqual)) if err != nil { fmt.Println("failed to open sqlmock database:", err) } defer db.Close() rows := NewRows([]string{"id", "title"}). AddRow(1, "one"). AddRow(2, "two") mock.ExpectSql(nil, "SELECT * FROM users").WillReturnRows(rows) rs, err := db.Query("SELECT * FROM users") if err != nil { fmt.Println("failed to match expected query") return } defer rs.Close() for rs.Next() { var id int var title string rs.Scan(&id, &title) fmt.Println("scanned id:", id, "and title:", title) } if rs.Err() != nil { fmt.Println("got rows error:", rs.Err()) }
Output: scanned id: 1 and title: one scanned id: 2 and title: two
var QueryMatcherEqual QueryMatcher = QueryMatcherFunc(func(expectedSQL, actualSQL string) error { expect := stripQuery(expectedSQL) actual := stripQuery(actualSQL) if actual != expect { return fmt.Errorf(`actual sql: "%s" does not equal to expected "%s"`, actual, expect) } return nil })
QueryMatcherEqual is the SQL query matcher which simply tries a case sensitive match of expected and actual SQL strings without whitespace.
var QueryMatcherRegexp QueryMatcher = QueryMatcherFunc(func(expectedSQL, actualSQL string) error { expect := stripQuery(expectedSQL) actual := stripQuery(actualSQL) re, err := regexp.Compile(expect) if err != nil { return err } if !re.MatchString(actual) { return fmt.Errorf(`could not match actual sql: "%s" with expected regexp "%s"`, actual, re.String()) } return nil })
QueryMatcherRegexp is the default SQL query matcher used by sqlmock. It parses expectedSQL to a regular expression and attempts to match actualSQL.
type QueryMatcherFunc ¶
QueryMatcherFunc type is an adapter to allow the use of ordinary functions as QueryMatcher. If f is a function with the appropriate signature, QueryMatcherFunc(f) is a QueryMatcher that calls f.
func (QueryMatcherFunc) Match ¶
func (f QueryMatcherFunc) Match(expectedSQL, actualSQL string) error
Match implements the QueryMatcher
type Rows ¶
type Rows struct {
// contains filtered or unexported fields
}
Rows is a mocked collection of rows to return for Query result
Example ¶
db, mock, err := New() if err != nil { fmt.Println("failed to open sqlmock database:", err) } defer db.Close() rows := NewRows([]string{"id", "title"}). AddRow(1, "one"). AddRow(2, "two") mock.ExpectSql(nil, "SELECT").WillReturnRows(rows) rs, _ := db.Query("SELECT") defer rs.Close() for rs.Next() { var id int var title string rs.Scan(&id, &title) fmt.Println("scanned id:", id, "and title:", title) } if rs.Err() != nil { fmt.Println("got rows error:", rs.Err()) }
Output: scanned id: 1 and title: one scanned id: 2 and title: two
Example (CloseError) ¶
db, mock, err := New() if err != nil { fmt.Println("failed to open sqlmock database:", err) } defer db.Close() rows := NewRows([]string{"id", "title"}).CloseError(fmt.Errorf("close error")) mock.ExpectSql(nil, "SELECT").WillReturnRows(rows) rs, _ := db.Query("SELECT") // Note: that close will return error only before rows EOF // that is a default sql package behavior. If you run rs.Next() // it will handle the error internally and return nil bellow if err := rs.Close(); err != nil { fmt.Println("got error:", err) }
Output: got error: close error
Example (CustomDriverValue) ¶
db, mock, err := New() if err != nil { fmt.Println("failed to open sqlmock database:", err) } defer db.Close() rows := NewRows([]string{"id", "null_int"}). AddRow(1, 7). AddRow(5, sql.NullInt64{Int64: 5, Valid: true}). AddRow(2, sql.NullInt64{}) mock.ExpectSql(nil, "SELECT").WillReturnRows(rows) rs, _ := db.Query("SELECT") defer rs.Close() for rs.Next() { var id int var num sql.NullInt64 rs.Scan(&id, &num) fmt.Println("scanned id:", id, "and null int64:", num) } if rs.Err() != nil { fmt.Println("got rows error:", rs.Err()) }
Output: scanned id: 1 and null int64: {7 true} scanned id: 5 and null int64: {5 true} scanned id: 2 and null int64: {0 false}
Example (ExpectToBeClosed) ¶
db, mock, err := New() if err != nil { fmt.Println("failed to open sqlmock database:", err) } defer db.Close() rows := NewRows([]string{"id", "title"}).AddRow(1, "john") mock.ExpectSql(nil, "SELECT").WillReturnRows(rows).RowsWillBeClosed() db.Query("SELECT") if err := mock.ExpectationsWereMet(); err != nil { fmt.Println("got error:", err) }
Output: got error: expected query rows to be closed, but it was not: ExpectedSql => expecting Query, QueryContext or QueryRow which: - matches sql: 'SELECT' - is without arguments - should return rows: row 0 - [1 john]
Example (RawBytes) ¶
db, mock, err := New() if err != nil { fmt.Println("failed to open sqlmock database:", err) } defer db.Close() rows := NewRows([]string{"id", "binary"}). AddRow(1, []byte(`one binary value with some text!`)). AddRow(2, []byte(`two binary value with even more text than the first one`)) mock.ExpectSql(nil, "SELECT").WillReturnRows(rows) rs, _ := db.Query("SELECT") defer rs.Close() type scanned struct { id int raw sql.RawBytes } fmt.Println("initial read...") var ss []scanned for rs.Next() { var s scanned rs.Scan(&s.id, &s.raw) ss = append(ss, s) fmt.Println("scanned id:", s.id, "and raw:", string(s.raw)) } if rs.Err() != nil { fmt.Println("got rows error:", rs.Err()) } fmt.Println("after reading all...") for _, s := range ss { fmt.Println("scanned id:", s.id, "and raw:", string(s.raw)) }
Output: initial read... scanned id: 1 and raw: one binary value with some text! scanned id: 2 and raw: two binary value with even more text than the first one after reading all... scanned id: 1 and raw: ☠☠☠ MEMORY OVERWRITTEN ☠ scanned id: 2 and raw: ☠☠☠ MEMORY OVERWRITTEN ☠☠☠ ☠☠☠ MEMORY
Example (RowError) ¶
db, mock, err := New() if err != nil { fmt.Println("failed to open sqlmock database:", err) } defer db.Close() rows := NewRows([]string{"id", "title"}). AddRow(0, "one"). AddRow(1, "two"). RowError(1, fmt.Errorf("row error")) mock.ExpectSql(nil, "SELECT").WillReturnRows(rows) rs, _ := db.Query("SELECT") defer rs.Close() for rs.Next() { var id int var title string rs.Scan(&id, &title) fmt.Println("scanned id:", id, "and title:", title) } if rs.Err() != nil { fmt.Println("got rows error:", rs.Err()) }
Output: scanned id: 0 and title: one got rows error: row error
func NewRows ¶
NewRows allows Rows to be created from a sql driver.Value slice or from the CSV string and to be used as sql driver.Rows. Use Sqlmock.NewRows instead if using a custom converter
func (*Rows) AddRow ¶
AddRow composed from database driver.Value slice return the same instance to perform subsequent actions. Note that the number of values must match the number of columns
func (*Rows) AddRows ¶
AddRows adds multiple rows composed from database driver.Value slice and returns the same instance to perform subsequent actions.
Example ¶
db, mock, err := New() if err != nil { fmt.Println("failed to open sqlmock database:", err) } defer db.Close() values := [][]driver.Value{ { 1, "one", }, { 2, "two", }, } rows := NewRows([]string{"id", "title"}).AddRows(values...) mock.ExpectSql(nil, "SELECT").WillReturnRows(rows) rs, _ := db.Query("SELECT") defer rs.Close() for rs.Next() { var id int var title string rs.Scan(&id, &title) fmt.Println("scanned id:", id, "and title:", title) } if rs.Err() != nil { fmt.Println("got rows error:", rs.Err()) }
Output: scanned id: 1 and title: one scanned id: 2 and title: two
func (*Rows) CloseError ¶
CloseError allows to set an error which will be returned by rows.Close function.
The close error will be triggered only in cases when rows.Next() EOF was not yet reached, that is a default sql library behavior
type Sqlmock ¶
type Sqlmock interface { // Common Embed common methods Common }
Sqlmock interface for Go 1.8+
Example (Goroutines) ¶
db, mock, err := New() if err != nil { fmt.Println("failed to open sqlmock database:", err) } defer db.Close() // note this line is important for unordered expectation matching mock.MatchExpectationsInOrder(false) result := NewResult(1, 1) mock.ExpectSql(nil, "^UPDATE one").WithArgs("one").WillReturnResult(result) mock.ExpectSql(nil, "^UPDATE two").WithArgs("one", "two").WillReturnResult(result) mock.ExpectSql(nil, "^UPDATE three").WithArgs("one", "two", "three").WillReturnResult(result) var wg sync.WaitGroup queries := map[string][]interface{}{ "one": {"one"}, "two": {"one", "two"}, "three": {"one", "two", "three"}, } wg.Add(len(queries)) for table, args := range queries { go func(tbl string, a []interface{}) { if _, err := db.Exec("UPDATE "+tbl, a...); err != nil { fmt.Println("error was not expected:", err) } wg.Done() }(table, args) } wg.Wait() if err := mock.ExpectationsWereMet(); err != nil { fmt.Println("there were unfulfilled expectations:", err) }
Output:
func New ¶
New creates sqlmock database connection and a mock to manage expectations. Accepts options, like ValueConverterOption, to use a ValueConverter from a specific driver. Pings db so that all expectations could be asserted.
Example ¶
db, mock, err := New() if err != nil { fmt.Println("expected no error, but got:", err) return } defer db.Close() // now we can expect operations performed on db mock.ExpectBegin().WillReturnError(fmt.Errorf("an error will occur on db.Begin() call"))
Output:
func NewWithDSN ¶
NewWithDSN creates sqlmock database connection with a specific DSN and a mock to manage expectations. Accepts options, like ValueConverterOption, to use a ValueConverter from a specific driver. Pings db so that all expectations could be asserted.
This method is introduced because of sql abstraction libraries, which do not provide a way to initialize with sql.DB instance. For example GORM library.
Note, it will error if attempted to create with an already used dsn
It is not recommended to use this method, unless you really need it and there is no other way around.