-
-
Notifications
You must be signed in to change notification settings - Fork 407
/
query_test.go
124 lines (109 loc) · 3.36 KB
/
query_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package sqlmock
import (
"fmt"
"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 {
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},
{"","SELECT from table", fmt.Errorf(`expectedSQL can't be empty`)},
}
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)
}
}
}