Skip to content

Commit

Permalink
Merge pull request #333 from sfc-gh-tvidyasankar/patch-1
Browse files Browse the repository at this point in the history
fix bug/334 when checking empty expectedQuery with actual query
  • Loading branch information
l3pp4rd authored Apr 3, 2024
2 parents 108200d + f53e637 commit 6622224
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
5 changes: 4 additions & 1 deletion query.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ func (f QueryMatcherFunc) Match(expectedSQL, actualSQL string) error {
// QueryMatcherRegexp is the default SQL query matcher
// used by sqlmock. It parses expectedSQL to a regular
// expression and attempts to match actualSQL.
var QueryMatcherRegexp QueryMatcher = QueryMatcherFunc(func(expectedSQL, actualSQL string) error {
var QueryMatcherRegexp QueryMatcher = QueryMatcherFunc(func(expectedSQL, actualSQL string) error {
expect := stripQuery(expectedSQL)
actual := stripQuery(actualSQL)
if actual != "" && expect == "" {
return fmt.Errorf("expectedSQL can't be empty")
}
re, err := regexp.Compile(expect)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func TestQueryMatcherRegexp(t *testing.T) {
{"SELECT (.+) FROM users", "SELECT name, email FROM users WHERE id = ?", nil},
{"Select (.+) FROM users", "SELECT name, email FROM users WHERE id = ?", fmt.Errorf(`could not match actual sql: "SELECT name, email FROM users WHERE id = ?" with expected regexp "Select (.+) FROM users"`)},
{"SELECT (.+) FROM\nusers", "SELECT name, email\n FROM users\n WHERE id = ?", nil},
{"","SELECT from table", fmt.Errorf(`expectedSQL can't be empty`)},
}

for i, c := range cases {
Expand Down

0 comments on commit 6622224

Please sign in to comment.