forked from DATA-DOG/go-sqlmock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expectations_go18_test.go
185 lines (152 loc) · 5.27 KB
/
expectations_go18_test.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//go:build go1.8
// +build go1.8
package sqlmock
import (
"database/sql"
"database/sql/driver"
"testing"
"time"
)
func TestQueryExpectationArgComparison(t *testing.T) {
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter, noArgs: true}
against := []driver.NamedValue{{Value: int64(5), Ordinal: 1}}
if err := e.argsMatches(against); err == nil {
t.Error("arguments should not match, since argument was passed, but noArgs was set")
}
e.noArgs = false
if err := e.argsMatches(against); err != nil {
t.Error("arguments should match, since argument was passed, but no expected args or noArgs was set")
}
e.args = []driver.Value{5, "str"}
against = []driver.NamedValue{{Value: int64(5), Ordinal: 1}}
if err := e.argsMatches(against); err == nil {
t.Error("arguments should not match, since the size is not the same")
}
against = []driver.NamedValue{
{Value: int64(3), Ordinal: 1},
{Value: "str", Ordinal: 2},
}
if err := e.argsMatches(against); err == nil {
t.Error("arguments should not match, since the first argument (int value) is different")
}
against = []driver.NamedValue{
{Value: int64(5), Ordinal: 1},
{Value: "st", Ordinal: 2},
}
if err := e.argsMatches(against); err == nil {
t.Error("arguments should not match, since the second argument (string value) is different")
}
against = []driver.NamedValue{
{Value: int64(5), Ordinal: 1},
{Value: "str", Ordinal: 2},
}
if err := e.argsMatches(against); err != nil {
t.Errorf("arguments should match, but it did not: %s", err)
}
const longForm = "Jan 2, 2006 at 3:04pm (MST)"
tm, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
e.args = []driver.Value{5, tm}
against = []driver.NamedValue{
{Value: int64(5), Ordinal: 1},
{Value: tm, Ordinal: 2},
}
if err := e.argsMatches(against); err != nil {
t.Error("arguments should match, but it did not")
}
e.args = []driver.Value{5, AnyArg()}
if err := e.argsMatches(against); err != nil {
t.Errorf("arguments should match, but it did not: %s", err)
}
}
func TestQueryExpectationArgComparisonBool(t *testing.T) {
var e *queryBasedExpectation
e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
against := []driver.NamedValue{
{Value: true, Ordinal: 1},
}
if err := e.argsMatches(against); err != nil {
t.Error("arguments should match, since arguments are the same")
}
e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
against = []driver.NamedValue{
{Value: false, Ordinal: 1},
}
if err := e.argsMatches(against); err != nil {
t.Error("arguments should match, since argument are the same")
}
e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
against = []driver.NamedValue{
{Value: false, Ordinal: 1},
}
if err := e.argsMatches(against); err == nil {
t.Error("arguments should not match, since argument is different")
}
e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
against = []driver.NamedValue{
{Value: true, Ordinal: 1},
}
if err := e.argsMatches(against); err == nil {
t.Error("arguments should not match, since argument is different")
}
}
func TestQueryExpectationNamedArgComparison(t *testing.T) {
e := &queryBasedExpectation{converter: driver.DefaultParameterConverter, noArgs: true}
against := []driver.NamedValue{{Value: int64(5), Name: "id"}}
if err := e.argsMatches(against); err == nil {
t.Error("arguments should not match, since argument was passed, but noArgs was set")
}
e.noArgs = false
if err := e.argsMatches(against); err != nil {
t.Error("arguments should match, since argument was passed, but no expected args or noArgs was set")
}
e.args = []driver.Value{
sql.Named("id", 5),
sql.Named("s", "str"),
}
if err := e.argsMatches(against); err == nil {
t.Error("arguments should not match, since the size is not the same")
}
against = []driver.NamedValue{
{Value: int64(5), Name: "id"},
{Value: "str", Name: "s"},
}
if err := e.argsMatches(against); err != nil {
t.Errorf("arguments should have matched, but it did not: %v", err)
}
against = []driver.NamedValue{
{Value: int64(5), Name: "id"},
{Value: "str", Name: "username"},
}
if err := e.argsMatches(against); err == nil {
t.Error("arguments matched, but it should have not due to Name")
}
e.args = []driver.Value{int64(5), "str"}
against = []driver.NamedValue{
{Value: int64(5), Ordinal: 0},
{Value: "str", Ordinal: 1},
}
if err := e.argsMatches(against); err == nil {
t.Error("arguments matched, but it should have not due to wrong Ordinal position")
}
against = []driver.NamedValue{
{Value: int64(5), Ordinal: 1},
{Value: "str", Ordinal: 2},
}
if err := e.argsMatches(against); err != nil {
t.Errorf("arguments should have matched, but it did not: %v", err)
}
}
type panicConverter struct {
}
func (s panicConverter) ConvertValue(v interface{}) (driver.Value, error) {
panic(v)
}
func Test_queryBasedExpectation_attemptArgMatch(t *testing.T) {
e := &queryBasedExpectation{converter: new(panicConverter), args: []driver.Value{"test"}}
values := []driver.NamedValue{
{Ordinal: 1, Name: "test", Value: "test"},
}
if err := e.attemptArgMatch(values); err == nil {
t.Errorf("error expected")
}
}