1
0
mirror of https://github.com/zhashkevych/go-sqlxmock.git synced 2025-06-12 21:47:29 +02:00

use configured QueryMatcher in order to match expected SQL to actual, closes #70

This commit is contained in:
gedi
2018-12-11 14:56:33 +02:00
parent 2a15d9c09b
commit a6e6646ad9
6 changed files with 100 additions and 65 deletions

View File

@ -5,6 +5,42 @@ import (
"testing"
)
func ExampleQueryMatcher() {
// configure to use case sensitive SQL query matcher
// instead of default regular expression matcher
db, mock, err := New(QueryMatcherOption(QueryMatcherEqual))
if err != nil {
fmt.Println("failed to open sqlmock database:", err)
}
defer db.Close()
rows := NewRows([]string{"id", "title"}).
AddRow(1, "one").
AddRow(2, "two")
mock.ExpectQuery("SELECT * FROM users").WillReturnRows(rows)
rs, err := db.Query("SELECT * FROM users")
if err != nil {
fmt.Println("failed to match expected query")
return
}
defer rs.Close()
for rs.Next() {
var id int
var title string
rs.Scan(&id, &title)
fmt.Println("scanned id:", id, "and title:", title)
}
if rs.Err() != nil {
fmt.Println("got rows error:", rs.Err())
}
// Output: scanned id: 1 and title: one
// scanned id: 2 and title: two
}
func TestQueryStringStripping(t *testing.T) {
assert := func(actual, expected string) {
if res := stripQuery(actual); res != expected {