Skip to content

Commit 52e994a

Browse files
authored
Merge branch 'master' into patch-1
2 parents 645bbfb + 6bed17c commit 52e994a

16 files changed

+178
-29
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: [l3pp4rd, gliptak, dolmen, IvoGoman]

driver.go

Lines changed: 2 additions & 2 deletions
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()

expectations.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,27 @@ type ExpectedQuery struct {
134134
// WithArgs will match given expected args to actual database query arguments.
135135
// if at least one argument does not match, it will return an error. For specific
136136
// arguments an sqlmock.Argument interface can be used to match an argument.
137+
// Must not be used together with WithoutArgs()
137138
func (e *ExpectedQuery) WithArgs(args ...driver.Value) *ExpectedQuery {
139+
if e.noArgs {
140+
panic("WithArgs() and WithoutArgs() must not be used together")
141+
}
138142
e.args = args
139143
return e
140144
}
141145

146+
// WithoutArgs will ensure that no arguments are passed for this query.
147+
// if at least one argument is passed, it will return an error. This allows
148+
// for stricter validation of the query arguments.
149+
// Must no be used together with WithArgs()
150+
func (e *ExpectedQuery) WithoutArgs() *ExpectedQuery {
151+
if len(e.args) > 0 {
152+
panic("WithoutArgs() and WithArgs() must not be used together")
153+
}
154+
e.noArgs = true
155+
return e
156+
}
157+
142158
// RowsWillBeClosed expects this query rows to be closed.
143159
func (e *ExpectedQuery) RowsWillBeClosed() *ExpectedQuery {
144160
e.rowsMustBeClosed = true
@@ -195,11 +211,27 @@ type ExpectedExec struct {
195211
// WithArgs will match given expected args to actual database exec operation arguments.
196212
// if at least one argument does not match, it will return an error. For specific
197213
// arguments an sqlmock.Argument interface can be used to match an argument.
214+
// Must not be used together with WithoutArgs()
198215
func (e *ExpectedExec) WithArgs(args ...driver.Value) *ExpectedExec {
216+
if len(e.args) > 0 {
217+
panic("WithArgs() and WithoutArgs() must not be used together")
218+
}
199219
e.args = args
200220
return e
201221
}
202222

223+
// WithoutArgs will ensure that no args are passed for this expected database exec action.
224+
// if at least one argument is passed, it will return an error. This allows for stricter
225+
// validation of the query arguments.
226+
// Must not be used together with WithArgs()
227+
func (e *ExpectedExec) WithoutArgs() *ExpectedExec {
228+
if len(e.args) > 0 {
229+
panic("WithoutArgs() and WithArgs() must not be used together")
230+
}
231+
e.noArgs = true
232+
return e
233+
}
234+
203235
// WillReturnError allows to set an error for expected database exec action
204236
func (e *ExpectedExec) WillReturnError(err error) *ExpectedExec {
205237
e.err = err
@@ -338,6 +370,7 @@ type queryBasedExpectation struct {
338370
expectSQL string
339371
converter driver.ValueConverter
340372
args []driver.Value
373+
noArgs bool // ensure no args are passed
341374
}
342375

343376
// ExpectedPing is used to manage *sql.DB.Ping expectations.

expectations_before_go18.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build !go1.8
12
// +build !go1.8
23

34
package sqlmock
@@ -17,7 +18,7 @@ func (e *ExpectedQuery) WillReturnRows(rows *Rows) *ExpectedQuery {
1718

1819
func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
1920
if nil == e.args {
20-
if len(args) > 0 {
21+
if e.noArgs && len(args) > 0 {
2122
return fmt.Errorf("expected 0, but got %d arguments", len(args))
2223
}
2324
return nil

expectations_before_go18_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build !go1.8
12
// +build !go1.8
23

34
package sqlmock
@@ -9,10 +10,15 @@ import (
910
)
1011

1112
func TestQueryExpectationArgComparison(t *testing.T) {
12-
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter}
13+
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter, noArgs: true}
1314
against := []namedValue{{Value: int64(5), Ordinal: 1}}
1415
if err := e.argsMatches(against); err == nil {
15-
t.Error("arguments should not match, since no expectation was set, but argument was passed")
16+
t.Error("arguments should not match, since argument was passed, but noArgs was set")
17+
}
18+
19+
e.noArgs = false
20+
if err := e.argsMatches(against); err != nil {
21+
t.Error("arguments should match, since argument was passed, but no expected args or noArgs was set")
1622
}
1723

1824
e.args = []driver.Value{5, "str"}

expectations_go18.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build go1.8
12
// +build go1.8
23

34
package sqlmock
@@ -30,7 +31,7 @@ func (e *ExpectedQuery) WillReturnRows(rows ...*Rows) *ExpectedQuery {
3031

3132
func (e *queryBasedExpectation) argsMatches(args []driver.NamedValue) error {
3233
if nil == e.args {
33-
if len(args) > 0 {
34+
if e.noArgs && len(args) > 0 {
3435
return fmt.Errorf("expected 0, but got %d arguments", len(args))
3536
}
3637
return nil

expectations_go18_test.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build go1.8
12
// +build go1.8
23

34
package sqlmock
@@ -10,10 +11,15 @@ import (
1011
)
1112

1213
func TestQueryExpectationArgComparison(t *testing.T) {
13-
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter}
14+
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter, noArgs: true}
1415
against := []driver.NamedValue{{Value: int64(5), Ordinal: 1}}
1516
if err := e.argsMatches(against); err == nil {
16-
t.Error("arguments should not match, since no expectation was set, but argument was passed")
17+
t.Error("arguments should not match, since argument was passed, but noArgs was set")
18+
}
19+
20+
e.noArgs = false
21+
if err := e.argsMatches(against); err != nil {
22+
t.Error("arguments should match, since argument was passed, but no expected args or noArgs was set")
1723
}
1824

1925
e.args = []driver.Value{5, "str"}
@@ -102,10 +108,15 @@ func TestQueryExpectationArgComparisonBool(t *testing.T) {
102108
}
103109

104110
func TestQueryExpectationNamedArgComparison(t *testing.T) {
105-
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter}
111+
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter, noArgs: true}
106112
against := []driver.NamedValue{{Value: int64(5), Name: "id"}}
107113
if err := e.argsMatches(against); err == nil {
108-
t.Errorf("arguments should not match, since no expectation was set, but argument was passed")
114+
t.Error("arguments should not match, since argument was passed, but noArgs was set")
115+
}
116+
117+
e.noArgs = false
118+
if err := e.argsMatches(against); err != nil {
119+
t.Error("arguments should match, since argument was passed, but no expected args or noArgs was set")
109120
}
110121

111122
e.args = []driver.Value{

expectations_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,29 @@ func TestCustomValueConverterQueryScan(t *testing.T) {
102102
}
103103
}
104104

105+
func TestQueryWithNoArgsAndWithArgsPanic(t *testing.T) {
106+
defer func() {
107+
if r := recover(); r != nil {
108+
return
109+
}
110+
t.Error("Expected panic for using WithArgs and ExpectNoArgs together")
111+
}()
112+
mock := &sqlmock{}
113+
mock.ExpectQuery("SELECT (.+) FROM user").WithArgs("John").WithoutArgs()
114+
}
115+
116+
func TestExecWithNoArgsAndWithArgsPanic(t *testing.T) {
117+
defer func() {
118+
if r := recover(); r != nil {
119+
return
120+
}
121+
t.Error("Expected panic for using WithArgs and ExpectNoArgs together")
122+
}()
123+
mock := &sqlmock{}
124+
mock.ExpectExec("^INSERT INTO user").WithArgs("John").WithoutArgs()
125+
}
126+
127+
105128
func TestQueryWillReturnsNil(t *testing.T) {
106129
t.Parallel()
107130

@@ -122,5 +145,4 @@ func TestQueryWillReturnsNil(t *testing.T) {
122145
_, err = mock.(*sqlmock).Query(query, []driver.Value{"test"})
123146
if err != nil {
124147
t.Error(err)
125-
}
126148
}

options.go

Lines changed: 6 additions & 3 deletions
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

query.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ func (f QueryMatcherFunc) Match(expectedSQL, actualSQL string) error {
4242
// QueryMatcherRegexp is the default SQL query matcher
4343
// used by sqlmock. It parses expectedSQL to a regular
4444
// expression and attempts to match actualSQL.
45-
var QueryMatcherRegexp QueryMatcher = QueryMatcherFunc(func(expectedSQL, actualSQL string) error {
45+
var QueryMatcherRegexp QueryMatcher = QueryMatcherFunc(func(expectedSQL, actualSQL string) error {
4646
expect := stripQuery(expectedSQL)
4747
actual := stripQuery(actualSQL)
48+
if actual != "" && expect == "" {
49+
return fmt.Errorf("expectedSQL can't be empty")
50+
}
4851
re, err := regexp.Compile(expect)
4952
if err != nil {
5053
return err

0 commit comments

Comments
 (0)