forked from DATA-DOG/go-sqlmock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expectations.go
106 lines (91 loc) · 1.92 KB
/
expectations.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
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
}