1
0
mirror of https://github.com/DATA-DOG/go-sqlmock.git synced 2025-02-09 13:13:54 +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
No known key found for this signature in database
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.

View File

@ -1,3 +1,4 @@
//go:build !go1.8
// +build !go1.8
package sqlmock
@ -17,7 +18,7 @@ func (e *ExpectedQuery) WillReturnRows(rows *Rows) *ExpectedQuery {
func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
if nil == e.args {
if len(args) > 0 {
if e.noArgs && len(args) > 0 {
return fmt.Errorf("expected 0, but got %d arguments", len(args))
}
return nil

View File

@ -1,3 +1,4 @@
//go:build !go1.8
// +build !go1.8
package sqlmock
@ -9,10 +10,15 @@ import (
)
func TestQueryExpectationArgComparison(t *testing.T) {
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter}
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter, noArgs: true}
against := []namedValue{{Value: int64(5), Ordinal: 1}}
if err := e.argsMatches(against); err == nil {
t.Error("arguments should not match, since no expectation was set, but argument was passed")
t.Error("arguments should not match, since argument was passed, but noArgs was set")
}
e.noArgs = false
if err := e.argsMatches(against); err != nil {
t.Error("arguments should match, since argument was passed, but no expected args or noArgs was set")
}
e.args = []driver.Value{5, "str"}

View File

@ -1,3 +1,4 @@
//go:build go1.8
// +build go1.8
package sqlmock
@ -30,7 +31,7 @@ func (e *ExpectedQuery) WillReturnRows(rows ...*Rows) *ExpectedQuery {
func (e *queryBasedExpectation) argsMatches(args []driver.NamedValue) error {
if nil == e.args {
if len(args) > 0 {
if e.noArgs && len(args) > 0 {
return fmt.Errorf("expected 0, but got %d arguments", len(args))
}
return nil

View File

@ -1,3 +1,4 @@
//go:build go1.8
// +build go1.8
package sqlmock
@ -10,10 +11,15 @@ import (
)
func TestQueryExpectationArgComparison(t *testing.T) {
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter}
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter, noArgs: true}
against := []driver.NamedValue{{Value: int64(5), Ordinal: 1}}
if err := e.argsMatches(against); err == nil {
t.Error("arguments should not match, since no expectation was set, but argument was passed")
t.Error("arguments should not match, since argument was passed, but noArgs was set")
}
e.noArgs = false
if err := e.argsMatches(against); err != nil {
t.Error("arguments should match, since argument was passed, but no expected args or noArgs was set")
}
e.args = []driver.Value{5, "str"}
@ -102,10 +108,15 @@ func TestQueryExpectationArgComparisonBool(t *testing.T) {
}
func TestQueryExpectationNamedArgComparison(t *testing.T) {
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter}
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter, noArgs: true}
against := []driver.NamedValue{{Value: int64(5), Name: "id"}}
if err := e.argsMatches(against); err == nil {
t.Errorf("arguments should not match, since no expectation was set, but argument was passed")
t.Error("arguments should not match, since argument was passed, but noArgs was set")
}
e.noArgs = false
if err := e.argsMatches(against); err != nil {
t.Error("arguments should match, since argument was passed, but no expected args or noArgs was set")
}
e.args = []driver.Value{

View File

@ -101,3 +101,25 @@ func TestCustomValueConverterQueryScan(t *testing.T) {
t.Error(err)
}
}
func TestQueryWithNoArgsAndWithArgsPanic(t *testing.T) {
defer func() {
if r := recover(); r != nil {
return
}
t.Error("Expected panic for using WithArgs and ExpectNoArgs together")
}()
mock := &sqlmock{}
mock.ExpectQuery("SELECT (.+) FROM user").WithArgs("John").WithoutArgs()
}
func TestExecWithNoArgsAndWithArgsPanic(t *testing.T) {
defer func() {
if r := recover(); r != nil {
return
}
t.Error("Expected panic for using WithArgs and ExpectNoArgs together")
}()
mock := &sqlmock{}
mock.ExpectExec("^INSERT INTO user").WithArgs("John").WithoutArgs()
}

View File

@ -1,3 +1,4 @@
//go:build go1.8
// +build go1.8
package sqlmock
@ -437,7 +438,6 @@ func TestContextExecErrorDelay(t *testing.T) {
// test that return of error is delayed
var delay time.Duration = 100 * time.Millisecond
mock.ExpectExec("^INSERT INTO articles").
WithArgs("hello").
WillReturnError(errors.New("slow fail")).
WillDelayFor(delay)

View File

@ -749,6 +749,16 @@ func TestRunExecsWithExpectedErrorMeetsExpectations(t *testing.T) {
}
}
func TestRunExecsWithNoArgsExpectedMeetsExpectations(t *testing.T) {
db, dbmock, _ := New()
dbmock.ExpectExec("THE FIRST EXEC").WithoutArgs().WillReturnResult(NewResult(0, 0))
_, err := db.Exec("THE FIRST EXEC", "foobar")
if err == nil {
t.Fatalf("expected error, but there wasn't any")
}
}
func TestRunQueryWithExpectedErrorMeetsExpectations(t *testing.T) {
db, dbmock, _ := New()
dbmock.ExpectQuery("THE FIRST QUERY").WillReturnError(fmt.Errorf("big bad bug"))
@ -959,7 +969,7 @@ func TestPrepareExec(t *testing.T) {
mock.ExpectBegin()
ep := mock.ExpectPrepare("INSERT INTO ORDERS\\(ID, STATUS\\) VALUES \\(\\?, \\?\\)")
for i := 0; i < 3; i++ {
ep.ExpectExec().WithArgs(i, "Hello"+strconv.Itoa(i)).WillReturnResult(NewResult(1, 1))
ep.ExpectExec().WillReturnResult(NewResult(1, 1))
}
mock.ExpectCommit()
tx, _ := db.Begin()
@ -1073,7 +1083,7 @@ func TestPreparedStatementCloseExpectation(t *testing.T) {
defer db.Close()
ep := mock.ExpectPrepare("INSERT INTO ORDERS").WillBeClosed()
ep.ExpectExec().WithArgs(1, "Hello").WillReturnResult(NewResult(1, 1))
ep.ExpectExec().WillReturnResult(NewResult(1, 1))
stmt, err := db.Prepare("INSERT INTO ORDERS(ID, STATUS) VALUES (?, ?)")
if err != nil {
@ -1104,7 +1114,6 @@ func TestExecExpectationErrorDelay(t *testing.T) {
// test that return of error is delayed
var delay time.Duration = 100 * time.Millisecond
mock.ExpectExec("^INSERT INTO articles").
WithArgs("hello").
WillReturnError(errors.New("slow fail")).
WillDelayFor(delay)
@ -1230,10 +1239,10 @@ func Test_sqlmock_Prepare_and_Exec(t *testing.T) {
mock.ExpectPrepare("SELECT (.+) FROM users WHERE (.+)")
expected := NewResult(1, 1)
mock.ExpectExec("SELECT (.+) FROM users WHERE (.+)").WithArgs("test").
mock.ExpectExec("SELECT (.+) FROM users WHERE (.+)").
WillReturnResult(expected)
expectedRows := mock.NewRows([]string{"id", "name", "email"}).AddRow(1, "test", "test@example.com")
mock.ExpectQuery("SELECT (.+) FROM users WHERE (.+)").WithArgs("test").WillReturnRows(expectedRows)
mock.ExpectQuery("SELECT (.+) FROM users WHERE (.+)").WillReturnRows(expectedRows)
got, err := mock.(*sqlmock).Prepare(query)
if err != nil {
@ -1326,7 +1335,7 @@ func Test_sqlmock_Query(t *testing.T) {
}
defer db.Close()
expectedRows := mock.NewRows([]string{"id", "name", "email"}).AddRow(1, "test", "test@example.com")
mock.ExpectQuery("SELECT (.+) FROM users WHERE (.+)").WithArgs("test").WillReturnRows(expectedRows)
mock.ExpectQuery("SELECT (.+) FROM users WHERE (.+)").WillReturnRows(expectedRows)
query := "SELECT name, email FROM users WHERE name = ?"
rows, err := mock.(*sqlmock).Query(query, []driver.Value{"test"})
if err != nil {
@ -1340,3 +1349,19 @@ func Test_sqlmock_Query(t *testing.T) {
return
}
}
func Test_sqlmock_QueryExpectWithoutArgs(t *testing.T) {
db, mock, err := New()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()
expectedRows := mock.NewRows([]string{"id", "name", "email"}).AddRow(1, "test", "test@example.com")
mock.ExpectQuery("SELECT (.+) FROM users WHERE (.+)").WillReturnRows(expectedRows).WithoutArgs()
query := "SELECT name, email FROM users WHERE name = ?"
_, err = mock.(*sqlmock).Query(query, []driver.Value{"test"})
if err == nil {
t.Errorf("error expected")
return
}
}