diff --git a/README.md b/README.md index d39102a..6f9de5c 100644 --- a/README.md +++ b/README.md @@ -277,7 +277,16 @@ Instead of result we can return error.. ``` go sqlmock.ExpectQuery("SELECT (.*) FROM orders"). WithArgs("string value"). - WillReturnResult(sqlmock.NewResult(0, 1)) + WillReturnRows(sqlmock.NewRows([]string{"col"}).AddRow("val")) +``` + +**NOTE:** it matches a regular expression. Some regex special characters must be escaped if you want to match them. +For example if we want to match a subselect: + +``` go +sqlmock.ExpectQuery("SELECT (.*) FROM orders WHERE id IN \\(SELECT id FROM finished WHERE status = 1\\)"). + WithArgs("string value"). + WillReturnRows(sqlmock.NewRows([]string{"col"}).AddRow("val")) ``` **WithArgs** expectation, compares values based on their type, for usual values like **string, float, int** diff --git a/expectations_test.go b/expectations_test.go index 2ba1664..5bcccd3 100644 --- a/expectations_test.go +++ b/expectations_test.go @@ -2,6 +2,7 @@ package sqlmock import ( "database/sql/driver" + "regexp" "testing" "time" ) @@ -57,3 +58,16 @@ func TestQueryExpectationArgComparison(t *testing.T) { t.Error("arguments should match, but it did not") } } + +func TestQueryExpectationSqlMatch(t *testing.T) { + e := &expectedExec{} + e.sqlRegex = regexp.MustCompile("SELECT x FROM") + if !e.queryMatches("SELECT x FROM someting") { + t.Errorf("Sql must have matched the query") + } + + e.sqlRegex = regexp.MustCompile("SELECT COUNT\\(x\\) FROM") + if !e.queryMatches("SELECT COUNT(x) FROM someting") { + t.Errorf("Sql must have matched the query") + } +}