-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathsqlserver_test.go
383 lines (288 loc) · 9.79 KB
/
sqlserver_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
package sqlserver
import (
"context"
"errors"
"regexp"
"testing"
"time"
sqlmock "github.com/DATA-DOG/go-sqlmock"
"github.com/italolelis/outboxer"
"github.com/italolelis/outboxer/lock"
)
func TestSQLServer_WithInstance_must_return_SQLServerDataStore(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
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()
initDatastoreMock(t, mock)
ds, err := WithInstance(ctx, db)
if err != nil {
t.Fatalf("failed to setup the data store: %s", err)
}
defer ds.Close()
if ds.SchemaName != "test_schema" {
t.Errorf("Expected schema name %s but got %s", "test_schema", ds.SchemaName)
}
if ds.DatabaseName != "test" {
t.Errorf("Expected database name %s but got %s", "test", ds.DatabaseName)
}
}
func TestSQLServer_WithInstance_should_return_error_when_no_db_selected(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
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()
mock.ExpectQuery(`SELECT DB_NAME() `).
WillReturnRows(sqlmock.NewRows([]string{"DB_NAME()"}).AddRow(""))
_, err = WithInstance(ctx, db)
if err != ErrNoDatabaseName {
t.Fatalf("Expected ErrNoDatabaseName to be returned when no database selected : %s", err)
}
}
func TestSQLServer_WithInstance_should_return_error_when_no_schema_selected(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
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()
mock.ExpectQuery(`SELECT DB_NAME() `).
WillReturnRows(sqlmock.NewRows([]string{"DB_NAME()"}).AddRow("test"))
mock.ExpectQuery(`SELECT SCHEMA_NAME()`).
WillReturnRows(sqlmock.NewRows([]string{"SCHEMA_NAME()"}).AddRow(""))
_, err = WithInstance(ctx, db)
if err != ErrNoSchema {
t.Fatalf("Expected ErrNoSchema to be returned when no schema selected : %s", err)
}
}
func TestSQLServer_should_add_message(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
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()
initDatastoreMock(t, mock)
ds, err := WithInstance(ctx, db)
if err != nil {
t.Fatalf("failed to setup the data store: %s", err)
}
defer ds.Close()
mock.ExpectExec(regexp.QuoteMeta(`INSERT INTO [test_schema].[event_store]`)).
WillReturnResult(sqlmock.NewResult(0, 1))
if err := ds.Add(ctx, &outboxer.OutboxMessage{
Payload: []byte("test payload"),
}); err != nil {
t.Fatalf("failed to add message in the data store: %s", err)
}
}
func TestSQLServer_should_add_message_with_tx(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
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()
initDatastoreMock(t, mock)
ds, err := WithInstance(ctx, db)
if err != nil {
t.Fatalf("failed to setup the data store: %s", err)
}
defer ds.Close()
mock.ExpectBegin()
mock.ExpectExec(`SELECT (.+) from event_store`).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectExec(regexp.QuoteMeta(`INSERT INTO [test_schema].[event_store]`)).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectCommit()
fn := func(tx outboxer.ExecerContext) error {
_, err := tx.ExecContext(ctx, "SELECT * from event_store")
return err
}
if err := ds.AddWithinTx(ctx, &outboxer.OutboxMessage{
Payload: []byte("test payload"),
}, fn); err != nil {
t.Fatalf("failed to add message in the data store: %s", err)
}
}
func TestSQLServer_add_message_with_tx_should_rollback_on_error(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
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()
initDatastoreMock(t, mock)
ds, err := WithInstance(ctx, db)
if err != nil {
t.Fatalf("failed to setup the data store: %s", err)
}
defer ds.Close()
mock.ExpectBegin()
mock.ExpectExec(`SELECT (.+) from event_store`).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectExec(regexp.QuoteMeta(`INSERT INTO [test_schema].[event_store]`)).
WillReturnError(errors.New("Failed to insert"))
mock.ExpectRollback()
fn := func(tx outboxer.ExecerContext) error {
_, err := tx.ExecContext(ctx, "SELECT * from event_store")
return err
}
if err := ds.AddWithinTx(ctx, &outboxer.OutboxMessage{
Payload: []byte("test payload"),
}, fn); err == nil {
t.Fatalf("This should fail and rollback: %s", err)
}
}
func TestSQLServer_should_get_events(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
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()
initDatastoreMock(t, mock)
ds, err := WithInstance(ctx, db)
if err != nil {
t.Fatalf("failed to setup the data store: %s", err)
}
defer ds.Close()
mock.ExpectQuery(regexp.QuoteMeta(`SELECT TOP 10 * FROM [test_schema].[event_store] WHERE dispatched = 0`)).
WillReturnRows(sqlmock.NewRows([]string{"id", "dispatched", "dispatched_at", "payload", "options", "headers"}).
AddRow(1, false, time.Now(), []byte("test payload"), outboxer.DynamicValues{}, outboxer.DynamicValues{}))
msgs, err := ds.GetEvents(ctx, 10)
if err != nil {
t.Fatalf("failed to retrieve messages from the data store: %s", err)
}
if len(msgs) != 1 {
t.Fatalf("was expecting 1 message in the data store but got %d", len(msgs))
}
}
func TestSQLServer_should_set_as_dispatched(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
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()
initDatastoreMock(t, mock)
ds, err := WithInstance(ctx, db)
if err != nil {
t.Fatalf("failed to setup the data store: %s", err)
}
defer ds.Close()
mock.ExpectExec(regexp.QuoteMeta(`UPDATE [test_schema].[event_store] SET`)).
WithArgs(1).
WillReturnResult(sqlmock.NewResult(0, 1))
err = ds.SetAsDispatched(ctx, 1)
if err != nil {
t.Fatalf("failed to set message as dispatched: %s", err)
}
}
func TestSQLServer_should_remove_messages(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
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()
initDatastoreMock(t, mock)
ds, err := WithInstance(ctx, db)
if err != nil {
t.Fatalf("failed to setup the data store: %s", err)
}
defer ds.Close()
mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta(`DELETE FROM [test_schema].[event_store] WHERE id IN`)).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectCommit()
if err := ds.Remove(ctx, time.Now(), 10); err != nil {
t.Fatalf("failed to remove messages: %s", err)
}
}
func TestRemoveMessages_Rollback(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
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()
initDatastoreMock(t, mock)
ds, err := WithInstance(ctx, db)
if err != nil {
t.Fatalf("failed to setup the data store: %s", err)
}
defer ds.Close()
mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta(`DELETE FROM [test_schema].[event_store] WHERE id IN`)).
WillReturnError(errors.New("Failed to delete"))
mock.ExpectRollback()
if err := ds.Remove(ctx, time.Now(), 10); err == nil {
t.Fatal("expected an error but got none")
}
}
func TestRemoveMessages_Failed_Rollback(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
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()
initDatastoreMock(t, mock)
ds, err := WithInstance(ctx, db)
if err != nil {
t.Fatalf("failed to setup the data store: %s", err)
}
defer ds.Close()
mock.ExpectBegin()
mock.ExpectExec(regexp.QuoteMeta(`DELETE FROM [test_schema].[event_store] WHERE id IN`)).
WillReturnError(errors.New("Failed to delete"))
mock.ExpectRollback().
WillReturnError(errors.New("Failed to rollback"))
if err := ds.Remove(ctx, time.Now(), 10); err == nil {
t.Fatal("expected an error but got none")
}
}
func initDatastoreMock(t *testing.T, mock sqlmock.Sqlmock) {
mock.ExpectQuery(`SELECT DB_NAME() `).
WillReturnRows(sqlmock.NewRows([]string{"DB_NAME()"}).AddRow("test"))
mock.ExpectQuery(`SELECT SCHEMA_NAME()`).
WillReturnRows(sqlmock.NewRows([]string{"SCHEMA_NAME()"}).AddRow("test_schema"))
initLockMock(t, mock)
}
func initLockMock(t *testing.T, mock sqlmock.Sqlmock) {
aid, err := lock.Generate("test", "test_schema")
if err != nil {
t.Fatalf("failed to generate the lock value: %s", err)
}
mock.ExpectExec(`EXEC sp_getapplock
@Resource = @p1,
@LockOwner='Session',
@LockMode = 'Exclusive'; `).
WithArgs(aid).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectExec(
regexp.QuoteMeta(`IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='event_store' and xtype='U')
CREATE TABLE test_schema.event_store`)).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectExec(`EXEC sp_releaseapplock
@Resource = @p1,
@LockOwner='Session'; `).
WithArgs(aid).
WillReturnResult(sqlmock.NewResult(0, 1))
}