mirror of
https://github.com/DATA-DOG/go-sqlmock.git
synced 2025-04-07 06:50:00 +02:00
mention about escaping characters for regular expressions #6
This commit is contained in:
parent
db5b4c9deb
commit
c8ac9dfd29
11
README.md
11
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**
|
||||
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user