Skip to content

Commit 012d928

Browse files
authored
Merge pull request #208 from zevst/master
migration from custom namedValue to driver.NamedValue
2 parents b91d98d + 4fbf605 commit 012d928

15 files changed

+818
-319
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/examples/blog/blog
22
/examples/orders/orders
33
/examples/basic/basic
4+
.idea/

expectations.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -339,21 +339,6 @@ type queryBasedExpectation struct {
339339
args []driver.Value
340340
}
341341

342-
func (e *queryBasedExpectation) attemptArgMatch(args []namedValue) (err error) {
343-
// catch panic
344-
defer func() {
345-
if e := recover(); e != nil {
346-
_, ok := e.(error)
347-
if !ok {
348-
err = fmt.Errorf(e.(string))
349-
}
350-
}
351-
}()
352-
353-
err = e.argsMatches(args)
354-
return
355-
}
356-
357342
// ExpectedPing is used to manage *sql.DB.Ping expectations.
358343
// Returned by *Sqlmock.ExpectPing.
359344
type ExpectedPing struct {

expectations_before_go18.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,18 @@ func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
5050
}
5151
return nil
5252
}
53+
54+
func (e *queryBasedExpectation) attemptArgMatch(args []namedValue) (err error) {
55+
// catch panic
56+
defer func() {
57+
if e := recover(); e != nil {
58+
_, ok := e.(error)
59+
if !ok {
60+
err = fmt.Errorf(e.(string))
61+
}
62+
}
63+
}()
64+
65+
err = e.argsMatches(args)
66+
return
67+
}

expectations_before_go18_test.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// +build !go1.8
2+
3+
package sqlmock
4+
5+
import (
6+
"database/sql/driver"
7+
"testing"
8+
"time"
9+
)
10+
11+
func TestQueryExpectationArgComparison(t *testing.T) {
12+
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter}
13+
against := []namedValue{{Value: int64(5), Ordinal: 1}}
14+
if err := e.argsMatches(against); err != nil {
15+
t.Errorf("arguments should match, since the no expectation was set, but got err: %s", err)
16+
}
17+
18+
e.args = []driver.Value{5, "str"}
19+
20+
against = []namedValue{{Value: int64(5), Ordinal: 1}}
21+
if err := e.argsMatches(against); err == nil {
22+
t.Error("arguments should not match, since the size is not the same")
23+
}
24+
25+
against = []namedValue{
26+
{Value: int64(3), Ordinal: 1},
27+
{Value: "str", Ordinal: 2},
28+
}
29+
if err := e.argsMatches(against); err == nil {
30+
t.Error("arguments should not match, since the first argument (int value) is different")
31+
}
32+
33+
against = []namedValue{
34+
{Value: int64(5), Ordinal: 1},
35+
{Value: "st", Ordinal: 2},
36+
}
37+
if err := e.argsMatches(against); err == nil {
38+
t.Error("arguments should not match, since the second argument (string value) is different")
39+
}
40+
41+
against = []namedValue{
42+
{Value: int64(5), Ordinal: 1},
43+
{Value: "str", Ordinal: 2},
44+
}
45+
if err := e.argsMatches(against); err != nil {
46+
t.Errorf("arguments should match, but it did not: %s", err)
47+
}
48+
49+
const longForm = "Jan 2, 2006 at 3:04pm (MST)"
50+
tm, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
51+
e.args = []driver.Value{5, tm}
52+
53+
against = []namedValue{
54+
{Value: int64(5), Ordinal: 1},
55+
{Value: tm, Ordinal: 2},
56+
}
57+
if err := e.argsMatches(against); err != nil {
58+
t.Error("arguments should match, but it did not")
59+
}
60+
61+
e.args = []driver.Value{5, AnyArg()}
62+
if err := e.argsMatches(against); err != nil {
63+
t.Errorf("arguments should match, but it did not: %s", err)
64+
}
65+
}
66+
67+
func TestQueryExpectationArgComparisonBool(t *testing.T) {
68+
var e *queryBasedExpectation
69+
70+
e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
71+
against := []namedValue{
72+
{Value: true, Ordinal: 1},
73+
}
74+
if err := e.argsMatches(against); err != nil {
75+
t.Error("arguments should match, since arguments are the same")
76+
}
77+
78+
e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
79+
against = []namedValue{
80+
{Value: false, Ordinal: 1},
81+
}
82+
if err := e.argsMatches(against); err != nil {
83+
t.Error("arguments should match, since argument are the same")
84+
}
85+
86+
e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
87+
against = []namedValue{
88+
{Value: false, Ordinal: 1},
89+
}
90+
if err := e.argsMatches(against); err == nil {
91+
t.Error("arguments should not match, since argument is different")
92+
}
93+
94+
e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
95+
against = []namedValue{
96+
{Value: true, Ordinal: 1},
97+
}
98+
if err := e.argsMatches(against); err == nil {
99+
t.Error("arguments should not match, since argument is different")
100+
}
101+
}
102+
103+
type panicConverter struct {
104+
}
105+
106+
func (s panicConverter) ConvertValue(v interface{}) (driver.Value, error) {
107+
panic(v)
108+
}
109+
110+
func Test_queryBasedExpectation_attemptArgMatch(t *testing.T) {
111+
e := &queryBasedExpectation{converter: new(panicConverter), args: []driver.Value{"test"}}
112+
values := []namedValue{
113+
{Ordinal: 1, Name: "test", Value: "test"},
114+
}
115+
if err := e.attemptArgMatch(values); err == nil {
116+
t.Errorf("error expected")
117+
}
118+
}

