@@ -6,11 +6,104 @@ import (
6
6
"database/sql"
7
7
"database/sql/driver"
8
8
"testing"
9
+ "time"
9
10
)
10
11
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
+
11
104
func TestQueryExpectationNamedArgComparison (t * testing.T ) {
12
105
e := & queryBasedExpectation {converter : driver .DefaultParameterConverter }
13
- against := []namedValue {{Value : int64 (5 ), Name : "id" }}
106
+ against := []driver. NamedValue {{Value : int64 (5 ), Name : "id" }}
14
107
if err := e .argsMatches (against ); err != nil {
15
108
t .Errorf ("arguments should match, since the no expectation was set, but got err: %s" , err )
16
109
}
@@ -24,7 +117,7 @@ func TestQueryExpectationNamedArgComparison(t *testing.T) {
24
117
t .Error ("arguments should not match, since the size is not the same" )
25
118
}
26
119
27
- against = []namedValue {
120
+ against = []driver. NamedValue {
28
121
{Value : int64 (5 ), Name : "id" },
29
122
{Value : "str" , Name : "s" },
30
123
}
@@ -33,7 +126,7 @@ func TestQueryExpectationNamedArgComparison(t *testing.T) {
33
126
t .Errorf ("arguments should have matched, but it did not: %v" , err )
34
127
}
35
128
36
- against = []namedValue {
129
+ against = []driver. NamedValue {
37
130
{Value : int64 (5 ), Name : "id" },
38
131
{Value : "str" , Name : "username" },
39
132
}
@@ -44,7 +137,7 @@ func TestQueryExpectationNamedArgComparison(t *testing.T) {
44
137
45
138
e .args = []driver.Value {int64 (5 ), "str" }
46
139
47
- against = []namedValue {
140
+ against = []driver. NamedValue {
48
141
{Value : int64 (5 ), Ordinal : 0 },
49
142
{Value : "str" , Ordinal : 1 },
50
143
}
@@ -53,7 +146,7 @@ func TestQueryExpectationNamedArgComparison(t *testing.T) {
53
146
t .Error ("arguments matched, but it should have not due to wrong Ordinal position" )
54
147
}
55
148
56
- against = []namedValue {
149
+ against = []driver. NamedValue {
57
150
{Value : int64 (5 ), Ordinal : 1 },
58
151
{Value : "str" , Ordinal : 2 },
59
152
}
@@ -62,3 +155,20 @@ func TestQueryExpectationNamedArgComparison(t *testing.T) {
62
155
t .Errorf ("arguments should have matched, but it did not: %v" , err )
63
156
}
64
157
}
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