mirror of
https://github.com/DATA-DOG/go-sqlmock.git
synced 2025-03-19 20:57:50 +02:00
Merge pull request #329 from IvoGoman/optionalNoArgs
fix: make no argument passed validation opt-in
This commit is contained in:
commit
13767dc13a
@ -134,11 +134,27 @@ type ExpectedQuery struct {
|
|||||||
// WithArgs will match given expected args to actual database query arguments.
|
// 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
|
// 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.
|
// 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 {
|
func (e *ExpectedQuery) WithArgs(args ...driver.Value) *ExpectedQuery {
|
||||||
|
if e.noArgs {
|
||||||
|
panic("WithArgs() and WithoutArgs() must not be used together")
|
||||||
|
}
|
||||||
e.args = args
|
e.args = args
|
||||||
return e
|
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.
|
// RowsWillBeClosed expects this query rows to be closed.
|
||||||
func (e *ExpectedQuery) RowsWillBeClosed() *ExpectedQuery {
|
func (e *ExpectedQuery) RowsWillBeClosed() *ExpectedQuery {
|
||||||
e.rowsMustBeClosed = true
|
e.rowsMustBeClosed = true
|
||||||
@ -195,11 +211,27 @@ type ExpectedExec struct {
|
|||||||
// WithArgs will match given expected args to actual database exec operation arguments.
|
// 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
|
// 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.
|
// 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 {
|
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
|
e.args = args
|
||||||
return e
|
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
|
// WillReturnError allows to set an error for expected database exec action
|
||||||
func (e *ExpectedExec) WillReturnError(err error) *ExpectedExec {
|
func (e *ExpectedExec) WillReturnError(err error) *ExpectedExec {
|
||||||
e.err = err
|
e.err = err
|
||||||
@ -338,6 +370,7 @@ type queryBasedExpectation struct {
|
|||||||
expectSQL string
|
expectSQL string
|
||||||
converter driver.ValueConverter
|
converter driver.ValueConverter
|
||||||
args []driver.Value
|
args []driver.Value
|
||||||
|
noArgs bool // ensure no args are passed
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExpectedPing is used to manage *sql.DB.Ping expectations.
|
// ExpectedPing is used to manage *sql.DB.Ping expectations.
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
//go:build !go1.8
|
||||||
// +build !go1.8
|
// +build !go1.8
|
||||||
|
|
||||||
package sqlmock
|
package sqlmock
|
||||||
@ -17,7 +18,7 @@ func (e *ExpectedQuery) WillReturnRows(rows *Rows) *ExpectedQuery {
|
|||||||
|
|
||||||
func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
|
func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
|
||||||
if nil == e.args {
|
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 fmt.Errorf("expected 0, but got %d arguments", len(args))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
//go:build !go1.8
|
||||||
// +build !go1.8
|
// +build !go1.8
|
||||||
|
|
||||||
package sqlmock
|
package sqlmock
|
||||||
@ -9,10 +10,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestQueryExpectationArgComparison(t *testing.T) {
|
func TestQueryExpectationArgComparison(t *testing.T) {
|
||||||
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter}
|
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter, noArgs: true}
|
||||||
against := []namedValue{{Value: int64(5), Ordinal: 1}}
|
against := []namedValue{{Value: int64(5), Ordinal: 1}}
|
||||||
if err := e.argsMatches(against); err == nil {
|
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"}
|
e.args = []driver.Value{5, "str"}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
//go:build go1.8
|
||||||
// +build go1.8
|
// +build go1.8
|
||||||
|
|
||||||
package sqlmock
|
package sqlmock
|
||||||
@ -30,7 +31,7 @@ func (e *ExpectedQuery) WillReturnRows(rows ...*Rows) *ExpectedQuery {
|
|||||||
|
|
||||||
func (e *queryBasedExpectation) argsMatches(args []driver.NamedValue) error {
|
func (e *queryBasedExpectation) argsMatches(args []driver.NamedValue) error {
|
||||||
if nil == e.args {
|
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 fmt.Errorf("expected 0, but got %d arguments", len(args))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
//go:build go1.8
|
||||||
// +build go1.8
|
// +build go1.8
|
||||||
|
|
||||||
package sqlmock
|
package sqlmock
|
||||||
@ -10,10 +11,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestQueryExpectationArgComparison(t *testing.T) {
|
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}}
|
against := []driver.NamedValue{{Value: int64(5), Ordinal: 1}}
|
||||||
if err := e.argsMatches(against); err == nil {
|
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"}
|
e.args = []driver.Value{5, "str"}
|
||||||
@ -102,10 +108,15 @@ func TestQueryExpectationArgComparisonBool(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestQueryExpectationNamedArgComparison(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"}}
|
against := []driver.NamedValue{{Value: int64(5), Name: "id"}}
|
||||||
if err := e.argsMatches(against); err == nil {
|
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{
|
e.args = []driver.Value{
|
||||||
|
@ -101,3 +101,25 @@ func TestCustomValueConverterQueryScan(t *testing.T) {
|
|||||||
t.Error(err)
|
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()
|
||||||
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
//go:build go1.8
|
||||||
// +build go1.8
|
// +build go1.8
|
||||||
|
|
||||||
package sqlmock
|
package sqlmock
|
||||||
@ -437,7 +438,6 @@ func TestContextExecErrorDelay(t *testing.T) {
|
|||||||
// test that return of error is delayed
|
// test that return of error is delayed
|
||||||
var delay time.Duration = 100 * time.Millisecond
|
var delay time.Duration = 100 * time.Millisecond
|
||||||
mock.ExpectExec("^INSERT INTO articles").
|
mock.ExpectExec("^INSERT INTO articles").
|
||||||
WithArgs("hello").
|
|
||||||
WillReturnError(errors.New("slow fail")).
|
WillReturnError(errors.New("slow fail")).
|
||||||
WillDelayFor(delay)
|
WillDelayFor(delay)
|
||||||
|
|
||||||
|
@ -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) {
|
func TestRunQueryWithExpectedErrorMeetsExpectations(t *testing.T) {
|
||||||
db, dbmock, _ := New()
|
db, dbmock, _ := New()
|
||||||
dbmock.ExpectQuery("THE FIRST QUERY").WillReturnError(fmt.Errorf("big bad bug"))
|
dbmock.ExpectQuery("THE FIRST QUERY").WillReturnError(fmt.Errorf("big bad bug"))
|
||||||
@ -959,7 +969,7 @@ func TestPrepareExec(t *testing.T) {
|
|||||||
mock.ExpectBegin()
|
mock.ExpectBegin()
|
||||||
ep := mock.ExpectPrepare("INSERT INTO ORDERS\\(ID, STATUS\\) VALUES \\(\\?, \\?\\)")
|
ep := mock.ExpectPrepare("INSERT INTO ORDERS\\(ID, STATUS\\) VALUES \\(\\?, \\?\\)")
|
||||||
for i := 0; i < 3; i++ {
|
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()
|
mock.ExpectCommit()
|
||||||
tx, _ := db.Begin()
|
tx, _ := db.Begin()
|
||||||
@ -1073,7 +1083,7 @@ func TestPreparedStatementCloseExpectation(t *testing.T) {
|
|||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
ep := mock.ExpectPrepare("INSERT INTO ORDERS").WillBeClosed()
|
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 (?, ?)")
|
stmt, err := db.Prepare("INSERT INTO ORDERS(ID, STATUS) VALUES (?, ?)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1104,7 +1114,6 @@ func TestExecExpectationErrorDelay(t *testing.T) {
|
|||||||
// test that return of error is delayed
|
// test that return of error is delayed
|
||||||
var delay time.Duration = 100 * time.Millisecond
|
var delay time.Duration = 100 * time.Millisecond
|
||||||
mock.ExpectExec("^INSERT INTO articles").
|
mock.ExpectExec("^INSERT INTO articles").
|
||||||
WithArgs("hello").
|
|
||||||
WillReturnError(errors.New("slow fail")).
|
WillReturnError(errors.New("slow fail")).
|
||||||
WillDelayFor(delay)
|
WillDelayFor(delay)
|
||||||
|
|
||||||
@ -1230,10 +1239,10 @@ func Test_sqlmock_Prepare_and_Exec(t *testing.T) {
|
|||||||
|
|
||||||
mock.ExpectPrepare("SELECT (.+) FROM users WHERE (.+)")
|
mock.ExpectPrepare("SELECT (.+) FROM users WHERE (.+)")
|
||||||
expected := NewResult(1, 1)
|
expected := NewResult(1, 1)
|
||||||
mock.ExpectExec("SELECT (.+) FROM users WHERE (.+)").WithArgs("test").
|
mock.ExpectExec("SELECT (.+) FROM users WHERE (.+)").
|
||||||
WillReturnResult(expected)
|
WillReturnResult(expected)
|
||||||
expectedRows := mock.NewRows([]string{"id", "name", "email"}).AddRow(1, "test", "test@example.com")
|
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)
|
got, err := mock.(*sqlmock).Prepare(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1326,7 +1335,7 @@ func Test_sqlmock_Query(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
expectedRows := mock.NewRows([]string{"id", "name", "email"}).AddRow(1, "test", "test@example.com")
|
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 = ?"
|
query := "SELECT name, email FROM users WHERE name = ?"
|
||||||
rows, err := mock.(*sqlmock).Query(query, []driver.Value{"test"})
|
rows, err := mock.(*sqlmock).Query(query, []driver.Value{"test"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1340,3 +1349,19 @@ func Test_sqlmock_Query(t *testing.T) {
|
|||||||
return
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user