expectations_go18.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package sqlmock
44

55
import (
66
"database/sql"
7+
"database/sql/driver"
78
"fmt"
89
"reflect"
910
)
@@ -19,7 +20,7 @@ func (e *ExpectedQuery) WillReturnRows(rows ...*Rows) *ExpectedQuery {
1920
return e
2021
}
2122

22-
func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
23+
func (e *queryBasedExpectation) argsMatches(args []driver.NamedValue) error {
2324
if nil == e.args {
2425
return nil
2526
}
@@ -59,3 +60,18 @@ func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
5960
}
6061
return nil
6162
}
63+
64+
func (e *queryBasedExpectation) attemptArgMatch(args []driver.NamedValue) (err error) {
65+
// catch panic
66+
defer func() {
67+
if e := recover(); e != nil {
68+
_, ok := e.(error)
69+
if !ok {
70+
err = fmt.Errorf(e.(string))
71+
}
72+
}
73+
}()
74+
75+
err = e.argsMatches(args)
76+
return
77+
}

expectations_go18_test.go

Lines changed: 115 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,104 @@ import (
66
"database/sql"
77
"database/sql/driver"
88
"testing"
9+
"time"
910
)
1011

12+
func TestQueryExpectationArgComparison(t *testing.T) {
13+
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter}
14+
against := []driver.NamedValue{{Value: int64(5), Ordinal: 1}}
15+
if err := e.argsMatches(against); err != nil {
16+
t.Errorf("arguments should match, since the no expectation was set, but got err: %s", err)
17+
}
18+
19+
e.args = []driver.Value{5, "str"}
20+
21+
against = []driver.NamedValue{{Value: int64(5), Ordinal: 1}}
22+
if err := e.argsMatches(against); err == nil {
23+
t.Error("arguments should not match, since the size is not the same")
24+
}
25+
26+
against = []driver.NamedValue{
27+
{Value: int64(3), Ordinal: 1},
28+
{Value: "str", Ordinal: 2},
29+
}
30+
if err := e.argsMatches(against); err == nil {
31+
t.Error("arguments should not match, since the first argument (int value) is different")
32+
}
33+
34+
against = []driver.NamedValue{
35+
{Value: int64(5), Ordinal: 1},
36+
{Value: "st", Ordinal: 2},
37+
}
38+
if err := e.argsMatches(against); err == nil {
39+
t.Error("arguments should not match, since the second argument (string value) is different")
40+
}
41+
42+
against = []driver.NamedValue{
43+
{Value: int64(5), Ordinal: 1},
44+
{Value: "str", Ordinal: 2},
45+
}
46+
if err := e.argsMatches(against); err != nil {
47+
t.Errorf("arguments should match, but it did not: %s", err)
48+
}
49+
50+
const longForm = "Jan 2, 2006 at 3:04pm (MST)"
51+
tm, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
52+
e.args = []driver.Value{5, tm}
53+
54+
against = []driver.NamedValue{
55+
{Value: int64(5), Ordinal: 1},
56+
{Value: tm, Ordinal: 2},
57+
}
58+
if err := e.argsMatches(against); err != nil {
59+
t.Error("arguments should match, but it did not")
60+
}
61+
62+
e.args = []driver.Value{5, AnyArg()}
63+
if err := e.argsMatches(against); err != nil {
64+
t.Errorf("arguments should match, but it did not: %s", err)
65+
}
66+
}
67+
68+
func TestQueryExpectationArgComparisonBool(t *testing.T) {
69+
var e *queryBasedExpectation
70+
71+
e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
72+
against := []driver.NamedValue{
73+
{Value: true, Ordinal: 1},
74+
}
75+
if err := e.argsMatches(against); err != nil {
76+
t.Error("arguments should match, since arguments are the same")
77+
}
78+
79+
e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
80+
against = []driver.NamedValue{
81+
{Value: false, Ordinal: 1},
82+
}
83+
if err := e.argsMatches(against); err != nil {
84+
t.Error("arguments should match, since argument are the same")
85+
}
86+
87+
e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
88+
against = []driver.NamedValue{
89+
{Value: false, Ordinal: 1},
90+
}
91+
if err := e.argsMatches(against); err == nil {
92+
t.Error("arguments should not match, since argument is different")
93+
}
94+
95+
e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
96+
against = []driver.NamedValue{
97+
{Value: true, Ordinal: 1},
98+
}
99+
if err := e.argsMatches(against); err == nil {
100+
t.Error("arguments should not match, since argument is different")
101+
}
102+
}
103+
11104
func TestQueryExpectationNamedArgComparison(t *testing.T) {
12105
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter}
13-
against := []namedValue{{Value: int64(5), Name: "id"}}
106+
against := []driver.NamedValue{{Value: int64(5), Name: "id"}}
14107
if err := e.argsMatches(against); err != nil {
15108
t.Errorf("arguments should match, since the no expectation was set, but got err: %s", err)
16109
}
@@ -24,7 +117,7 @@ func TestQueryExpectationNamedArgComparison(t *testing.T) {
24117
t.Error("arguments should not match, since the size is not the same")
25118
}
26119

