mirror of
https://github.com/DATA-DOG/go-sqlmock.git
synced 2025-03-17 20:48:01 +02:00
Merge pull request #307 from Woody1193/master
Added SqlMock type to allow for options to be sent to function that creates the sqlmock
This commit is contained in:
commit
6bed17cdbe
@ -40,7 +40,7 @@ func (d *mockDriver) Open(dsn string) (driver.Conn, error) {
|
|||||||
// a specific driver.
|
// a specific driver.
|
||||||
// Pings db so that all expectations could be
|
// Pings db so that all expectations could be
|
||||||
// asserted.
|
// asserted.
|
||||||
func New(options ...func(*sqlmock) error) (*sql.DB, Sqlmock, error) {
|
func New(options ...SqlMockOption) (*sql.DB, Sqlmock, error) {
|
||||||
pool.Lock()
|
pool.Lock()
|
||||||
dsn := fmt.Sprintf("sqlmock_db_%d", pool.counter)
|
dsn := fmt.Sprintf("sqlmock_db_%d", pool.counter)
|
||||||
pool.counter++
|
pool.counter++
|
||||||
@ -67,7 +67,7 @@ func New(options ...func(*sqlmock) error) (*sql.DB, Sqlmock, error) {
|
|||||||
//
|
//
|
||||||
// It is not recommended to use this method, unless you
|
// It is not recommended to use this method, unless you
|
||||||
// really need it and there is no other way around.
|
// really need it and there is no other way around.
|
||||||
func NewWithDSN(dsn string, options ...func(*sqlmock) error) (*sql.DB, Sqlmock, error) {
|
func NewWithDSN(dsn string, options ...SqlMockOption) (*sql.DB, Sqlmock, error) {
|
||||||
pool.Lock()
|
pool.Lock()
|
||||||
if _, ok := pool.conns[dsn]; ok {
|
if _, ok := pool.conns[dsn]; ok {
|
||||||
pool.Unlock()
|
pool.Unlock()
|
||||||
|
@ -2,9 +2,12 @@ package sqlmock
|
|||||||
|
|
||||||
import "database/sql/driver"
|
import "database/sql/driver"
|
||||||
|
|
||||||
|
// SqlMockOption is the type defining an option used to configure an SqlMock at creation
|
||||||
|
type SqlMockOption func(*sqlmock) error
|
||||||
|
|
||||||
// ValueConverterOption allows to create a sqlmock connection
|
// ValueConverterOption allows to create a sqlmock connection
|
||||||
// with a custom ValueConverter to support drivers with special data types.
|
// with a custom ValueConverter to support drivers with special data types.
|
||||||
func ValueConverterOption(converter driver.ValueConverter) func(*sqlmock) error {
|
func ValueConverterOption(converter driver.ValueConverter) SqlMockOption {
|
||||||
return func(s *sqlmock) error {
|
return func(s *sqlmock) error {
|
||||||
s.converter = converter
|
s.converter = converter
|
||||||
return nil
|
return nil
|
||||||
@ -14,7 +17,7 @@ func ValueConverterOption(converter driver.ValueConverter) func(*sqlmock) error
|
|||||||
// QueryMatcherOption allows to customize SQL query matcher
|
// QueryMatcherOption allows to customize SQL query matcher
|
||||||
// and match SQL query strings in more sophisticated ways.
|
// and match SQL query strings in more sophisticated ways.
|
||||||
// The default QueryMatcher is QueryMatcherRegexp.
|
// The default QueryMatcher is QueryMatcherRegexp.
|
||||||
func QueryMatcherOption(queryMatcher QueryMatcher) func(*sqlmock) error {
|
func QueryMatcherOption(queryMatcher QueryMatcher) SqlMockOption {
|
||||||
return func(s *sqlmock) error {
|
return func(s *sqlmock) error {
|
||||||
s.queryMatcher = queryMatcher
|
s.queryMatcher = queryMatcher
|
||||||
return nil
|
return nil
|
||||||
@ -30,7 +33,7 @@ func QueryMatcherOption(queryMatcher QueryMatcher) func(*sqlmock) error {
|
|||||||
// If false is passed or this option is omitted, calls to Ping will not be
|
// 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
|
// considered when determining expectations and calls to ExpectPing will have
|
||||||
// no effect.
|
// no effect.
|
||||||
func MonitorPingsOption(monitorPings bool) func(*sqlmock) error {
|
func MonitorPingsOption(monitorPings bool) SqlMockOption {
|
||||||
return func(s *sqlmock) error {
|
return func(s *sqlmock) error {
|
||||||
s.monitorPings = monitorPings
|
s.monitorPings = monitorPings
|
||||||
return nil
|
return nil
|
||||||
|
@ -98,7 +98,7 @@ type sqlmock struct {
|
|||||||
expected []expectation
|
expected []expectation
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *sqlmock) open(options []func(*sqlmock) error) (*sql.DB, Sqlmock, error) {
|
func (c *sqlmock) open(options []SqlMockOption) (*sql.DB, Sqlmock, error) {
|
||||||
db, err := sql.Open("sqlmock", c.dsn)
|
db, err := sql.Open("sqlmock", c.dsn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return db, c, err
|
return db, c, err
|
||||||
|
Loading…
x
Reference in New Issue
Block a user