You've already forked go-sqlmock
mirror of
https://github.com/DATA-DOG/go-sqlmock.git
synced 2025-06-25 00:26:45 +02:00
add custom argument matching
This commit is contained in:
@ -6,6 +6,12 @@ import (
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// Argument interface allows to match
|
||||
// any argument in specific way
|
||||
type Argument interface {
|
||||
Match(driver.Value) bool
|
||||
}
|
||||
|
||||
// an expectation interface
|
||||
type expectation interface {
|
||||
fulfilled() bool
|
||||
@ -47,6 +53,13 @@ func (e *queryBasedExpectation) argsMatches(args []driver.Value) bool {
|
||||
return false
|
||||
}
|
||||
for k, v := range args {
|
||||
matcher, ok := e.args[k].(Argument)
|
||||
if ok {
|
||||
if !matcher.Match(v) {
|
||||
return false
|
||||
}
|
||||
continue
|
||||
}
|
||||
vi := reflect.ValueOf(v)
|
||||
ai := reflect.ValueOf(e.args[k])
|
||||
switch vi.Kind() {
|
||||
|
@ -6,6 +6,13 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type matcher struct {
|
||||
}
|
||||
|
||||
func (m matcher) Match(driver.Value) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func TestQueryExpectationArgComparison(t *testing.T) {
|
||||
e := &queryBasedExpectation{}
|
||||
against := []driver.Value{5}
|
||||
@ -44,4 +51,9 @@ func TestQueryExpectationArgComparison(t *testing.T) {
|
||||
if !e.argsMatches(against) {
|
||||
t.Error("arguments should match (time will be compared only by type), but it did not")
|
||||
}
|
||||
|
||||
against = []driver.Value{5, matcher{}}
|
||||
if !e.argsMatches(against) {
|
||||
t.Error("arguments should match, but it did not")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user