Skip to content

Commit a04eaa2

Browse files
committed
Added SqlMock type to allow for options to be sent to function that creates the sqlmock
1 parent 44e746a commit a04eaa2

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

driver.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (d *mockDriver) Open(dsn string) (driver.Conn, error) {
4040
// a specific driver.
4141
// Pings db so that all expectations could be
4242
// asserted.
43-
func New(options ...func(*sqlmock) error) (*sql.DB, Sqlmock, error) {
43+
func New(options ...SqlMockOption) (*sql.DB, Sqlmock, error) {
4444
pool.Lock()
4545
dsn := fmt.Sprintf("sqlmock_db_%d", pool.counter)
4646
pool.counter++
@@ -67,7 +67,7 @@ func New(options ...func(*sqlmock) error) (*sql.DB, Sqlmock, error) {
6767
//
6868
// It is not recommended to use this method, unless you
6969
// really need it and there is no other way around.
70-
func NewWithDSN(dsn string, options ...func(*sqlmock) error) (*sql.DB, Sqlmock, error) {
70+
func NewWithDSN(dsn string, options ...SqlMockOption) (*sql.DB, Sqlmock, error) {
7171
pool.Lock()
7272
if _, ok := pool.conns[dsn]; ok {
7373
pool.Unlock()

options.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package sqlmock
22

33
import "database/sql/driver"
44

5+
// SqlMockOption is the type defining an option used to configure an SqlMock at creation
6+
type SqlMockOption func(*sqlmock) error
7+
58
// ValueConverterOption allows to create a sqlmock connection
69
// with a custom ValueConverter to support drivers with special data types.
7-
func ValueConverterOption(converter driver.ValueConverter) func(*sqlmock) error {
10+
func ValueConverterOption(converter driver.ValueConverter) SqlMockOption {
811
return func(s *sqlmock) error {
912
s.converter = converter
1013
return nil
@@ -14,7 +17,7 @@ func ValueConverterOption(converter driver.ValueConverter) func(*sqlmock) error
1417
// QueryMatcherOption allows to customize SQL query matcher
1518
// and match SQL query strings in more sophisticated ways.
1619
// The default QueryMatcher is QueryMatcherRegexp.
17-
func QueryMatcherOption(queryMatcher QueryMatcher) func(*sqlmock) error {
20+
func QueryMatcherOption(queryMatcher QueryMatcher) SqlMockOption {
1821
return func(s *sqlmock) error {
1922
s.queryMatcher = queryMatcher
2023
return nil
@@ -30,7 +33,7 @@ func QueryMatcherOption(queryMatcher QueryMatcher) func(*sqlmock) error {
3033
// If false is passed or this option is omitted, calls to Ping will not be
3134
// considered when determining expectations and calls to ExpectPing will have
3235
// no effect.
33-
func MonitorPingsOption(monitorPings bool) func(*sqlmock) error {
36+
func MonitorPingsOption(monitorPings bool) SqlMockOption {
3437
return func(s *sqlmock) error {
3538
s.monitorPings = monitorPings
3639
return nil

sqlmock.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ type sqlmock struct {
9898
expected []expectation
9999
}
100100

101-
func (c *sqlmock) open(options []func(*sqlmock) error) (*sql.DB, Sqlmock, error) {
101+
func (c *sqlmock) open(options []SqlMockOption) (*sql.DB, Sqlmock, error) {
102102
db, err := sql.Open("sqlmock", c.dsn)
103103
if err != nil {
104104
return db, c, err

0 commit comments

Comments
 (0)