forked from chrisyxlee/pgxpoolmock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
query_contains.go
36 lines (29 loc) · 1007 Bytes
/
query_contains.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
package pgxpoolmock
import (
"fmt"
"regexp"
"github.com/golang/mock/gomock"
)
// QueryContainsMatcher implements the gomock matcher interface
// (https://pkg.go.dev/github.com/golang/mock/gomock#Matcher) to match a string (SQL query) that
// contains the given string. This is useful for passing into pgxpoolmock.MockPgxIface as the
// SQL query string argument since SQLC generated code will contain the associated function
// name as well.
//
// Usage: pgxMock.EXPECT().QueryRow(gomock.Any(), QueryContains("GetSomething")).Return(NewRow(1, "foo")).
type QueryContainsMatcher struct{ re *regexp.Regexp }
func QueryContains(s string) gomock.Matcher {
return &QueryContainsMatcher{
re: regexp.MustCompile(fmt.Sprintf(".*%s.*", s)),
}
}
func (m *QueryContainsMatcher) Matches(x interface{}) bool {
str, ok := x.(string)
if !ok {
return false
}
return m.re.MatchString(str)
}
func (m *QueryContainsMatcher) String() string {
return fmt.Sprintf("matches the regexp `%s`", m.re.String())
}