-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathpostgres_test.go
222 lines (172 loc) · 5.85 KB
/
postgres_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
package postgres
import (
"context"
"errors"
"testing"
"time"
sqlmock "github.com/DATA-DOG/go-sqlmock"
"github.com/italolelis/outboxer"
"github.com/italolelis/outboxer/lock"
)
// nolint
func TestPostgres_AddSuccessfully(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(`INSERT INTO event_store (.+) VALUES (.+)`).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectCommit()
mock.ExpectQuery(`SELECT (.+) FROM event_store WHERE dispatched = false LIMIT 10`).
WillReturnRows(sqlmock.NewRows([]string{"id", "dispatched", "dispatched_at", "payload", "options", "headers"}).
AddRow(1, false, time.Now(), []byte("test payload"), outboxer.DynamicValues{}, outboxer.DynamicValues{}))
mock.ExpectExec(`update event_store set (.+)where id = ?`).
WithArgs(1).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectBegin()
mock.ExpectExec(`DELETE FROM (.+) WHERE ctid IN (.+)`).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectCommit()
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)
}
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))
}
for _, m := range msgs {
err := ds.SetAsDispatched(ctx, m.ID)
if err != nil {
t.Fatalf("failed to set message as dispatched: %s", err)
}
}
if err := ds.Remove(ctx, time.Now(), 10); err != nil {
t.Fatalf("failed to remove messages: %s", err)
}
}
func TestPostgres_WithInstanceNoDB(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 CURRENT_DATABASE()`).
WillReturnError(errors.New("missing database"))
_, err = WithInstance(ctx, db)
if err == nil {
t.Fatal("setting up the DS instance is supposed to error", err)
}
}
func TestPostgres_WithInstanceWithEmptyDBName(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 CURRENT_DATABASE()`).
WillReturnRows(sqlmock.NewRows([]string{"DATABASE()"}).AddRow(""))
_, err = WithInstance(ctx, db)
if err == nil {
t.Fatal("setting up the DS instance is supposed to error", err)
}
}
func TestPostgres_WithInstanceNoSchema(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 CURRENT_SCHEMA()`).
WillReturnError(errors.New("missing database"))
_, err = WithInstance(ctx, db)
if err == nil {
t.Fatal("setting up the DS instance is supposed to error", err)
}
}
func TestPostgres_WithInstanceWithEmptySchemaName(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 CURRENT_SCHEMA()`).
WillReturnRows(sqlmock.NewRows([]string{"CURRENT_SCHEMA()"}).AddRow(""))
_, err = WithInstance(ctx, db)
if err == nil {
t.Fatal("setting up the DS instance is supposed to error", err)
}
}
func TestPostgres_AddWithinTx(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 LIMIT 1`).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectExec(`INSERT INTO event_store (.+) VALUES (.+)`).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectCommit()
fn := func(tx outboxer.ExecerContext) error {
_, err := tx.ExecContext(ctx, "SELECT * from event_store LIMIT 1")
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 initDatastoreMock(t *testing.T, mock sqlmock.Sqlmock) {
mock.ExpectQuery(`SELECT CURRENT_DATABASE()`).
WillReturnRows(sqlmock.NewRows([]string{"CURRENT_DATABASE()"}).AddRow("test"))
mock.ExpectQuery(`SELECT CURRENT_SCHEMA()`).
WillReturnRows(sqlmock.NewRows([]string{"CURRENT_SCHEMA()"}).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(`SELECT pg_advisory_lock(.+)`).
WithArgs(aid).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectExec(`CREATE TABLE IF NOT EXISTS event_store (.+);`).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectExec(`SELECT pg_advisory_unlock(.+)`).
WithArgs(aid).
WillReturnResult(sqlmock.NewResult(0, 1))
}