mirror of
https://github.com/DATA-DOG/go-sqlmock.git
synced 2025-04-02 21:45:24 +02:00
- apply stripQuery before assigning query expectation
- remove unused function
This commit is contained in:
parent
9758862a89
commit
1fd67a1762
@ -113,6 +113,11 @@ func TestBuildQuery(t *testing.T){
|
||||
address,
|
||||
anotherfield
|
||||
FROM user
|
||||
where
|
||||
name = 'John'
|
||||
and
|
||||
address = 'Jakarta'
|
||||
|
||||
`
|
||||
|
||||
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 {
|
||||
e := &ExpectedExec{}
|
||||
sqlRegexStr = buildQuery(sqlRegexStr)
|
||||
sqlRegexStr = stripQuery(sqlRegexStr)
|
||||
e.sqlRegex = regexp.MustCompile(sqlRegexStr)
|
||||
c.expected = append(c.expected, e)
|
||||
return e
|
||||
@ -302,7 +302,7 @@ func (c *sqlmock) Prepare(query string) (driver.Stmt, error) {
|
||||
}
|
||||
|
||||
func (c *sqlmock) ExpectPrepare(sqlRegexStr string) *ExpectedPrepare {
|
||||
sqlRegexStr = buildQuery(sqlRegexStr)
|
||||
sqlRegexStr = stripQuery(sqlRegexStr)
|
||||
e := &ExpectedPrepare{sqlRegex: regexp.MustCompile(sqlRegexStr), mock: c}
|
||||
c.expected = append(c.expected, 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 {
|
||||
e := &ExpectedQuery{}
|
||||
sqlRegexStr = buildQuery(sqlRegexStr)
|
||||
sqlRegexStr = stripQuery(sqlRegexStr)
|
||||
e.sqlRegex = regexp.MustCompile(sqlRegexStr)
|
||||
c.expected = append(c.expected, e)
|
||||
return e
|
||||
|
@ -3,17 +3,17 @@ package sqlmock
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
"github.com/golang/go/src/pkg/strconv"
|
||||
)
|
||||
|
||||
func cancelOrder(db *sql.DB, orderID int) error {
|
||||
tx, _ := db.Begin()
|
||||
_, _ = tx.Query("SELECT * FROM orders {0} FOR UPDATE", orderID)
|
||||
err := tx.Rollback()
|
||||
if (err != nil) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
11
util.go
11
util.go
@ -11,14 +11,3 @@ var re = regexp.MustCompile("\\s+")
|
||||
func stripQuery(q string) (s string) {
|
||||
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