1
0
mirror of https://github.com/DATA-DOG/go-sqlmock.git synced 2025-07-03 00:36:52 +02:00

fix: make no argument passed validation opt-in

This commit is contained in:
Ivo Gosemann
2023-12-11 17:33:56 +01:00
parent b2d135c5e4
commit a6a27b71b9
8 changed files with 114 additions and 15 deletions

View File

@ -134,11 +134,27 @@ type ExpectedQuery struct {
// 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.Argument interface can be used to match an argument.
// Must not be used together with WithoutArgs()
func (e *ExpectedQuery) WithArgs(args ...driver.Value) *ExpectedQuery {
if e.noArgs {
panic("WithArgs() and WithoutArgs() must not be used together")
}
e.args = args
return e
}
// WithoutArgs will ensure that no arguments are passed for this query.
// if at least one argument is passed, it will return an error. This allows
// for stricter validation of the query arguments.
// Must no be used together with WithArgs()
func (e *ExpectedQuery) WithoutArgs() *ExpectedQuery {
if len(e.args) > 0 {
panic("WithoutArgs() and WithArgs() must not be used together")
}
e.noArgs = true
return e
}
// RowsWillBeClosed expects this query rows to be closed.
func (e *ExpectedQuery) RowsWillBeClosed() *ExpectedQuery {
e.rowsMustBeClosed = true
@ -195,11 +211,27 @@ type ExpectedExec struct {
// WithArgs will match given expected args to actual database exec operation arguments.
// if at least one argument does not match, it will return an error. For specific
// arguments an sqlmock.Argument interface can be used to match an argument.
// Must not be used together with WithoutArgs()
func (e *ExpectedExec) WithArgs(args ...driver.Value) *ExpectedExec {
if len(e.args) > 0 {
panic("WithArgs() and WithoutArgs() must not be used together")
}
e.args = args
return e
}
// WithoutArgs will ensure that no args are passed for this expected database exec action.
// if at least one argument is passed, it will return an error. This allows for stricter
// validation of the query arguments.
// Must not be used together with WithArgs()
func (e *ExpectedExec) WithoutArgs() *ExpectedExec {
if len(e.args) > 0 {
panic("WithoutArgs() and WithArgs() must not be used together")
}
e.noArgs = true
return e
}
// WillReturnError allows to set an error for expected database exec action
func (e *ExpectedExec) WillReturnError(err error) *ExpectedExec {
e.err = err
@ -338,6 +370,7 @@ type queryBasedExpectation struct {
expectSQL string
converter driver.ValueConverter
args []driver.Value
noArgs bool // ensure no args are passed
}
// ExpectedPing is used to manage *sql.DB.Ping expectations.