DATA-DOG / go-sqlmock

Sql mock driver for golang to test database interactions
Other
6.16k stars 407 forks source link

fix: make no argument passed validation opt-in #329

Closed IvoGoman closed 10 months ago

IvoGoman commented 11 months ago

https://github.com/DATA-DOG/go-sqlmock/pull/295 introduced stricter checking of query arguments. This results in new test failures when using e.g. ExpectedQuery or ExpectedExec without WithArgs(..) and the actual query passes arguments. This case of no expected arguments but actual arguments passed was previously not caught.

However, there is also the case where queries are matched via QueryMatcherRegexp and there is no intention to strictly validate the actual arguments. This case broke with the above mentioned change and requires rather cumbersome fixing as such:

// this would fail with arguments do not match: expected 0, but got 6 arguments
mock.ExpectExec("INSERT .*foobar::foobar.*").WillReturnResult(sqlmock.NewResult(0, 1))
...
// this would be the fix
mock.ExpectExec("INSERT .*foobar::foobar*").WithArgs(sqlmock.AnyArg(), sqlmock.AnyArg(), sqlmock.AnyArg(), sqlmock.AnyArg(), sqlmock.AnyArg(), sqlmock.AnyArg()).WillReturnResult(sqlmock.NewResult(0, 1))

In order to allow for both strict and relaxed matching of the expectations this change adds WithoutArgs(). This new Method on ExpectedQuery and ExpectedExec allows to strictly ensure that the actual query does not pass any arguments. This reverts the strict argument checking introduced with https://github.com/DATA-DOG/go-sqlmock/pull/295 and makes this behavior opt-in. Furthermore, a test will panic if both WithArgs(..) and WithoutArgs() are used together.

IvoGoman commented 11 months ago

@filikos @JessieAMorris could you kindly have a look at this PR?