-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_test.go
420 lines (313 loc) · 9.88 KB
/
storage_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
package sqluct_test
import (
"context"
"database/sql/driver"
"errors"
"fmt"
"testing"
sqlmock "github.com/DATA-DOG/go-sqlmock"
"github.com/Masterminds/squirrel"
"github.com/bool64/sqluct"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type (
dumpConnector struct{}
dumpDriver struct{}
dumpConn struct{}
dumpStmt struct{ query string }
)
func (d dumpStmt) Close() error {
return nil
}
func (d dumpStmt) NumInput() int {
return -1
}
func (d dumpStmt) Exec(args []driver.Value) (driver.Result, error) {
fmt.Println("exec", d.query, args)
return nil, errors.New("skip")
}
func (d dumpStmt) Query(args []driver.Value) (driver.Rows, error) {
fmt.Println("query", d.query, args)
return nil, errors.New("skip")
}
func (d dumpConn) Prepare(query string) (driver.Stmt, error) {
return dumpStmt{query: query}, nil
}
func (d dumpConn) Close() error {
return nil
}
func (d dumpConn) Begin() (driver.Tx, error) {
return nil, nil //nolint:nilnil
}
func (d dumpDriver) Open(name string) (driver.Conn, error) {
fmt.Println("open", name)
return dumpConn{}, nil
}
func (d dumpConnector) Connect(_ context.Context) (driver.Conn, error) {
return dumpConn{}, nil
}
func (d dumpConnector) Driver() driver.Driver {
return dumpDriver{}
}
func TestStorage_InTx_FailToStart(t *testing.T) {
db, mock, err := sqlmock.New()
require.NoError(t, err)
type ctxKey struct{}
ctx := context.WithValue(context.Background(), ctxKey{}, "a")
errReceived := false
st := sqluct.NewStorage(sqlx.NewDb(db, "mock"))
st.OnError = func(ctx context.Context, err error) {
errReceived = true
assert.Equal(t, "a", ctx.Value(ctxKey{}))
require.EqualError(t, err, "failed to begin tx: begin error")
}
mock.ExpectBegin().WillReturnError(errors.New("begin error"))
err = st.InTx(ctx, nil)
require.EqualError(t, err, "failed to begin tx: begin error")
require.NoError(t, mock.ExpectationsWereMet())
assert.True(t, errReceived)
}
func TestStorage_InTx_Success(t *testing.T) {
db, mock, err := sqlmock.New()
require.NoError(t, err)
st := sqluct.NewStorage(sqlx.NewDb(db, "mock"))
mock.ExpectBegin()
mock.ExpectCommit()
err = st.InTx(context.TODO(), func(_ context.Context) error {
return nil
})
require.NoError(t, err)
require.NoError(t, mock.ExpectationsWereMet())
}
func TestStorage_InTx_ReuseTx(t *testing.T) {
db, mock, err := sqlmock.New()
require.NoError(t, err)
dbx := sqlx.NewDb(db, "mock")
st := sqluct.NewStorage(dbx)
mock.ExpectBegin()
// We don't expect COMMIT because it's not the beginner.
// Manually start a transaction.
tx, err := dbx.BeginTxx(context.TODO(), nil)
require.NoError(t, err)
ctx := sqluct.TxToContext(context.TODO(), tx)
counter := 0
// Start using transaction.
err = st.InTx(ctx, func(_ context.Context) error {
counter++
return nil
})
assert.Equal(t, 1, counter)
require.NoError(t, err)
require.NoError(t, mock.ExpectationsWereMet())
}
func TestStorage_InTx_RollbackOnError(t *testing.T) {
db, mock, err := sqlmock.New()
require.NoError(t, err)
st := sqluct.NewStorage(sqlx.NewDb(db, "mock"))
mock.ExpectBegin()
mock.ExpectRollback()
// Start using transaction.
err = st.InTx(context.TODO(), func(_ context.Context) error {
return errors.New("error")
})
require.EqualError(t, err, "error")
require.NoError(t, mock.ExpectationsWereMet())
}
func TestStorage_InTx_ErrorOnRollback(t *testing.T) {
db, mock, err := sqlmock.New()
require.NoError(t, err)
st := sqluct.NewStorage(sqlx.NewDb(db, "mock"))
mock.ExpectBegin()
mock.ExpectRollback().WillReturnError(errors.New("rollback error"))
// Start using transaction.
err = st.InTx(context.TODO(), func(_ context.Context) error {
return errors.New("error")
})
require.EqualError(t, err, "failed to rollback: rollback error")
require.NoError(t, mock.ExpectationsWereMet())
}
func TestStorage_InTx_FailToCommit(t *testing.T) {
db, mock, err := sqlmock.New()
require.NoError(t, err)
st := sqluct.NewStorage(sqlx.NewDb(db, "mock"))
mock.ExpectBegin()
mock.ExpectCommit().WillReturnError(errors.New("commit error"))
// Start using transaction.
err = st.InTx(context.TODO(), func(_ context.Context) error {
return nil
})
require.EqualError(t, err, "failed to commit: commit error")
require.NoError(t, mock.ExpectationsWereMet())
}
func TestStorage_SelectContext_slice(t *testing.T) {
type row struct {
One int `db:"one"`
Two int `db:"two"`
Three int `db:"three"`
}
db, mock, err := sqlmock.New()
require.NoError(t, err)
traceStarted := false
traceFinished := false
st := sqluct.NewStorage(sqlx.NewDb(db, "mock"))
st.Trace = func(ctx context.Context, stmt string, args []interface{}) (newCtx context.Context, onFinish func(error)) {
traceStarted = true
assert.Equal(t, "SELECT one, two, three FROM table", stmt)
assert.Empty(t, args)
return ctx, func(err error) {
traceFinished = true
require.NoError(t, err)
}
}
qb := st.QueryBuilder().Select("one", "two", "three").From("table")
rows := make([]row, 0, 100)
ctx := context.Background()
mockedRows := sqlmock.NewRows([]string{"one", "two", "three"})
for i := 0; i < 100; i++ {
mockedRows.AddRow(i, 2*i, 3*i)
}
mock.ExpectQuery("SELECT one, two, three FROM table").WillReturnRows(mockedRows)
err = st.Select(ctx, qb, &rows)
require.NoError(t, err)
i := 0
for _, item := range rows {
assert.Equal(t, row{One: i, Two: 2 * i, Three: 3 * i}, item)
i++
}
assert.Equal(t, 100, i)
assert.True(t, traceStarted)
assert.True(t, traceFinished)
}
func TestStorage_SelectContext_row(t *testing.T) {
type row struct {
One int `db:"one"`
Two int `db:"two"`
Three int `db:"three"`
}
db, mock, err := sqlmock.New()
require.NoError(t, err)
st := sqluct.NewStorage(sqlx.NewDb(db, "mock"))
qb := st.SelectStmt("table", row{})
var item row
ctx := context.Background()
mockedRows := sqlmock.NewRows([]string{"one", "two", "three"})
mockedRows.AddRow(1, 2, 3)
mock.ExpectQuery("SELECT one, two, three FROM table").WillReturnRows(mockedRows)
err = st.Select(ctx, qb, &item)
require.NoError(t, err)
assert.Equal(t, row{One: 1, Two: 2, Three: 3}, item)
}
func TestStorage_ExecContext(t *testing.T) {
db, mock, err := sqlmock.New()
require.NoError(t, err)
traceStarted := false
traceFinished := false
st := sqluct.NewStorage(sqlx.NewDb(db, "mock"))
st.Trace = func(ctx context.Context, stmt string, args []interface{}) (newCtx context.Context, onFinish func(error)) {
traceStarted = true
assert.Equal(t, "DELETE FROM table", stmt)
assert.Empty(t, args)
return ctx, func(err error) {
traceFinished = true
require.NoError(t, err)
}
}
mock.ExpectExec("DELETE FROM table").WillReturnResult(sqlmock.NewResult(0, 1))
qb := st.DeleteStmt("table")
_, err = st.Exec(context.Background(), qb)
require.NoError(t, err)
assert.True(t, traceStarted)
assert.True(t, traceFinished)
}
func TestStorage_DeleteStmt_backticks(t *testing.T) {
db, mock, err := sqlmock.New()
require.NoError(t, err)
st := sqluct.NewStorage(sqlx.NewDb(db, "mock"))
st.Format = squirrel.Dollar
st.IdentifierQuoter = sqluct.QuoteBackticks
mock.ExpectExec("DELETE FROM `table`").
WillReturnResult(sqlmock.NewResult(0, 1))
_, err = st.DeleteStmt("table").Exec()
require.NoError(t, err)
require.NoError(t, mock.ExpectationsWereMet())
}
func TestStorage_InsertStmt_backticks(t *testing.T) {
db, mock, err := sqlmock.New()
require.NoError(t, err)
st := sqluct.NewStorage(sqlx.NewDb(db, "mock"))
st.Format = squirrel.Dollar
st.IdentifierQuoter = sqluct.QuoteBackticks
mock.ExpectExec("INSERT INTO `table` \\(`order_id`,`amount`\\) VALUES \\(\\$1,\\$2\\)").
WithArgs(10, 20).WillReturnResult(sqlmock.NewResult(1, 1))
_, err = st.InsertStmt("table", struct {
OrderID int `db:"order_id"`
Amount int `db:"amount"`
}{10, 20}).Exec()
require.NoError(t, err)
require.NoError(t, mock.ExpectationsWereMet())
}
func TestStorage_UpdateStmt(t *testing.T) {
st := sqluct.Storage{
Format: squirrel.Dollar,
}
query, args, err := st.UpdateStmt("table", struct {
OrderID int `db:"order_id"`
Amount int `db:"amount"`
}{10, 20}).ToSql()
require.NoError(t, err)
assert.Equal(t, "UPDATE table SET order_id = $1, amount = $2", query)
assert.Equal(t, []interface{}{10, 20}, args)
}
func TestStorage_UpdateStmt_ansi(t *testing.T) {
st := sqluct.Storage{
Format: squirrel.Dollar,
}
st.IdentifierQuoter = sqluct.QuoteANSI
query, args, err := st.UpdateStmt("table", struct {
OrderID int `db:"order_id"`
Amount int `db:"amount"`
}{10, 20}).ToSql()
require.NoError(t, err)
assert.Equal(t, `UPDATE "table" SET "order_id" = $1, "amount" = $2`, query)
assert.Equal(t, []interface{}{10, 20}, args)
}
func TestStorage_SelectStmt(t *testing.T) {
st := sqluct.NewStorage(nil)
query, _, err := st.SelectStmt("table", struct {
OrderID int `db:"order_id"`
Amount int `db:"amount"`
}{}).ToSql()
require.NoError(t, err)
assert.Equal(t, "SELECT order_id, amount FROM table", query)
}
func TestStorage_SelectStmt_backticks(t *testing.T) {
st := sqluct.NewStorage(nil)
st.IdentifierQuoter = sqluct.QuoteBackticks
query, _, err := st.SelectStmt("table", struct {
OrderID int `db:"order_id"`
Amount int `db:"amount"`
}{}).ToSql()
require.NoError(t, err)
assert.Equal(t, "SELECT `order_id`, `amount` FROM `table`", query)
}
func TestStorage_DB(t *testing.T) {
db, _, err := sqlmock.New()
require.NoError(t, err)
dbx := sqlx.NewDb(db, "test")
st := sqluct.NewStorage(dbx)
assert.Equal(t, dbx, st.DB())
}
func TestStmt_ToSql(t *testing.T) {
s, a, err := sqluct.Stmt("SELECT * FROM foo WHERE id=? AND name=?", 1, "bar").ToSql()
assert.Equal(t, "SELECT * FROM foo WHERE id=? AND name=?", s)
assert.Equal(t, []interface{}{1, "bar"}, a)
require.NoError(t, err)
}
func TestPlain_ToSql(t *testing.T) {
s, a, err := sqluct.Plain("SELECT * FROM foo WHERE id=1 AND name='bar'").ToSql()
assert.Equal(t, "SELECT * FROM foo WHERE id=1 AND name='bar'", s)
assert.Nil(t, a)
require.NoError(t, err)
}