-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquerier_test.go
348 lines (310 loc) · 11.7 KB
/
querier_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
package satomic_test
import (
"context"
"database/sql"
"errors"
"io/ioutil"
"testing"
)
import (
sqlmock "github.com/DATA-DOG/go-sqlmock"
)
import (
"github.com/dhui/satomic"
"github.com/dhui/satomic/satomictest"
"github.com/dhui/satomic/savepointers"
"github.com/dhui/satomic/savepointers/mock"
)
func TestDefaultQuerierAtomicNoSavepoint(t *testing.T) {
beginErr := errors.New("begin error")
expectedBeginErr := satomictest.NewError(nil, beginErr)
selectErr := errors.New("select 1 error")
expectedSelectErr := satomictest.NewError(selectErr, nil)
rbErr := errors.New("rollback error")
expectedRbErr := satomictest.NewError(selectErr, rbErr)
commitErr := errors.New("commit error")
expectedCommitErr := satomictest.NewError(nil, commitErr)
testCases := []struct {
name string
mocker func(sqlmock.Sqlmock) sqlmock.Sqlmock
expectedErr *satomic.Error
}{
{name: "success", mocker: func(m sqlmock.Sqlmock) sqlmock.Sqlmock {
m.ExpectBegin()
m.ExpectQuery("SELECT 1;").WillReturnRows(sqlmock.NewRows([]string{""}).AddRow(1))
m.ExpectCommit()
return m
}, expectedErr: nil},
{name: "begin err", mocker: func(m sqlmock.Sqlmock) sqlmock.Sqlmock {
m.ExpectBegin().WillReturnError(beginErr)
return m
}, expectedErr: expectedBeginErr},
{name: "commit error", mocker: func(m sqlmock.Sqlmock) sqlmock.Sqlmock {
m.ExpectBegin()
m.ExpectQuery("SELECT 1;").WillReturnRows(sqlmock.NewRows([]string{""}).AddRow(1))
m.ExpectCommit().WillReturnError(commitErr)
return m
}, expectedErr: expectedCommitErr},
{name: "select error", mocker: func(m sqlmock.Sqlmock) sqlmock.Sqlmock {
m.ExpectBegin()
m.ExpectQuery("SELECT 1;").WillReturnError(selectErr)
m.ExpectRollback()
return m
}, expectedErr: expectedSelectErr},
{name: "select error, rollback error", mocker: func(m sqlmock.Sqlmock) sqlmock.Sqlmock {
m.ExpectBegin()
m.ExpectQuery("SELECT 1;").WillReturnError(selectErr)
m.ExpectRollback().WillReturnError(rbErr)
return m
}, expectedErr: expectedRbErr},
}
ctx := context.Background()
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
db, _sqlmock, err := sqlmock.New()
if err != nil {
t.Fatal("Error creating sqlmock:", err)
}
defer db.Close() // nolint:errcheck
_sqlmock = tc.mocker(_sqlmock)
q, err := satomic.NewQuerier(ctx, db, mock.NewSavepointer(ioutil.Discard, true), sql.TxOptions{})
if err != nil {
t.Fatal("Error creating Querier:", err)
}
if err := q.Atomic(func(ctx context.Context, q satomic.Querier) error {
var dummy int
return q.QueryRowContext(ctx, "SELECT 1;").Scan(&dummy)
}); !satomictest.ErrsEq(err, tc.expectedErr) {
t.Errorf("Didn't get the expected error: %+v != %+v", err, tc.expectedErr)
}
if err := _sqlmock.ExpectationsWereMet(); err != nil {
t.Error(err)
}
})
}
}
func TestDefaultQuerierAtomicNoSavepointPanic(t *testing.T) {
rbErr := errors.New("rollback error")
expectedRbErr := satomictest.NewError(nil, rbErr)
testCases := []struct {
name string
mocker func(sqlmock.Sqlmock) sqlmock.Sqlmock
panicVal interface{}
expectedErr *satomic.Error
}{
{name: "rollback success", mocker: func(m sqlmock.Sqlmock) sqlmock.Sqlmock {
m.ExpectBegin()
m.ExpectQuery("SELECT 1;").WillReturnRows(sqlmock.NewRows([]string{""}).AddRow(1))
m.ExpectRollback()
return m
}, panicVal: "whoa!", expectedErr: nil},
{name: "rollback error", mocker: func(m sqlmock.Sqlmock) sqlmock.Sqlmock {
m.ExpectBegin()
m.ExpectQuery("SELECT 1;").WillReturnRows(sqlmock.NewRows([]string{""}).AddRow(1))
m.ExpectRollback().WillReturnError(rbErr)
return m
}, panicVal: "whoa!", expectedErr: expectedRbErr},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
db, _sqlmock, err := sqlmock.New()
if err != nil {
t.Fatal("Error creating sqlmock:", err)
}
defer db.Close() // nolint:errcheck
_sqlmock = tc.mocker(_sqlmock)
ctx := context.Background()
q, err := satomic.NewQuerier(ctx, db, mock.NewSavepointer(ioutil.Discard, true),
sql.TxOptions{})
if err != nil {
t.Fatal("Error creating Querier:", err)
}
defer func() {
if r := recover(); r != tc.panicVal {
t.Errorf("Didn't get the expected panic value: %+v != %+v", r, tc.panicVal)
}
}()
if err := q.Atomic(func(ctx context.Context, q satomic.Querier) error {
var dummy int
if err := q.QueryRowContext(ctx, "SELECT 1;").Scan(&dummy); err != nil {
t.Error(err)
}
panic(tc.panicVal)
}); !satomictest.ErrsEq(err, tc.expectedErr) {
t.Errorf("Didn't get the expected error: %+v != %+v", err, tc.expectedErr)
}
if err := _sqlmock.ExpectationsWereMet(); err != nil {
t.Error(err)
}
})
}
}
func TestDefaultQuerierAtomicSingleSavepointReleased(t *testing.T) {
rbErr := errors.New("rollback error")
expectedRbErr := satomictest.NewError(nil, rbErr)
createErr := errors.New("savepoint error")
expectedCreateErr := satomictest.NewError(nil, createErr)
selectErr := errors.New("select 2 error")
expectedSelectErr := satomictest.NewError(selectErr, nil)
releaseErr := errors.New("release error")
expectedReleaseErr := satomictest.NewError(selectErr, releaseErr)
savepointer := func(release bool) *mock.Savepointer {
return mock.NewSavepointer(ioutil.Discard, release)
}
testCases := []struct {
name string
savepointer *mock.Savepointer
mocker func(sqlmock.Sqlmock) sqlmock.Sqlmock
expectedErr *satomic.Error
}{
{name: "success", savepointer: savepointer(true), mocker: func(m sqlmock.Sqlmock) sqlmock.Sqlmock {
m.ExpectBegin()
m.ExpectQuery("SELECT 1;").WillReturnRows(sqlmock.NewRows([]string{""}).AddRow(1))
m.ExpectExec("SAVEPOINT 1;").WillReturnResult(sqlmock.NewResult(0, 0))
m.ExpectQuery("SELECT 2;").WillReturnRows(sqlmock.NewRows([]string{""}).AddRow(2))
m.ExpectExec("RELEASE 1;").WillReturnResult(sqlmock.NewResult(0, 0))
m.ExpectCommit()
return m
}, expectedErr: nil},
{name: "no release", savepointer: savepointer(false), mocker: func(m sqlmock.Sqlmock) sqlmock.Sqlmock {
m.ExpectBegin()
m.ExpectQuery("SELECT 1;").WillReturnRows(sqlmock.NewRows([]string{""}).AddRow(1))
m.ExpectExec("SAVEPOINT 1;").WillReturnResult(sqlmock.NewResult(0, 0))
m.ExpectQuery("SELECT 2;").WillReturnRows(sqlmock.NewRows([]string{""}).AddRow(2))
m.ExpectCommit()
return m
}, expectedErr: nil},
{name: "release error", savepointer: savepointer(true), mocker: func(m sqlmock.Sqlmock) sqlmock.Sqlmock {
m.ExpectBegin()
m.ExpectQuery("SELECT 1;").WillReturnRows(sqlmock.NewRows([]string{""}).AddRow(1))
m.ExpectExec("SAVEPOINT 1;").WillReturnResult(sqlmock.NewResult(0, 0))
m.ExpectQuery("SELECT 2;").WillReturnRows(sqlmock.NewRows([]string{""}).AddRow(2))
m.ExpectExec("RELEASE 1;").WillReturnError(rbErr)
m.ExpectCommit()
return m
}, expectedErr: expectedRbErr},
{name: "create error", savepointer: savepointer(true), mocker: func(m sqlmock.Sqlmock) sqlmock.Sqlmock {
m.ExpectBegin()
m.ExpectQuery("SELECT 1;").WillReturnRows(sqlmock.NewRows([]string{""}).AddRow(1))
m.ExpectExec("SAVEPOINT 1;").WillReturnError(createErr)
m.ExpectCommit()
return m
}, expectedErr: expectedCreateErr},
{name: "select error", savepointer: savepointer(true), mocker: func(m sqlmock.Sqlmock) sqlmock.Sqlmock {
m.ExpectBegin()
m.ExpectQuery("SELECT 1;").WillReturnRows(sqlmock.NewRows([]string{""}).AddRow(1))
m.ExpectExec("SAVEPOINT 1;").WillReturnResult(sqlmock.NewResult(0, 0))
m.ExpectQuery("SELECT 2;").WillReturnError(selectErr)
m.ExpectExec("ROLLBACK TO 1;").WillReturnResult(sqlmock.NewResult(0, 0))
m.ExpectCommit()
return m
}, expectedErr: expectedSelectErr},
{name: "rollback error", savepointer: savepointer(true), mocker: func(m sqlmock.Sqlmock) sqlmock.Sqlmock {
m.ExpectBegin()
m.ExpectQuery("SELECT 1;").WillReturnRows(sqlmock.NewRows([]string{""}).AddRow(1))
m.ExpectExec("SAVEPOINT 1;").WillReturnResult(sqlmock.NewResult(0, 0))
m.ExpectQuery("SELECT 2;").WillReturnError(selectErr)
m.ExpectExec("ROLLBACK TO 1;").WillReturnError(releaseErr)
m.ExpectCommit()
return m
}, expectedErr: expectedReleaseErr},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
db, _sqlmock, err := sqlmock.New()
if err != nil {
t.Fatal("Error creating sqlmock:", err)
}
defer db.Close() // nolint:errcheck
_sqlmock = tc.mocker(_sqlmock)
ctx := context.Background()
q, err := satomic.NewQuerier(ctx, db, tc.savepointer, sql.TxOptions{})
if err != nil {
t.Fatal("Error creating Querier:", err)
}
if err := q.Atomic(func(ctx context.Context, q satomic.Querier) error {
var dummy int
if err := q.QueryRowContext(ctx, "SELECT 1;").Scan(&dummy); err != nil {
t.Log("error in select 1:", err)
return err
}
if err := q.Atomic(func(ctx context.Context, q satomic.Querier) error {
return q.QueryRowContext(ctx, "SELECT 2;").Scan(&dummy)
}); !satomictest.ErrsEq(err, tc.expectedErr) {
t.Errorf("Didn't get the expected error: %+v != %+v", err, tc.expectedErr)
}
return nil
}); err != nil {
t.Error(err)
}
if err := _sqlmock.ExpectationsWereMet(); err != nil {
t.Error(err)
}
})
}
}
func TestNewQuerierWithTxCreator(t *testing.T) {
noopMocker := func(m sqlmock.Sqlmock) sqlmock.Sqlmock { return m }
pingErr := errors.New("ping error")
pingErrMocker := func(m sqlmock.Sqlmock) sqlmock.Sqlmock {
m.ExpectPing().WillReturnError(pingErr)
return m
}
getDb := func() (*sql.DB, sqlmock.Sqlmock) {
db, _sqlmock, err := sqlmock.New()
if err != nil {
t.Fatal("Error creating sqlmock:", err)
}
return db, _sqlmock
}
testCases := []struct {
name string
mocker func(sqlmock.Sqlmock) sqlmock.Sqlmock
getDb func() (*sql.DB, sqlmock.Sqlmock)
savepointer savepointers.Savepointer
txCreator satomic.TxCreator
expectedErr error
}{
{name: "nil db", mocker: noopMocker,
getDb: func() (*sql.DB, sqlmock.Sqlmock) {
_, _sqlmock := getDb()
return nil, _sqlmock
}, savepointer: mock.NewSavepointer(ioutil.Discard, true), txCreator: satomic.DefaultTxCreator,
expectedErr: satomic.ErrNeedsDb},
{name: "nil savepointer", mocker: noopMocker, getDb: getDb, savepointer: nil,
txCreator: satomic.DefaultTxCreator, expectedErr: satomic.ErrNeedsSavepointer},
{name: "success", mocker: noopMocker, getDb: getDb, savepointer: mock.NewSavepointer(ioutil.Discard, true),
txCreator: satomic.DefaultTxCreator, expectedErr: nil},
{name: "success - nil TxCreator", mocker: noopMocker, getDb: getDb,
savepointer: mock.NewSavepointer(ioutil.Discard, true), txCreator: nil, expectedErr: nil},
{name: "error", mocker: pingErrMocker, getDb: func() (*sql.DB, sqlmock.Sqlmock) {
db, _sqlmock, err := sqlmock.New(sqlmock.MonitorPingsOption(true))
if err != nil {
t.Fatal("Error creating sqlmock:", err)
}
return db, _sqlmock
}, savepointer: mock.NewSavepointer(ioutil.Discard, true),
txCreator: satomic.DefaultTxCreator, expectedErr: pingErr},
}
ctx := context.Background()
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
db, _sqlmock := tc.getDb()
_sqlmock = tc.mocker(_sqlmock)
if _, err := satomic.NewQuerierWithTxCreator(ctx, db, tc.savepointer,
sql.TxOptions{}, tc.txCreator); err != tc.expectedErr {
t.Errorf("Didn't get the expected error: %+v != %+v", err, tc.expectedErr)
}
if err := _sqlmock.ExpectationsWereMet(); err != nil {
t.Error(err)
}
})
}
}
func TestQuerierBaseImplementers(t *testing.T) { //nolint:revive
f := func(qb satomic.QuerierBase) {}
// Test that sql.DB implements the satomic.QuerierBase interface
f(&sql.DB{})
// Test that sql.Tx implements the satomic.QuerierBase interface
f(&sql.Tx{})
}