-
Notifications
You must be signed in to change notification settings - Fork 31
/
stmt_go19_test.go
171 lines (154 loc) · 3.71 KB
/
stmt_go19_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
package sqlmw
import (
"database/sql"
"database/sql/driver"
"reflect"
"testing"
)
// TestDefaultParameterConversion ensures that
// driver.DefaultParameterConverter is used when neither stmt nor con
// implements any value converters.
func TestDefaultParameterConversion(t *testing.T) {
driverName := driverName(t)
expectVal := int64(1)
con := &fakeConn{}
fakeStmt := &fakeStmt{
rows: &fakeRows{
con: con,
vals: [][]driver.Value{{expectVal}},
},
}
con.stmt = fakeStmt
sql.Register(
driverName,
Driver(&fakeDriver{conn: con}, &NullInterceptor{}),
)
db, err := sql.Open(driverName, "")
if err != nil {
t.Fatalf("Failed to open: %v", err)
}
t.Cleanup(func() {
if err := db.Close(); err != nil {
t.Errorf("Failed to close db: %v", err)
}
})
stmt, err := db.Prepare("")
if err != nil {
t.Fatalf("Prepare failed: %s", err)
}
// int32 values are converted by driver.DefaultParameterConverter to
// int64
queryVal := int32(1)
rows, err := stmt.Query(queryVal)
if err != nil {
t.Fatalf("Query failed: %s", err)
}
count := 0
for rows.Next() {
var v int64
err := rows.Scan(&v)
if err != nil {
t.Fatalf("rows.Scan failed, %v", err)
}
if v != 1 {
t.Errorf("converted value is %d, passed value to Query was: %d", v, expectVal)
}
count++
}
if count != 1 {
t.Fatalf("got too many rows, expected 1, got %d ", 1)
}
}
func TestWrappedStmt_CheckNamedValue(t *testing.T) {
tests := map[string]struct {
fd *fakeDriver
expected struct {
cc bool // Whether the fakeConn's CheckNamedValue was called
sc bool // Whether the fakeStmt's CheckNamedValue was called
}
}{
"When both conn and stmt implement CheckNamedValue": {
fd: &fakeDriver{
conn: &fakeConnWithCheckNamedValue{
fakeConn: fakeConn{
stmt: &fakeStmtWithCheckNamedValue{},
},
},
},
expected: struct {
cc bool
sc bool
}{cc: false, sc: true},
},
"When only conn implements CheckNamedValue": {
fd: &fakeDriver{
conn: &fakeConnWithCheckNamedValue{
fakeConn: fakeConn{
stmt: &fakeStmtWithoutCheckNamedValue{},
},
},
},
expected: struct {
cc bool
sc bool
}{cc: true, sc: false},
},
"When only stmt implements CheckNamedValue": {
fd: &fakeDriver{
conn: &fakeConnWithoutCheckNamedValue{
fakeConn: fakeConn{
stmt: &fakeStmtWithCheckNamedValue{},
},
},
},
expected: struct {
cc bool
sc bool
}{cc: false, sc: true},
},
"When both stmt do not implement CheckNamedValue": {
fd: &fakeDriver{
conn: &fakeConnWithoutCheckNamedValue{
fakeConn: fakeConn{
stmt: &fakeStmtWithoutCheckNamedValue{},
},
},
},
expected: struct {
cc bool
sc bool
}{cc: false, sc: false},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
driverName := driverName(t)
sql.Register(driverName, Driver(test.fd, &fakeInterceptor{}))
db, err := sql.Open(driverName, "dummy")
if err != nil {
t.Errorf("Failed to open: %v", err)
}
defer func() {
if err := db.Close(); err != nil {
t.Errorf("Failed to close db: %v", err)
}
}()
stmt, err := db.Prepare("SELECT foo FROM bar Where 1 = ?")
if err != nil {
t.Errorf("Failed to prepare: %v", err)
}
if _, err := stmt.Query(1); err != nil {
t.Errorf("Failed to query: %v", err)
}
conn := reflect.ValueOf(test.fd.conn).Elem()
sc := conn.FieldByName("stmt").Elem().Elem().FieldByName("called").Bool()
cc := conn.FieldByName("called").Bool()
if test.expected.sc != sc {
t.Errorf("sc mismatch.\n got: %#v\nwant: %#v", sc, test.expected.sc)
}
if test.expected.cc != cc {
t.Errorf("cc mismatch.\n got: %#v\nwant: %#v", cc, test.expected.cc)
}
})
}
}