-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
dbq_test.go
457 lines (365 loc) · 13.4 KB
/
dbq_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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// Copyright 2019-20 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved.
package dbq
import (
"context"
"database/sql/driver"
"fmt"
"testing"
"time"
"github.com/DATA-DOG/go-sqlmock"
"github.com/google/go-cmp/cmp"
"github.com/mitchellh/mapstructure"
)
type AnyTime struct{}
// Match satisfies sqlmock.Argument interface
func (a AnyTime) Match(v driver.Value) bool {
_, ok := v.(time.Time)
return ok
}
type store struct {
ID int64 `dbq:"id"`
Product string `dbq:"product"`
Price float64 `dbq:"price"`
Quantity int64 `dbq:"quantity"`
Available int64 `dbq:"available"`
DateAdded time.Time `dbq:"date_added"`
}
type store2 struct {
ID int64 `dbq:"id"`
Product string `dbq:"product"`
Price float64 `dbq:"price"`
Quantity int64 `dbq:"quantity"`
Available int64 `dbq:"available"`
DateAdded time.Time `dbq:"date_added"`
}
func (s *store2) PostUnmarshal(ctx context.Context, row, count int) error {
// This postUnmarshall method changes the timezone on DateAdded in Store struct
// From UTC to CEST (Europe/Budapest)
loc, err := time.LoadLocation("Europe/Budapest")
if err != nil {
return err
}
newTimeZone := s.DateAdded.In(loc)
s.DateAdded = newTimeZone
return nil
}
func TestMustQ(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()
// tRef := "2006-01-02 15:04:05"
tRef := time.Now()
rows := sqlmock.NewRows([]string{"id", "product", "price", "quantity", "available", "date_added"}).
AddRow(int64(1), "wrist watch", float64(45000.98), int64(6), int64(1), tRef).
AddRow(int64(2), "bags", float64(25089.55), int64(10), int64(0), tRef).
AddRow(int64(3), "car", float64(598000999.99), int64(3), int64(1), tRef)
expected := []interface{}{
&store{
ID: 1,
Product: "wrist watch",
Price: float64(45000.98),
Quantity: int64(6),
Available: int64(1),
DateAdded: tRef,
},
&store{
ID: 2,
Product: "bags",
Price: float64(25089.55),
Quantity: int64(10),
Available: int64(0),
DateAdded: tRef,
},
&store{
ID: 3,
Product: "car",
Price: float64(598000999.99),
Quantity: int64(3),
Available: int64(1),
DateAdded: tRef,
},
}
row := sqlmock.NewRows([]string{"id", "product", "price", "quantity", "available", "date_added"}).
AddRow(int64(1), "wrist watch", float64(45000.98), int64(6), int64(1), tRef)
mock.ExpectQuery("^SELECT (.+) FROM store$").WillReturnRows(rows) // Multiple result select query
mock.ExpectQuery("^SELECT (.+) FROM store.*$").WillReturnRows(sqlmock.NewRows(nil)) // zero result
mock.ExpectQuery("^SELECT (.+) FROM store LIMIT 1$").WillReturnRows(row) // single result
ctx := context.Background()
// Testing Multiple Data select with MustQ
opts := &Options{ConcreteStruct: store{}, DecoderConfig: &StructorConfig{
DecodeHook: mapstructure.StringToTimeHookFunc(time.RFC3339),
WeaklyTypedInput: true}}
actual := MustQ(ctx, db, "SELECT * FROM store", opts)
if !cmp.Equal(expected, actual) {
t.Errorf("wrong val: expected: %T %v actual: %T %v", expected, expected, actual, actual)
}
// Test zero data select query with MustQ
_, err = Q(ctx, db, "SELECT * FROM store WHERE id = 20", opts)
if err != nil {
t.Errorf("There was an error while executing statement: %s", err)
}
// Testing Single Data Select
opts2 := &Options{ConcreteStruct: store{}, DecoderConfig: &StructorConfig{
DecodeHook: mapstructure.StringToTimeHookFunc(time.RFC3339),
WeaklyTypedInput: true}, SingleResult: true}
// Test Select return at most 1 result
_ = MustQ(ctx, db, "SELECT * FROM store LIMIT 1", opts2)
// we make sure that all expectations were met
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
func TestMustE(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()
// tRef := "2006-01-02 15:04:05"
tRef := time.Now()
mock.ExpectQuery("^SELECT (.+) FROM store$").
WillReturnError(fmt.Errorf("There was error while executing statement"))
mock.ExpectExec("INSERT INTO store").
WithArgs(4, "mobile phone", 456787.45, 8, 1, tRef).
WillReturnResult(sqlmock.NewResult(1, 1))
// This is for batch Insert with MySQL
mock.ExpectExec("INSERT INTO store").
WithArgs(
int64(6), "Dish Washer", float64(45534.34), int64(34), int64(1), AnyTime{},
int64(7), "Sewing Machine", float64(9843.35), int64(8), int64(0), AnyTime{},
int64(8), "Private Jet", float64(98748594.34), int64(2), int64(1), AnyTime{}).
WillReturnResult(sqlmock.NewResult(1, 1))
// This is for batch insert with PostgreSQL
mock.ExpectExec("INSERT INTO store").
WithArgs(
int64(6), "Dish Washer", float64(45534.34), int64(34), int64(1), AnyTime{},
int64(7), "Sewing Machine", float64(9843.35), int64(8), int64(0), AnyTime{},
int64(8), "Private Jet", float64(98748594.34), int64(2), int64(1), AnyTime{}).
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec("UPDATE store SET product").
WithArgs("buckets", 2).
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec("DELETE FROM store").
WithArgs(int64(1)).
WillReturnResult(sqlmock.NewResult(1, 1))
ctx := context.Background()
// Error testing on Select Query
opts := &Options{ConcreteStruct: store{}, DecoderConfig: &StructorConfig{
DecodeHook: mapstructure.StringToTimeHookFunc("2006-01-02 15:04:05"),
WeaklyTypedInput: true}}
_, err = E(ctx, db, "SELECT * FROM store", opts)
if err == nil {
t.Errorf("was expecting an error, but there was none.")
}
// Testing Single Insert
insertArgs := []interface{}{4, "mobile phone", 456787.45, 8, 1, tRef}
_ = MustE(ctx, db, "INSERT INTO store(id, product, price, quantity, available, date_added) VALUES (?, ?, ?, ?, ?, ?)", nil, insertArgs)
// Testing Batch Insert
storeProducts := []interface{}{
[]interface{}{int64(6), "Dish Washer", float64(45534.34), int64(34), int64(1), tRef},
[]interface{}{int64(7), "Sewing Machine", float64(9843.35), int64(8), int64(0), tRef},
[]interface{}{int64(8), "Private Jet", float64(98748594.34), int64(2), int64(1), tRef},
}
// batch insert statement on MySQL
stmt := INSERT("store", []string{"id", "product", "price", "quantity", "available", "date_added"}, len(storeProducts), MySQL)
_ = MustE(ctx, db, stmt, opts, storeProducts)
// batch insert statement on PostgreSQL
stmt2 := INSERT("store", []string{"id", "product", "price", "quantity", "available", "date_added"}, len(storeProducts), PostgreSQL)
_ = MustE(ctx, db, stmt2, opts, storeProducts)
// Testing Data update with MustE
updateArgs := []interface{}{"buckets", 2}
_ = MustE(ctx, db, "UPDATE store SET product = ? WHERE id = ?", nil, updateArgs)
// Testing Delete from table store
_ = MustE(ctx, db, "DELETE FROM store WHERE ID = ?", nil, []interface{}{int64(1)})
// we make sure that all expectations were met
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
func TestPostUnmarshalConcurrent(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()
// tRef := "2006-01-02 15:04:05"
tRef := time.Now()
// convert tRef to new timezone
loc, err := time.LoadLocation("Europe/Budapest")
if err != nil {
t.Errorf("an unexpected error occurred %s", err)
}
newTref := tRef.In(loc)
rows := sqlmock.NewRows([]string{"id", "product", "price", "quantity", "available", "date_added"}).
AddRow(int64(1), "wrist watch", float64(45000.98), int64(6), int64(1), tRef).
AddRow(int64(2), "bags", float64(25089.55), int64(10), int64(0), tRef).
AddRow(int64(3), "car", float64(598000999.99), int64(3), int64(1), tRef)
expected := []interface{}{
&store2{
ID: 1,
Product: "wrist watch",
Price: float64(45000.98),
Quantity: int64(6),
Available: int64(1),
DateAdded: newTref,
},
&store2{
ID: 2,
Product: "bags",
Price: float64(25089.55),
Quantity: int64(10),
Available: int64(0),
DateAdded: newTref,
},
&store2{
ID: 3,
Product: "car",
Price: float64(598000999.99),
Quantity: int64(3),
Available: int64(1),
DateAdded: newTref,
},
}
mock.ExpectQuery("^SELECT (.+) FROM store$").WillReturnRows(rows) // Multiple result select query
ctx := context.Background()
// Testing Multiple Data select with MustQ
opts := &Options{ConcreteStruct: store2{}, DecoderConfig: &StructorConfig{
DecodeHook: mapstructure.StringToTimeHookFunc(time.RFC3339),
WeaklyTypedInput: true},
ConcurrentPostUnmarshal: true}
actual := MustQ(ctx, db, "SELECT * FROM store", opts)
if !cmp.Equal(expected, actual) {
t.Errorf("wrong val: expected: %T %v actual: %T %v", expected, expected, actual, actual)
}
}
func TestPostUnmarshalSequential(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()
// tRef := "2006-01-02 15:04:05"
tRef := time.Now()
// convert tRef to newtimezone
loc, err := time.LoadLocation("Europe/Budapest")
if err != nil {
t.Errorf("an unexpected error occurred %s", err)
}
newTref := tRef.In(loc)
rows := sqlmock.NewRows([]string{"id", "product", "price", "quantity", "available", "date_added"}).
AddRow(int64(1), "wrist watch", float64(45000.98), int64(6), int64(1), tRef).
AddRow(int64(2), "bags", float64(25089.55), int64(10), int64(0), tRef).
AddRow(int64(3), "car", float64(598000999.99), int64(3), int64(1), tRef)
expected := []interface{}{
&store2{
ID: 1,
Product: "wrist watch",
Price: float64(45000.98),
Quantity: int64(6),
Available: int64(1),
DateAdded: newTref,
},
&store2{
ID: 2,
Product: "bags",
Price: float64(25089.55),
Quantity: int64(10),
Available: int64(0),
DateAdded: newTref,
},
&store2{
ID: 3,
Product: "car",
Price: float64(598000999.99),
Quantity: int64(3),
Available: int64(1),
DateAdded: newTref,
},
}
mock.ExpectQuery("^SELECT (.+) FROM store$").WillReturnRows(rows) // Multiple result select query
ctx := context.Background()
// Testing Multiple Data select with MustQ
opts := &Options{ConcreteStruct: store2{}, DecoderConfig: &StructorConfig{
DecodeHook: mapstructure.StringToTimeHookFunc(time.RFC3339),
WeaklyTypedInput: true},
ConcurrentPostUnmarshal: false}
actual := MustQ(ctx, db, "SELECT * FROM store", opts)
if !cmp.Equal(expected, actual) {
t.Errorf("wrong val: expected: %T %v actual: %T %v", expected, expected, actual, actual)
}
}
func TestMultipleQueryRawResult(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()
rows := sqlmock.NewRows([]string{"ID", "Product", "Price", "Quantity", "Available"}).
AddRow([]byte("1"), []byte("wrist watch"), []byte("45000.98"), []byte("6"), []byte("1")).
AddRow([]byte("2"), []byte("bags"), []byte("25089.55"), []byte("10"), []byte("0")).
AddRow([]byte("3"), []byte("car"), []byte("598000999.99"), []byte("3"), []byte("1"))
expected := []map[string]interface{}{
{
"ID": []byte("1"),
"Product": []byte("wrist watch"),
"Price": []byte("45000.98"),
"Quantity": []byte("6"),
"Available": []byte("1"),
},
{
"ID": []byte("2"),
"Product": []byte("bags"),
"Price": []byte("25089.55"),
"Quantity": []byte("10"),
"Available": []byte("0"),
},
{
"ID": []byte("3"),
"Product": []byte("car"),
"Price": []byte("598000999.99"),
"Quantity": []byte("3"),
"Available": []byte("1"),
},
}
mock.ExpectQuery("^SELECT (.+) FROM store$").WillReturnRows(rows) // Multiple result select query
ctx := context.Background()
// Testing Multiple Data select with MustQ
opts := &Options{
RawResults: true,
}
actual := MustQ(ctx, db, "SELECT * FROM store", opts)
if !cmp.Equal(expected, actual) {
t.Errorf("wrong val: expected: %T %v actual: %T %v", expected, expected, actual, actual)
}
}
func TestSingleQueryRawResult(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()
rows := sqlmock.NewRows([]string{"ID", "Product", "Price", "Quantity", "Available"}).
AddRow([]byte("1"), []byte("wrist watch"), []byte("45000.98"), []byte("6"), []byte("1")).
AddRow([]byte("2"), []byte("bags"), []byte("25089.55"), []byte("10"), []byte("0")).
AddRow([]byte("3"), []byte("car"), []byte("598000999.99"), []byte("3"), []byte("1"))
mock.ExpectQuery("^SELECT (.+) FROM store LIMIT 1$").WillReturnRows(rows) // Multiple result select query
ctx := context.Background()
// Testing Multiple Data select with MustQ
opts := &Options{
RawResults: true,
SingleResult: true,
}
expected := map[string]interface{}{
"ID": []byte("1"),
"Product": []byte("wrist watch"),
"Price": []byte("45000.98"),
"Quantity": []byte("6"),
"Available": []byte("1"),
}
actual := MustQ(ctx, db, "SELECT * FROM store LIMIT 1", opts)
if !cmp.Equal(expected, actual) {
t.Errorf("wrong val: expected: %T %v actual: %T %v", expected, expected, actual, actual)
}
}