This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main_test.go
193 lines (167 loc) Β· 4.34 KB
/
main_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
package main
import (
"database/sql"
"os"
"testing"
"github.com/buger/jsonparser"
"github.com/confluentinc/confluent-kafka-go/kafka"
"github.com/blendle/pg2kafka/eventqueue"
_ "github.com/lib/pq"
)
func TestFetchUnprocessedRecords(t *testing.T) {
db, eq, cleanup := setup(t)
defer cleanup()
// TODO: Use actual trigger to generate this?
events := []*eventqueue.Event{
{
ExternalID: []byte("fefc72b4-d8df-4039-9fb9-bfcb18066a2b"),
TableName: "users",
Statement: "UPDATE",
Data: []byte(`{ "email": "[email protected]" }`),
Processed: true,
},
{
ExternalID: []byte("fefc72b4-d8df-4039-9fb9-bfcb18066a2b"),
TableName: "users",
Statement: "UPDATE",
Data: []byte(`{ "email": "[email protected]" }`),
},
{
ExternalID: []byte("fefc72b4-d8df-4039-9fb9-bfcb18066a2b"),
TableName: "users",
Statement: "UPDATE",
Data: []byte(`{ "email": "[email protected]" }`),
},
{
ExternalID: nil,
TableName: "users",
Statement: "CREATE",
Data: []byte(`{ "email": "[email protected]" }`),
},
{
ExternalID: nil,
TableName: "users",
Statement: "UPDATE",
Data: []byte(`{ "email": "[email protected]" }`),
},
}
if err := insert(db, events); err != nil {
t.Fatalf("Error inserting events: %v", err)
}
p := &mockProducer{
messages: make([]*kafka.Message, 0),
}
ProcessEvents(p, eq)
expected := 4
actual := len(p.messages)
if actual != expected {
t.Fatalf("Unexpected number of messages produced. Expected %d, got %d", expected, actual)
}
msg := p.messages[0]
email, err := jsonparser.GetString(msg.Value, "data", "email")
if err != nil {
t.Fatal(err)
}
if email != "[email protected]" {
t.Errorf("Data did not match. Expected %v, got %v", "[email protected]", email)
}
externalID, err := jsonparser.GetString(msg.Value, "external_id")
if err != nil {
t.Fatal(err)
}
if externalID != "fefc72b4-d8df-4039-9fb9-bfcb18066a2b" {
t.Errorf("Expected %v, got %v", "fefc72b4-d8df-4039-9fb9-bfcb18066a2b", externalID)
}
msg = p.messages[3]
email, err = jsonparser.GetString(msg.Value, "data", "email")
if err != nil {
t.Fatal(err)
}
if email != "[email protected]" {
t.Errorf("Data did not match. Expected %v, got %v", "[email protected]", email)
}
if len(msg.Key) != 0 {
t.Errorf("Expected empty key, got %v", msg.Key)
}
}
// Helpers
func setup(t *testing.T) (*sql.DB, *eventqueue.Queue, func()) {
t.Helper()
topicNamespace = "users"
db, err := sql.Open("postgres", os.Getenv("DATABASE_URL"))
if err != nil {
t.Fatalf("failed to open database: %v", err)
}
eq := eventqueue.NewWithDB(db)
if err := eq.ConfigureOutboundEventQueueAndTriggers("./sql"); err != nil {
t.Fatal(err)
}
return db, eq, func() {
_, err := db.Exec("DELETE FROM pg2kafka.outbound_event_queue")
if err != nil {
t.Fatalf("failed to clear table: %v", err)
}
if err := db.Close(); err != nil {
t.Fatalf("Error closing db: %v", err)
}
}
}
func insert(db *sql.DB, events []*eventqueue.Event) error {
tx, err := db.Begin()
if err != nil {
return err
}
statement, err := tx.Prepare(`
INSERT INTO pg2kafka.outbound_event_queue (external_id, table_name, statement, data, processed)
VALUES ($1, $2, $3, $4, $5)
`)
if err != nil {
if txerr := tx.Rollback(); txerr != nil {
return txerr
}
return err
}
for _, e := range events {
_, serr := statement.Exec(e.ExternalID, e.TableName, e.Statement, e.Data, e.Processed)
if serr != nil {
if txerr := tx.Rollback(); err != nil {
return txerr
}
return serr
}
}
return tx.Commit()
}
var parseTopicNamespacetests = []struct {
in1, in2, out string
}{
{"", "", ""},
{"", "world", "world"},
{"hello", "", "hello."},
{"hello", "world", "hello.world"},
}
func TestParseTopicNamespace(t *testing.T) {
for _, tt := range parseTopicNamespacetests {
t.Run(tt.out, func(t *testing.T) {
actual := parseTopicNamespace(tt.in1, tt.in2)
if actual != tt.out {
t.Errorf("parseTopicNamespace(%q, %q) => %v, want: %v", tt.in1, tt.in2, actual, tt.out)
}
})
}
}
type mockProducer struct {
messages []*kafka.Message
}
func (p *mockProducer) Close() {
}
func (p *mockProducer) Flush(timeout int) int {
return 0
}
func (p *mockProducer) Produce(msg *kafka.Message, deliveryChan chan kafka.Event) error {
p.messages = append(p.messages, msg)
go func() {
deliveryChan <- msg
}()
return nil
}