mirror of
https://github.com/DATA-DOG/go-sqlmock.git
synced 2025-03-21 21:07:11 +02:00
update and test query whitespace stripping
This commit is contained in:
parent
6f1be66c43
commit
af59e07d27
@ -116,4 +116,3 @@ func (c *conn) Query(query string, args []driver.Value) (driver.Rows, error) {
|
|||||||
|
|
||||||
return eq.rows, nil
|
return eq.rows, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,4 +101,3 @@ type expectedExec struct {
|
|||||||
|
|
||||||
result driver.Result
|
result driver.Result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
16
util.go
16
util.go
@ -1,11 +1,17 @@
|
|||||||
package sqlmock
|
package sqlmock
|
||||||
|
|
||||||
import "strings"
|
import (
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var re *regexp.Regexp
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
re = regexp.MustCompile("\\s+")
|
||||||
|
}
|
||||||
|
|
||||||
// strip out new lines and trim spaces
|
// strip out new lines and trim spaces
|
||||||
func stripQuery(q string) (s string) {
|
func stripQuery(q string) (s string) {
|
||||||
s = strings.Replace(q, "\n", " ", -1)
|
return strings.TrimSpace(re.ReplaceAllString(q, " "))
|
||||||
s = strings.Replace(s, "\r", "", -1)
|
|
||||||
s = strings.TrimSpace(s)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
21
util_test.go
Normal file
21
util_test.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package sqlmock
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestQueryStringStripping(t *testing.T) {
|
||||||
|
assert := func(actual, expected string) {
|
||||||
|
if res := stripQuery(actual); res != expected {
|
||||||
|
t.Errorf("Expected '%s' to be '%s', but got '%s'", actual, expected, res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(" SELECT 1", "SELECT 1")
|
||||||
|
assert("SELECT 1 FROM d", "SELECT 1 FROM d")
|
||||||
|
assert(`
|
||||||
|
SELECT c
|
||||||
|
FROM D
|
||||||
|
`, "SELECT c FROM D")
|
||||||
|
assert("UPDATE (.+) SET ", "UPDATE (.+) SET")
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user