-
-
Notifications
You must be signed in to change notification settings - Fork 408
fix: make no argument passed validation opt-in #329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", "[email protected]") | ||
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", "[email protected]") | ||
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", "[email protected]") | ||
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 | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the name
noArgs
is internal, lets adjust the error msg: The users should know right away ifWithoutArgs()
orWithArgs()
is being used correctly: Lets adjust the error to something like:received arguments *all arguments key+value* but expected no arguments. Expect the query to be
WithoutArgs()`There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These particular test cases do not call
WithoutArgs()
orWithArgs()
. Only the internalqueryBasedExpectation
is manipulated to test the different behaviours.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyways, I will make a proposal to adjust many print statements since we can do some improvements on the UX of the library. Release will follow very soon