-
-
Notifications
You must be signed in to change notification settings - Fork 409
/
rows_go18_test.go
387 lines (334 loc) · 13 KB
/
rows_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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// +build go1.8
package sqlmock
import (
"database/sql"
"encoding/json"
"fmt"
"reflect"
"testing"
"time"
)
func TestQueryMultiRows(t *testing.T) {
t.Parallel()
db, mock, err := New()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()
rs1 := NewRows([]string{"id", "title"}).AddRow(5, "hello world")
rs2 := NewRows([]string{"name"}).AddRow("gopher").AddRow("john").AddRow("jane").RowError(2, fmt.Errorf("error"))
mock.ExpectQuery("SELECT (.+) FROM articles WHERE id = \\?;SELECT name FROM users").
WithArgs(5).
WillReturnRows(rs1, rs2)
rows, err := db.Query("SELECT id, title FROM articles WHERE id = ?;SELECT name FROM users", 5)
if err != nil {
t.Errorf("error was not expected, but got: %v", err)
}
defer rows.Close()
if !rows.Next() {
t.Error("expected a row to be available in first result set")
}
var id int
var name string
err = rows.Scan(&id, &name)
if err != nil {
t.Errorf("error was not expected, but got: %v", err)
}
if id != 5 || name != "hello world" {
t.Errorf("unexpected row values id: %v name: %v", id, name)
}
if rows.Next() {
t.Error("was not expecting next row in first result set")
}
if !rows.NextResultSet() {
t.Error("had to have next result set")
}
if !rows.Next() {
t.Error("expected a row to be available in second result set")
}
err = rows.Scan(&name)
if err != nil {
t.Errorf("error was not expected, but got: %v", err)
}
if name != "gopher" {
t.Errorf("unexpected row name: %v", name)
}
if !rows.Next() {
t.Error("expected a row to be available in second result set")
}
err = rows.Scan(&name)
if err != nil {
t.Errorf("error was not expected, but got: %v", err)
}
if name != "john" {
t.Errorf("unexpected row name: %v", name)
}
if rows.Next() {
t.Error("expected next row to produce error")
}
if rows.Err() == nil {
t.Error("expected an error, but there was none")
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
func TestQueryRowBytesInvalidatedByNext_jsonRawMessageIntoRawBytes(t *testing.T) {
t.Parallel()
replace := []byte(invalid)
rows := NewRows([]string{"raw"}).
AddRow(json.RawMessage(`{"thing": "one", "thing2": "two"}`)).
AddRow(json.RawMessage(`{"that": "foo", "this": "bar"}`))
scan := func(rs *sql.Rows) ([]byte, error) {
var raw sql.RawBytes
return raw, rs.Scan(&raw)
}
want := []struct {
Initial []byte
Replaced []byte
}{
{Initial: []byte(`{"thing": "one", "thing2": "two"}`), Replaced: replace[:len(replace)-6]},
{Initial: []byte(`{"that": "foo", "this": "bar"}`), Replaced: replace[:len(replace)-9]},
}
queryRowBytesInvalidatedByNext(t, rows, scan, want)
}
func TestQueryRowBytesNotInvalidatedByNext_jsonRawMessageIntoBytes(t *testing.T) {
t.Parallel()
rows := NewRows([]string{"raw"}).
AddRow(json.RawMessage(`{"thing": "one", "thing2": "two"}`)).
AddRow(json.RawMessage(`{"that": "foo", "this": "bar"}`))
scan := func(rs *sql.Rows) ([]byte, error) {
var b []byte
return b, rs.Scan(&b)
}
want := [][]byte{[]byte(`{"thing": "one", "thing2": "two"}`), []byte(`{"that": "foo", "this": "bar"}`)}
queryRowBytesNotInvalidatedByNext(t, rows, scan, want)
}
func TestQueryRowBytesNotInvalidatedByNext_bytesIntoCustomBytes(t *testing.T) {
t.Parallel()
rows := NewRows([]string{"raw"}).
AddRow([]byte(`one binary value with some text!`)).
AddRow([]byte(`two binary value with even more text than the first one`))
scan := func(rs *sql.Rows) ([]byte, error) {
type customBytes []byte
var b customBytes
return b, rs.Scan(&b)
}
want := [][]byte{[]byte(`one binary value with some text!`), []byte(`two binary value with even more text than the first one`)}
queryRowBytesNotInvalidatedByNext(t, rows, scan, want)
}
func TestQueryRowBytesNotInvalidatedByNext_jsonRawMessageIntoCustomBytes(t *testing.T) {
t.Parallel()
rows := NewRows([]string{"raw"}).
AddRow(json.RawMessage(`{"thing": "one", "thing2": "two"}`)).
AddRow(json.RawMessage(`{"that": "foo", "this": "bar"}`))
scan := func(rs *sql.Rows) ([]byte, error) {
type customBytes []byte
var b customBytes
return b, rs.Scan(&b)
}
want := [][]byte{[]byte(`{"thing": "one", "thing2": "two"}`), []byte(`{"that": "foo", "this": "bar"}`)}
queryRowBytesNotInvalidatedByNext(t, rows, scan, want)
}
func TestQueryRowBytesNotInvalidatedByClose_bytesIntoCustomBytes(t *testing.T) {
t.Parallel()
rows := NewRows([]string{"raw"}).AddRow([]byte(`one binary value with some text!`))
scan := func(rs *sql.Rows) ([]byte, error) {
type customBytes []byte
var b customBytes
return b, rs.Scan(&b)
}
queryRowBytesNotInvalidatedByClose(t, rows, scan, []byte(`one binary value with some text!`))
}
func TestQueryRowBytesInvalidatedByClose_jsonRawMessageIntoRawBytes(t *testing.T) {
t.Parallel()
replace := []byte(invalid)
rows := NewRows([]string{"raw"}).AddRow(json.RawMessage(`{"thing": "one", "thing2": "two"}`))
scan := func(rs *sql.Rows) ([]byte, error) {
var raw sql.RawBytes
return raw, rs.Scan(&raw)
}
want := struct {
Initial []byte
Replaced []byte
}{
Initial: []byte(`{"thing": "one", "thing2": "two"}`),
Replaced: replace[:len(replace)-6],
}
queryRowBytesInvalidatedByClose(t, rows, scan, want)
}
func TestQueryRowBytesNotInvalidatedByClose_jsonRawMessageIntoBytes(t *testing.T) {
t.Parallel()
rows := NewRows([]string{"raw"}).AddRow(json.RawMessage(`{"thing": "one", "thing2": "two"}`))
scan := func(rs *sql.Rows) ([]byte, error) {
var b []byte
return b, rs.Scan(&b)
}
queryRowBytesNotInvalidatedByClose(t, rows, scan, []byte(`{"thing": "one", "thing2": "two"}`))
}
func TestQueryRowBytesNotInvalidatedByClose_jsonRawMessageIntoCustomBytes(t *testing.T) {
t.Parallel()
rows := NewRows([]string{"raw"}).AddRow(json.RawMessage(`{"thing": "one", "thing2": "two"}`))
scan := func(rs *sql.Rows) ([]byte, error) {
type customBytes []byte
var b customBytes
return b, rs.Scan(&b)
}
queryRowBytesNotInvalidatedByClose(t, rows, scan, []byte(`{"thing": "one", "thing2": "two"}`))
}
func TestNewColumnWithDefinition(t *testing.T) {
now, _ := time.Parse(time.RFC3339, "2020-06-20T22:08:41Z")
t.Run("with one ResultSet", func(t *testing.T) {
db, mock, _ := New()
column1 := mock.NewColumn("test").OfType("VARCHAR", "").Nullable(true).WithLength(100)
column2 := mock.NewColumn("number").OfType("DECIMAL", float64(0.0)).Nullable(false).WithPrecisionAndScale(10, 4)
column3 := mock.NewColumn("when").OfType("TIMESTAMP", now)
rows := mock.NewRowsWithColumnDefinition(column1, column2, column3)
rows.AddRow("foo.bar", float64(10.123), now)
mQuery := mock.ExpectQuery("SELECT test, number, when from dummy")
isQuery := mQuery.WillReturnRows(rows)
isQueryClosed := mQuery.RowsWillBeClosed()
isDbClosed := mock.ExpectClose()
query, _ := db.Query("SELECT test, number, when from dummy")
if false == isQuery.fulfilled() {
t.Error("Query is not executed")
}
if query.Next() {
var test string
var number float64
var when time.Time
if queryError := query.Scan(&test, &number, &when); queryError != nil {
t.Error(queryError)
} else if test != "foo.bar" {
t.Error("field test is not 'foo.bar'")
} else if number != float64(10.123) {
t.Error("field number is not '10.123'")
} else if when != now {
t.Errorf("field when is not %v", now)
}
if columnTypes, colTypErr := query.ColumnTypes(); colTypErr != nil {
t.Error(colTypErr)
} else if len(columnTypes) != 3 {
t.Error("number of columnTypes")
} else if name := columnTypes[0].Name(); name != "test" {
t.Errorf("field 'test' has a wrong name '%s'", name)
} else if dbType := columnTypes[0].DatabaseTypeName(); dbType != "VARCHAR" {
t.Errorf("field 'test' has a wrong db type '%s'", dbType)
} else if columnTypes[0].ScanType().Kind() != reflect.String {
t.Error("field 'test' has a wrong scanType")
} else if _, _, ok := columnTypes[0].DecimalSize(); ok {
t.Error("field 'test' should have not precision, scale")
} else if length, ok := columnTypes[0].Length(); length != 100 || !ok {
t.Errorf("field 'test' has a wrong length '%d'", length)
} else if name := columnTypes[1].Name(); name != "number" {
t.Errorf("field 'number' has a wrong name '%s'", name)
} else if dbType := columnTypes[1].DatabaseTypeName(); dbType != "DECIMAL" {
t.Errorf("field 'number' has a wrong db type '%s'", dbType)
} else if columnTypes[1].ScanType().Kind() != reflect.Float64 {
t.Error("field 'number' has a wrong scanType")
} else if precision, scale, ok := columnTypes[1].DecimalSize(); precision != int64(10) || scale != int64(4) || !ok {
t.Error("field 'number' has a wrong precision, scale")
} else if _, ok := columnTypes[1].Length(); ok {
t.Error("field 'number' is not variable length type")
} else if _, ok := columnTypes[2].Nullable(); ok {
t.Error("field 'when' should have nullability unknown")
}
} else {
t.Error("no result set")
}
query.Close()
if false == isQueryClosed.fulfilled() {
t.Error("Query is not executed")
}
db.Close()
if false == isDbClosed.fulfilled() {
t.Error("Db is not closed")
}
})
t.Run("with more then one ResultSet", func(t *testing.T) {
db, mock, _ := New()
column1 := mock.NewColumn("test").OfType("VARCHAR", "").Nullable(true).WithLength(100)
column2 := mock.NewColumn("number").OfType("DECIMAL", float64(0.0)).Nullable(false).WithPrecisionAndScale(10, 4)
column3 := mock.NewColumn("when").OfType("TIMESTAMP", now)
rows1 := mock.NewRowsWithColumnDefinition(column1, column2, column3)
rows1.AddRow("foo.bar", float64(10.123), now)
rows2 := mock.NewRowsWithColumnDefinition(column1, column2, column3)
rows2.AddRow("bar.foo", float64(123.10), now.Add(time.Second*10))
rows3 := mock.NewRowsWithColumnDefinition(column1, column2, column3)
rows3.AddRow("lollipop", float64(10.321), now.Add(time.Second*20))
mQuery := mock.ExpectQuery("SELECT test, number, when from dummy")
isQuery := mQuery.WillReturnRows(rows1, rows2, rows3)
isQueryClosed := mQuery.RowsWillBeClosed()
isDbClosed := mock.ExpectClose()
query, _ := db.Query("SELECT test, number, when from dummy")
if false == isQuery.fulfilled() {
t.Error("Query is not executed")
}
rowsSi := 0
for query.Next() {
var test string
var number float64
var when time.Time
if queryError := query.Scan(&test, &number, &when); queryError != nil {
t.Error(queryError)
} else if rowsSi == 0 && test != "foo.bar" {
t.Error("field test is not 'foo.bar'")
} else if rowsSi == 0 && number != float64(10.123) {
t.Error("field number is not '10.123'")
} else if rowsSi == 0 && when != now {
t.Errorf("field when is not %v", now)
} else if rowsSi == 1 && test != "bar.foo" {
t.Error("field test is not 'bar.bar'")
} else if rowsSi == 1 && number != float64(123.10) {
t.Error("field number is not '123.10'")
} else if rowsSi == 1 && when != now.Add(time.Second*10) {
t.Errorf("field when is not %v", now)
} else if rowsSi == 2 && test != "lollipop" {
t.Error("field test is not 'lollipop'")
} else if rowsSi == 2 && number != float64(10.321) {
t.Error("field number is not '10.321'")
} else if rowsSi == 2 && when != now.Add(time.Second*20) {
t.Errorf("field when is not %v", now)
}
rowsSi++
if columnTypes, colTypErr := query.ColumnTypes(); colTypErr != nil {
t.Error(colTypErr)
} else if len(columnTypes) != 3 {
t.Error("number of columnTypes")
} else if name := columnTypes[0].Name(); name != "test" {
t.Errorf("field 'test' has a wrong name '%s'", name)
} else if dbType := columnTypes[0].DatabaseTypeName(); dbType != "VARCHAR" {
t.Errorf("field 'test' has a wrong db type '%s'", dbType)
} else if columnTypes[0].ScanType().Kind() != reflect.String {
t.Error("field 'test' has a wrong scanType")
} else if _, _, ok := columnTypes[0].DecimalSize(); ok {
t.Error("field 'test' should not have precision, scale")
} else if length, ok := columnTypes[0].Length(); length != 100 || !ok {
t.Errorf("field 'test' has a wrong length '%d'", length)
} else if name := columnTypes[1].Name(); name != "number" {
t.Errorf("field 'number' has a wrong name '%s'", name)
} else if dbType := columnTypes[1].DatabaseTypeName(); dbType != "DECIMAL" {
t.Errorf("field 'number' has a wrong db type '%s'", dbType)
} else if columnTypes[1].ScanType().Kind() != reflect.Float64 {
t.Error("field 'number' has a wrong scanType")
} else if precision, scale, ok := columnTypes[1].DecimalSize(); precision != int64(10) || scale != int64(4) || !ok {
t.Error("field 'number' has a wrong precision, scale")
} else if _, ok := columnTypes[1].Length(); ok {
t.Error("field 'number' is not variable length type")
} else if _, ok := columnTypes[2].Nullable(); ok {
t.Error("field 'when' should have nullability unknown")
}
}
if rowsSi == 0 {
t.Error("no result set")
}
query.Close()
if false == isQueryClosed.fulfilled() {
t.Error("Query is not executed")
}
db.Close()
if false == isDbClosed.fulfilled() {
t.Error("Db is not closed")
}
})
}