Skip to content

Commit 4c6f0e6

Browse files
committed
update formatting and readme
1 parent 27fabfa commit 4c6f0e6

File tree

4 files changed

+198
-197
lines changed

4 files changed

+198
-197
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ Visit [godoc](http://godoc.org/github.com/DATA-DOG/go-sqlmock)
320320

321321
## Changes
322322

323+
- **2014-05-29** allow to match arguments in more sophisticated ways, by providing an **sqlmock.Argument** interface
323324
- **2014-04-21** introduce **sqlmock.New()** to open a mock database connection for tests. This method
324325
calls sql.DB.Ping to ensure that connection is open, see [issue](https://github.com/DATA-DOG/go-sqlmock/issues/4).
325326
This way on Close it will surely assert if all expectations are met, even if database was not triggered at all.

connection.go

Lines changed: 89 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,118 @@
11
package sqlmock
22

33
import (
4-
"database/sql/driver"
5-
"fmt"
4+
"database/sql/driver"
5+
"fmt"
66
)
77

88
type conn struct {
9-
expectations []expectation
10-
active expectation
9+
expectations []expectation
10+
active expectation
1111
}
1212

1313
// Close a mock database driver connection. It should
1414
// be always called to ensure that all expectations
1515
// were met successfully. Returns error if there is any
1616
func (c *conn) Close() (err error) {
17-
for _, e := range mock.conn.expectations {
18-
if !e.fulfilled() {
19-
err = fmt.Errorf("there is a remaining expectation %T which was not matched yet", e)
20-
break
21-
}
22-
}
23-
mock.conn.expectations = []expectation{}
24-
mock.conn.active = nil
25-
return err
17+
for _, e := range mock.conn.expectations {
18+
if !e.fulfilled() {
19+
err = fmt.Errorf("there is a remaining expectation %T which was not matched yet", e)
20+
break
21+
}
22+
}
23+
mock.conn.expectations = []expectation{}
24+
mock.conn.active = nil
25+
return err
2626
}
2727

2828
func (c *conn) Begin() (driver.Tx, error) {
29-
e := c.next()
30-
if e == nil {
31-
return nil, fmt.Errorf("all expectations were already fulfilled, call to begin transaction was not expected")
32-
}
33-
34-
etb, ok := e.(*expectedBegin)
35-
if !ok {
36-
return nil, fmt.Errorf("call to begin transaction, was not expected, next expectation is %T as %+v", e, e)
37-
}
38-
etb.triggered = true
39-
return &transaction{c}, etb.err
29+
e := c.next()
30+
if e == nil {
31+
return nil, fmt.Errorf("all expectations were already fulfilled, call to begin transaction was not expected")
32+
}
33+
34+
etb, ok := e.(*expectedBegin)
35+
if !ok {
36+
return nil, fmt.Errorf("call to begin transaction, was not expected, next expectation is %T as %+v", e, e)
37+
}
38+
etb.triggered = true
39+
return &transaction{c}, etb.err
4040
}
4141

4242
// get next unfulfilled expectation
4343
func (c *conn) next() (e expectation) {
44-
for _, e = range c.expectations {
45-
if !e.fulfilled() {
46-
return
47-
}
48-
}
49-
return nil // all expectations were fulfilled
44+
for _, e = range c.expectations {
45+
if !e.fulfilled() {
46+
return
47+
}
48+
}
49+
return nil // all expectations were fulfilled
5050
}
5151

5252
func (c *conn) Exec(query string, args []driver.Value) (driver.Result, error) {
53-
e := c.next()
54-
query = stripQuery(query)
55-
if e == nil {
56-
return nil, fmt.Errorf("all expectations were already fulfilled, call to exec '%s' query with args %+v was not expected", query, args)
57-
}
58-
59-
eq, ok := e.(*expectedExec)
60-
if !ok {
61-
return nil, fmt.Errorf("call to exec query '%s' with args %+v, was not expected, next expectation is %T as %+v", query, args, e, e)
62-
}
63-
64-
eq.triggered = true
65-
if eq.err != nil {
66-
return nil, eq.err // mocked to return error
67-
}
68-
69-
if eq.result == nil {
70-
return nil, fmt.Errorf("exec query '%s' with args %+v, must return a database/sql/driver.result, but it was not set for expectation %T as %+v", query, args, eq, eq)
71-
}
72-
73-
if !eq.queryMatches(query) {
74-
return nil, fmt.Errorf("exec query '%s', does not match regex '%s'", query, eq.sqlRegex.String())
75-
}
76-
77-
if !eq.argsMatches(args) {
78-
return nil, fmt.Errorf("exec query '%s', args %+v does not match expected %+v", query, args, eq.args)
79-
}
80-
81-
return eq.result, nil
53+
e := c.next()
54+
query = stripQuery(query)
55+
if e == nil {
56+
return nil, fmt.Errorf("all expectations were already fulfilled, call to exec '%s' query with args %+v was not expected", query, args)
57+
}
58+
59+
eq, ok := e.(*expectedExec)
60+
if !ok {
61+
return nil, fmt.Errorf("call to exec query '%s' with args %+v, was not expected, next expectation is %T as %+v", query, args, e, e)
62+
}
63+
64+
eq.triggered = true
65+
if eq.err != nil {
66+
return nil, eq.err // mocked to return error
67+
}
68+
69+
if eq.result == nil {
70+
return nil, fmt.Errorf("exec query '%s' with args %+v, must return a database/sql/driver.result, but it was not set for expectation %T as %+v", query, args, eq, eq)
71+
}
72+
73+
if !eq.queryMatches(query) {
74+
return nil, fmt.Errorf("exec query '%s', does not match regex '%s'", query, eq.sqlRegex.String())
75+
}
76+
77+
if !eq.argsMatches(args) {
78+
return nil, fmt.Errorf("exec query '%s', args %+v does not match expected %+v", query, args, eq.args)
79+
}
80+
81+
return eq.result, nil
8282
}
8383

8484
func (c *conn) Prepare(query string) (driver.Stmt, error) {
85-
return &statement{mock.conn, stripQuery(query)}, nil
85+
return &statement{mock.conn, stripQuery(query)}, nil
8686
}
8787

8888
func (c *conn) Query(query string, args []driver.Value) (driver.Rows, error) {
89-
e := c.next()
90-
query = stripQuery(query)
91-
if e == nil {
92-
return nil, fmt.Errorf("all expectations were already fulfilled, call to query '%s' with args %+v was not expected", query, args)
93-
}
94-
95-
eq, ok := e.(*expectedQuery)
96-
if !ok {
97-
return nil, fmt.Errorf("call to query '%s' with args %+v, was not expected, next expectation is %T as %+v", query, args, e, e)
98-
}
99-
100-
eq.triggered = true
101-
if eq.err != nil {
102-
return nil, eq.err // mocked to return error
103-
}
104-
105-
if eq.rows == nil {
106-
return nil, fmt.Errorf("query '%s' with args %+v, must return a database/sql/driver.rows, but it was not set for expectation %T as %+v", query, args, eq, eq)
107-
}
108-
109-
if !eq.queryMatches(query) {
110-
return nil, fmt.Errorf("query '%s', does not match regex [%s]", query, eq.sqlRegex.String())
111-
}
112-
113-
if !eq.argsMatches(args) {
114-
return nil, fmt.Errorf("query '%s', args %+v does not match expected %+v", query, args, eq.args)
115-
}
116-
117-
return eq.rows, nil
89+
e := c.next()
90+
query = stripQuery(query)
91+
if e == nil {
92+
return nil, fmt.Errorf("all expectations were already fulfilled, call to query '%s' with args %+v was not expected", query, args)
93+
}
94+
95+
eq, ok := e.(*expectedQuery)
96+
if !ok {
97+
return nil, fmt.Errorf("call to query '%s' with args %+v, was not expected, next expectation is %T as %+v", query, args, e, e)
98+
}
99+
100+
eq.triggered = true
101+
if eq.err != nil {
102+
return nil, eq.err // mocked to return error
103+
}
104+
105+
if eq.rows == nil {
106+
return nil, fmt.Errorf("query '%s' with args %+v, must return a database/sql/driver.rows, but it was not set for expectation %T as %+v", query, args, eq, eq)
107+
}
108+
109+
if !eq.queryMatches(query) {
110+
return nil, fmt.Errorf("query '%s', does not match regex [%s]", query, eq.sqlRegex.String())
111+
}
112+
113+
if !eq.argsMatches(args) {
114+
return nil, fmt.Errorf("query '%s', args %+v does not match expected %+v", query, args, eq.args)
115+
}
116+
117+
return eq.rows, nil
118118
}

expectations.go

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,119 @@
11
package sqlmock
22

33
import (
4-
"database/sql/driver"
5-
"reflect"
6-
"regexp"
4+
"database/sql/driver"
5+
"reflect"
6+
"regexp"
77
)
88

99
// Argument interface allows to match
1010
// any argument in specific way
1111
type Argument interface {
12-
Match(driver.Value) bool
12+
Match(driver.Value) bool
1313
}
1414

1515
// an expectation interface
1616
type expectation interface {
17-
fulfilled() bool
18-
setError(err error)
17+
fulfilled() bool
18+
setError(err error)
1919
}
2020

2121
// common expectation struct
2222
// satisfies the expectation interface
2323
type commonExpectation struct {
24-
triggered bool
25-
err error
24+
triggered bool
25+
err error
2626
}
2727

2828
func (e *commonExpectation) fulfilled() bool {
29-
return e.triggered
29+
return e.triggered
3030
}
3131

3232
func (e *commonExpectation) setError(err error) {
33-
e.err = err
33+
e.err = err
3434
}
3535

3636
// query based expectation
3737
// adds a query matching logic
3838
type queryBasedExpectation struct {
39-
commonExpectation
40-
sqlRegex *regexp.Regexp
41-
args []driver.Value
39+
commonExpectation
40+
sqlRegex *regexp.Regexp
41+
args []driver.Value
4242
}
4343

4444
func (e *queryBasedExpectation) queryMatches(sql string) bool {
45-
return e.sqlRegex.MatchString(sql)
45+
return e.sqlRegex.MatchString(sql)
4646
}
4747

4848
func (e *queryBasedExpectation) argsMatches(args []driver.Value) bool {
49-
if nil == e.args {
50-
return true
51-
}
52-
if len(args) != len(e.args) {
53-
return false
54-
}
55-
for k, v := range args {
56-
matcher, ok := e.args[k].(Argument)
57-
if ok {
58-
if !matcher.Match(v) {
59-
return false
60-
}
61-
continue
62-
}
63-
vi := reflect.ValueOf(v)
64-
ai := reflect.ValueOf(e.args[k])
65-
switch vi.Kind() {
66-
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
67-
if vi.Int() != ai.Int() {
68-
return false
69-
}
70-
case reflect.Float32, reflect.Float64:
71-
if vi.Float() != ai.Float() {
72-
return false
73-
}
74-
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
75-
if vi.Uint() != ai.Uint() {
76-
return false
77-
}
78-
case reflect.String:
79-
if vi.String() != ai.String() {
80-
return false
81-
}
82-
default:
83-
// compare types like time.Time based on type only
84-
if vi.Kind() != ai.Kind() {
85-
return false
86-
}
87-
}
88-
}
89-
return true
49+
if nil == e.args {
50+
return true
51+
}
52+
if len(args) != len(e.args) {
53+
return false
54+
}
55+
for k, v := range args {
56+
matcher, ok := e.args[k].(Argument)
57+
if ok {
58+
if !matcher.Match(v) {
59+
return false
60+
}
61+
continue
62+
}
63+
vi := reflect.ValueOf(v)
64+
ai := reflect.ValueOf(e.args[k])
65+
switch vi.Kind() {
66+
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
67+
if vi.Int() != ai.Int() {
68+
return false
69+
}
70+
case reflect.Float32, reflect.Float64:
71+
if vi.Float() != ai.Float() {
72+
return false
73+
}
74+
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
75+
if vi.Uint() != ai.Uint() {
76+
return false
77+
}
78+
case reflect.String:
79+
if vi.String() != ai.String() {
80+
return false
81+
}
82+
default:
83+
// compare types like time.Time based on type only
84+
if vi.Kind() != ai.Kind() {
85+
return false
86+
}
87+
}
88+
}
89+
return true
9090
}
9191

9292
// begin transaction
9393
type expectedBegin struct {
94-
commonExpectation
94+
commonExpectation
9595
}
9696

9797
// tx commit
9898
type expectedCommit struct {
99-
commonExpectation
99+
commonExpectation
100100
}
101101

102102
// tx rollback
103103
type expectedRollback struct {
104-
commonExpectation
104+
commonExpectation
105105
}
106106

107107
// query expectation
108108
type expectedQuery struct {
109-
queryBasedExpectation
109+
queryBasedExpectation
110110

111-
rows driver.Rows
111+
rows driver.Rows
112112
}
113113

114114
// exec query expectation
115115
type expectedExec struct {
116-
queryBasedExpectation
116+
queryBasedExpectation
117117

118-
result driver.Result
118+
result driver.Result
119119
}

0 commit comments

Comments
 (0)