mirror of
https://github.com/zhashkevych/go-sqlxmock.git
synced 2024-11-24 08:12:13 +02:00
52242da115
sometimes it's impossible to anticipate the args (such as in an insert), sometimes args don't need to be checked
107 lines
1.9 KiB
Go
107 lines
1.9 KiB
Go
package sqlmock
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"reflect"
|
|
"regexp"
|
|
)
|
|
|
|
// an expectation interface
|
|
type expectation interface {
|
|
fulfilled() bool
|
|
setError(err error)
|
|
}
|
|
|
|
// common expectation struct
|
|
// satisfies the expectation interface
|
|
type commonExpectation struct {
|
|
triggered bool
|
|
err error
|
|
}
|
|
|
|
func (e *commonExpectation) fulfilled() bool {
|
|
return e.triggered
|
|
}
|
|
|
|
func (e *commonExpectation) setError(err error) {
|
|
e.err = err
|
|
}
|
|
|
|
// query based expectation
|
|
// adds a query matching logic
|
|
type queryBasedExpectation struct {
|
|
commonExpectation
|
|
sqlRegex *regexp.Regexp
|
|
args []driver.Value
|
|
}
|
|
|
|
func (e *queryBasedExpectation) queryMatches(sql string) bool {
|
|
return e.sqlRegex.MatchString(sql)
|
|
}
|
|
|
|
func (e *queryBasedExpectation) argsMatches(args []driver.Value) bool {
|
|
if nil == e.args {
|
|
return true
|
|
}
|
|
if len(args) != len(e.args) {
|
|
return false
|
|
}
|
|
for k, v := range args {
|
|
vi := reflect.ValueOf(v)
|
|
ai := reflect.ValueOf(e.args[k])
|
|
switch vi.Kind() {
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
if vi.Int() != ai.Int() {
|
|
return false
|
|
}
|
|
case reflect.Float32, reflect.Float64:
|
|
if vi.Float() != ai.Float() {
|
|
return false
|
|
}
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
if vi.Uint() != ai.Uint() {
|
|
return false
|
|
}
|
|
case reflect.String:
|
|
if vi.String() != ai.String() {
|
|
return false
|
|
}
|
|
default:
|
|
// compare types like time.Time based on type only
|
|
if vi.Kind() != ai.Kind() {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// begin transaction
|
|
type expectedBegin struct {
|
|
commonExpectation
|
|
}
|
|
|
|
// tx commit
|
|
type expectedCommit struct {
|
|
commonExpectation
|
|
}
|
|
|
|
// tx rollback
|
|
type expectedRollback struct {
|
|
commonExpectation
|
|
}
|
|
|
|
// query expectation
|
|
type expectedQuery struct {
|
|
queryBasedExpectation
|
|
|
|
rows driver.Rows
|
|
}
|
|
|
|
// exec query expectation
|
|
type expectedExec struct {
|
|
queryBasedExpectation
|
|
|
|
result driver.Result
|
|
}
|