mirror of
https://github.com/zhashkevych/go-sqlxmock.git
synced 2024-11-24 08:12:13 +02:00
88 lines
2.5 KiB
Go
88 lines
2.5 KiB
Go
package sqlmock
|
|
|
|
import (
|
|
"fmt"
|
|
"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")
|
|
}
|
|
|
|
func TestQueryMatcherRegexp(t *testing.T) {
|
|
type testCase struct {
|
|
expected string
|
|
actual string
|
|
err error
|
|
}
|
|
|
|
cases := []testCase{
|
|
{"?\\l", "SEL", fmt.Errorf("error parsing regexp: missing argument to repetition operator: `?`")},
|
|
{"SELECT (.+) FROM users", "SELECT name, email FROM users WHERE id = ?", nil},
|
|
{"Select (.+) FROM users", "SELECT name, email FROM users WHERE id = ?", fmt.Errorf(`could not match actual sql: "SELECT name, email FROM users WHERE id = ?" with expected regexp "Select (.+) FROM users"`)},
|
|
{"SELECT (.+) FROM\nusers", "SELECT name, email\n FROM users\n WHERE id = ?", nil},
|
|
}
|
|
|
|
for i, c := range cases {
|
|
err := QueryMatcherRegexp.Match(c.expected, c.actual)
|
|
if err == nil && c.err != nil {
|
|
t.Errorf(`got no error, but expected "%v" at %d case`, c.err, i)
|
|
continue
|
|
}
|
|
if err != nil && c.err == nil {
|
|
t.Errorf(`got unexpected error "%v" at %d case`, err, i)
|
|
continue
|
|
}
|
|
if err == nil {
|
|
continue
|
|
}
|
|
if err.Error() != c.err.Error() {
|
|
t.Errorf(`expected error "%v", but got "%v" at %d case`, c.err, err, i)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestQueryMatcherEqual(t *testing.T) {
|
|
type testCase struct {
|
|
expected string
|
|
actual string
|
|
err error
|
|
}
|
|
|
|
cases := []testCase{
|
|
{"SELECT name, email FROM users WHERE id = ?", "SELECT name, email\n FROM users\n WHERE id = ?", nil},
|
|
{"SELECT", "Select", fmt.Errorf(`actual sql: "Select" does not equal to expected "SELECT"`)},
|
|
{"SELECT from users", "SELECT from table", fmt.Errorf(`actual sql: "SELECT from table" does not equal to expected "SELECT from users"`)},
|
|
}
|
|
|
|
for i, c := range cases {
|
|
err := QueryMatcherEqual.Match(c.expected, c.actual)
|
|
if err == nil && c.err != nil {
|
|
t.Errorf(`got no error, but expected "%v" at %d case`, c.err, i)
|
|
continue
|
|
}
|
|
if err != nil && c.err == nil {
|
|
t.Errorf(`got unexpected error "%v" at %d case`, err, i)
|
|
continue
|
|
}
|
|
if err == nil {
|
|
continue
|
|
}
|
|
if err.Error() != c.err.Error() {
|
|
t.Errorf(`expected error "%v", but got "%v" at %d case`, c.err, err, i)
|
|
}
|
|
}
|
|
}
|