27-
against = []namedValue{
120+
against = []driver.NamedValue{
28121
{Value: int64(5), Name: "id"},
29122
{Value: "str", Name: "s"},
30123
}
@@ -33,7 +126,7 @@ func TestQueryExpectationNamedArgComparison(t *testing.T) {
33126
t.Errorf("arguments should have matched, but it did not: %v", err)
34127
}
35128

36-
against = []namedValue{
129+
against = []driver.NamedValue{
37130
{Value: int64(5), Name: "id"},
38131
{Value: "str", Name: "username"},
39132
}
@@ -44,7 +137,7 @@ func TestQueryExpectationNamedArgComparison(t *testing.T) {
44137

45138
e.args = []driver.Value{int64(5), "str"}
46139

47-
against = []namedValue{
140+
against = []driver.NamedValue{
48141
{Value: int64(5), Ordinal: 0},
49142
{Value: "str", Ordinal: 1},
50143
}
@@ -53,7 +146,7 @@ func TestQueryExpectationNamedArgComparison(t *testing.T) {
53146
t.Error("arguments matched, but it should have not due to wrong Ordinal position")
54147
}
55148

56-
against = []namedValue{
149+
against = []driver.NamedValue{
57150
{Value: int64(5), Ordinal: 1},
58151
{Value: "str", Ordinal: 2},
59152
}
@@ -62,3 +155,20 @@ func TestQueryExpectationNamedArgComparison(t *testing.T) {
62155
t.Errorf("arguments should have matched, but it did not: %v", err)
63156
}
64157
}
158+
159+
type panicConverter struct {
160+
}
161+
162+
func (s panicConverter) ConvertValue(v interface{}) (driver.Value, error) {
163+
panic(v)
164+
}
165+
166+
func Test_queryBasedExpectation_attemptArgMatch(t *testing.T) {
167+
e := &queryBasedExpectation{converter: new(panicConverter), args: []driver.Value{"test"}}
168+
values := []driver.NamedValue{
169+
{Ordinal: 1, Name: "test", Value: "test"},
170+
}
171+
if err := e.attemptArgMatch(values); err == nil {
172+
t.Errorf("error expected")
173+
}
174+
}

0 commit comments

Comments
 (0)