mirror of
https://github.com/DATA-DOG/go-sqlmock.git
synced 2025-03-31 21:45:15 +02:00
Merge pull request #67 from ahmadmuzakki29/master
apply stripQuery before assigning query expectation
This commit is contained in:
commit
55ecc5a333
@ -113,6 +113,11 @@ func TestBuildQuery(t *testing.T){
|
|||||||
address,
|
address,
|
||||||
anotherfield
|
anotherfield
|
||||||
FROM user
|
FROM user
|
||||||
|
where
|
||||||
|
name = 'John'
|
||||||
|
and
|
||||||
|
address = 'Jakarta'
|
||||||
|
|
||||||
`
|
`
|
||||||
|
|
||||||
mock.ExpectQuery(query)
|
mock.ExpectQuery(query)
|
||||||
|
@ -255,7 +255,7 @@ func (c *sqlmock) Exec(query string, args []driver.Value) (res driver.Result, er
|
|||||||
|
|
||||||
func (c *sqlmock) ExpectExec(sqlRegexStr string) *ExpectedExec {
|
func (c *sqlmock) ExpectExec(sqlRegexStr string) *ExpectedExec {
|
||||||
e := &ExpectedExec{}
|
e := &ExpectedExec{}
|
||||||
sqlRegexStr = buildQuery(sqlRegexStr)
|
sqlRegexStr = stripQuery(sqlRegexStr)
|
||||||
e.sqlRegex = regexp.MustCompile(sqlRegexStr)
|
e.sqlRegex = regexp.MustCompile(sqlRegexStr)
|
||||||
c.expected = append(c.expected, e)
|
c.expected = append(c.expected, e)
|
||||||
return e
|
return e
|
||||||
@ -302,7 +302,7 @@ func (c *sqlmock) Prepare(query string) (driver.Stmt, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *sqlmock) ExpectPrepare(sqlRegexStr string) *ExpectedPrepare {
|
func (c *sqlmock) ExpectPrepare(sqlRegexStr string) *ExpectedPrepare {
|
||||||
sqlRegexStr = buildQuery(sqlRegexStr)
|
sqlRegexStr = stripQuery(sqlRegexStr)
|
||||||
e := &ExpectedPrepare{sqlRegex: regexp.MustCompile(sqlRegexStr), mock: c}
|
e := &ExpectedPrepare{sqlRegex: regexp.MustCompile(sqlRegexStr), mock: c}
|
||||||
c.expected = append(c.expected, e)
|
c.expected = append(c.expected, e)
|
||||||
return e
|
return e
|
||||||
@ -371,7 +371,7 @@ func (c *sqlmock) Query(query string, args []driver.Value) (rw driver.Rows, err
|
|||||||
|
|
||||||
func (c *sqlmock) ExpectQuery(sqlRegexStr string) *ExpectedQuery {
|
func (c *sqlmock) ExpectQuery(sqlRegexStr string) *ExpectedQuery {
|
||||||
e := &ExpectedQuery{}
|
e := &ExpectedQuery{}
|
||||||
sqlRegexStr = buildQuery(sqlRegexStr)
|
sqlRegexStr = stripQuery(sqlRegexStr)
|
||||||
e.sqlRegex = regexp.MustCompile(sqlRegexStr)
|
e.sqlRegex = regexp.MustCompile(sqlRegexStr)
|
||||||
c.expected = append(c.expected, e)
|
c.expected = append(c.expected, e)
|
||||||
return e
|
return e
|
||||||
|
@ -3,17 +3,17 @@ package sqlmock
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
"github.com/golang/go/src/pkg/strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func cancelOrder(db *sql.DB, orderID int) error {
|
func cancelOrder(db *sql.DB, orderID int) error {
|
||||||
tx, _ := db.Begin()
|
tx, _ := db.Begin()
|
||||||
_, _ = tx.Query("SELECT * FROM orders {0} FOR UPDATE", orderID)
|
_, _ = tx.Query("SELECT * FROM orders {0} FOR UPDATE", orderID)
|
||||||
err := tx.Rollback()
|
err := tx.Rollback()
|
||||||
if (err != nil) {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
11
util.go
11
util.go
@ -11,14 +11,3 @@ var re = regexp.MustCompile("\\s+")
|
|||||||
func stripQuery(q string) (s string) {
|
func stripQuery(q string) (s string) {
|
||||||
return strings.TrimSpace(re.ReplaceAllString(q, " "))
|
return strings.TrimSpace(re.ReplaceAllString(q, " "))
|
||||||
}
|
}
|
||||||
|
|
||||||
// mimicking how sql.DB build their queries
|
|
||||||
func buildQuery(q string)string{
|
|
||||||
q = strings.TrimSpace(q)
|
|
||||||
lines := strings.Split(q,"\n")
|
|
||||||
var newQuery string
|
|
||||||
for _,l := range lines{
|
|
||||||
newQuery = newQuery +" " +strings.TrimSpace(l)
|
|
||||||
}
|
|
||||||
return strings.TrimSpace(newQuery)
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user