2014-02-07 08:58:27 +02:00
package sqlmock
import (
"database/sql/driver"
"fmt"
)
type conn struct {
expectations [ ] expectation
active expectation
}
2014-02-08 17:51:58 +02:00
// Close a mock database driver connection. It should
2014-02-07 08:58:27 +02:00
// be always called to ensure that all expectations
2014-02-08 17:51:58 +02:00
// were met successfully. Returns error if there is any
2014-02-07 08:58:27 +02:00
func ( c * conn ) Close ( ) ( err error ) {
for _ , e := range mock . conn . expectations {
if ! e . fulfilled ( ) {
2014-02-08 17:51:58 +02:00
err = fmt . Errorf ( "there is a remaining expectation %T which was not matched yet" , e )
2014-02-07 08:58:27 +02:00
break
}
}
mock . conn . expectations = [ ] expectation { }
mock . conn . active = nil
return err
}
func ( c * conn ) Begin ( ) ( driver . Tx , error ) {
e := c . next ( )
if e == nil {
2014-02-08 17:51:58 +02:00
return nil , fmt . Errorf ( "all expectations were already fulfilled, call to begin transaction was not expected" )
2014-02-07 08:58:27 +02:00
}
etb , ok := e . ( * expectedBegin )
if ! ok {
2014-02-08 17:51:58 +02:00
return nil , fmt . Errorf ( "call to begin transaction, was not expected, next expectation is %T as %+v" , e , e )
2014-02-07 08:58:27 +02:00
}
etb . triggered = true
return & transaction { c } , etb . err
}
// get next unfulfilled expectation
func ( c * conn ) next ( ) ( e expectation ) {
for _ , e = range c . expectations {
if ! e . fulfilled ( ) {
return
}
}
return nil // all expectations were fulfilled
}
func ( c * conn ) Exec ( query string , args [ ] driver . Value ) ( driver . Result , error ) {
e := c . next ( )
query = stripQuery ( query )
if e == nil {
2014-02-08 17:51:58 +02:00
return nil , fmt . Errorf ( "all expectations were already fulfilled, call to exec '%s' query with args %+v was not expected" , query , args )
2014-02-07 08:58:27 +02:00
}
eq , ok := e . ( * expectedExec )
if ! ok {
2014-02-08 17:51:58 +02:00
return nil , fmt . Errorf ( "call to exec query '%s' with args %+v, was not expected, next expectation is %T as %+v" , query , args , e , e )
2014-02-07 08:58:27 +02:00
}
eq . triggered = true
if eq . err != nil {
return nil , eq . err // mocked to return error
}
if eq . result == nil {
2014-02-08 17:51:58 +02:00
return nil , fmt . Errorf ( "exec query '%s' with args %+v, must return a database/sql/driver.result, but it was not set for expectation %T as %+v" , query , args , eq , eq )
2014-02-07 08:58:27 +02:00
}
if ! eq . queryMatches ( query ) {
2014-02-08 17:51:58 +02:00
return nil , fmt . Errorf ( "exec query '%s', does not match regex '%s'" , query , eq . sqlRegex . String ( ) )
2014-02-07 08:58:27 +02:00
}
if ! eq . argsMatches ( args ) {
2014-02-08 17:51:58 +02:00
return nil , fmt . Errorf ( "exec query '%s', args %+v does not match expected %+v" , query , args , eq . args )
2014-02-07 08:58:27 +02:00
}
return eq . result , nil
}
func ( c * conn ) Prepare ( query string ) ( driver . Stmt , error ) {
return & statement { mock . conn , stripQuery ( query ) } , nil
}
func ( c * conn ) Query ( query string , args [ ] driver . Value ) ( driver . Rows , error ) {
e := c . next ( )
query = stripQuery ( query )
if e == nil {
2014-02-08 17:51:58 +02:00
return nil , fmt . Errorf ( "all expectations were already fulfilled, call to query '%s' with args %+v was not expected" , query , args )
2014-02-07 08:58:27 +02:00
}
eq , ok := e . ( * expectedQuery )
if ! ok {
2014-02-08 17:51:58 +02:00
return nil , fmt . Errorf ( "call to query '%s' with args %+v, was not expected, next expectation is %T as %+v" , query , args , e , e )
2014-02-07 08:58:27 +02:00
}
eq . triggered = true
if eq . err != nil {
return nil , eq . err // mocked to return error
}
if eq . rows == nil {
2014-02-08 17:51:58 +02:00
return nil , fmt . Errorf ( "query '%s' with args %+v, must return a database/sql/driver.rows, but it was not set for expectation %T as %+v" , query , args , eq , eq )
2014-02-07 08:58:27 +02:00
}
if ! eq . queryMatches ( query ) {
2014-02-08 17:51:58 +02:00
return nil , fmt . Errorf ( "query '%s', does not match regex [%s]" , query , eq . sqlRegex . String ( ) )
2014-02-07 08:58:27 +02:00
}
if ! eq . argsMatches ( args ) {
2014-02-08 17:51:58 +02:00
return nil , fmt . Errorf ( "query '%s', args %+v does not match expected %+v" , query , args , eq . args )
2014-02-07 08:58:27 +02:00
}
return eq . rows , nil
}