-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsource_integration_test.go
More file actions
399 lines (305 loc) · 10.7 KB
/
source_integration_test.go
File metadata and controls
399 lines (305 loc) · 10.7 KB
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
// Copyright © 2024 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mysql
import (
"context"
"encoding/json"
"errors"
"fmt"
"slices"
"testing"
"time"
testutils "github.com/conduitio-labs/conduit-connector-mysql/test"
"github.com/conduitio/conduit-commons/opencdc"
sdk "github.com/conduitio/conduit-connector-sdk"
"github.com/conduitio/conduit-connector-sdk/schema"
"github.com/matryer/is"
)
func testSource(ctx context.Context, is *is.I, cfg map[string]string) (sdk.Source, func()) {
source := &Source{}
cfg["dsn"] = testutils.DSN
cfg["cdc.disableLogs"] = "true"
err := sdk.Util.ParseConfig(ctx,
cfg, source.Config(),
Connector.NewSpecification().SourceParams,
)
is.NoErr(err)
is.NoErr(source.Open(ctx, nil))
return source, func() { is.NoErr(source.Teardown(ctx)) }
}
func testSourceFromUsers(ctx context.Context, is *is.I) (sdk.Source, func()) {
return testSource(ctx, is, map[string]string{
"tables": "users",
})
}
func TestSource_ConsistentSnapshot(t *testing.T) {
ctx := testutils.TestContext(t)
is := is.New(t)
db := testutils.NewDB(t)
testutils.CreateUserTable(is, db)
// insert 4 rows, the whole snapshot
user1 := testutils.InsertUser(is, db, 1)
user2 := testutils.InsertUser(is, db, 2)
user3 := testutils.InsertUser(is, db, 3)
user4 := testutils.InsertUser(is, db, 4)
// start source connector
source, teardown := testSourceFromUsers(ctx, is)
defer teardown()
// read 2 records -> they shall be snapshots
testutils.ReadAndAssertSnapshot(ctx, is, source, user1)
testutils.ReadAndAssertSnapshot(ctx, is, source, user2)
// insert 2 rows, delete the 4th inserted row
user5 := testutils.InsertUser(is, db, 5)
user6 := testutils.InsertUser(is, db, 6)
testutils.DeleteUser(is, db, user4)
// read 2 more records -> they shall be snapshots
// snapshot completed, so previous 2 inserts and delete done while
// snapshotting should be captured
testutils.ReadAndAssertSnapshot(ctx, is, source, user3)
testutils.ReadAndAssertSnapshot(ctx, is, source, user4)
testutils.ReadAndAssertCreate(ctx, is, source, user5)
testutils.ReadAndAssertCreate(ctx, is, source, user6)
testutils.ReadAndAssertDelete(ctx, is, source, user4)
}
func TestSource_NonZeroSnapshotStart(t *testing.T) {
ctx := testutils.TestContext(t)
is := is.New(t)
db := testutils.NewDB(t)
testutils.CreateUserTable(is, db)
// Insert 80 users starting from the 20th so that the starting row's primary key
// is greater than 0. This ensures a more realistic dataset where
// the first rows don't start at 0.
var inserted []testutils.User
for i := 20; i < 100; i++ {
user := testutils.InsertUser(is, db, i)
inserted = append(inserted, user)
}
source, teardown := testSource(ctx, is, map[string]string{
"tables": "users",
"snapshot.fetchSize": "10",
})
defer teardown()
for _, user := range inserted {
testutils.ReadAndAssertSnapshot(ctx, is, source, user)
}
}
func TestSource_EmptyChunkRead(t *testing.T) {
ctx := testutils.TestContext(t)
is := is.New(t)
db := testutils.NewDB(t)
testutils.CreateUserTable(is, db)
var expected []testutils.User
for i := range 100 {
userID := i + 1
if userID > 20 && userID < 40 {
continue
} else if userID > 60 && userID < 80 {
continue
}
user := testutils.InsertUser(is, db, userID)
expected = append(expected, user)
}
source, teardown := testSource(ctx, is, map[string]string{
"tables": "users",
"snapshot.fetchSize": "10",
})
defer teardown()
for _, user := range expected {
testutils.ReadAndAssertSnapshot(ctx, is, source, user)
}
}
func TestSource_UnsafeSnapshot(t *testing.T) {
is := is.New(t)
db := testutils.NewDB(t)
type TableWithoutPK struct {
// No id field, forcing gorm to not create a primary key
Data string `gorm:"size:100"`
}
testutils.CreateTables(is, db, &TableWithoutPK{})
db.Create([]TableWithoutPK{
{Data: "record A"},
{Data: "record B"},
})
is.NoErr(db.Error)
expectedData := []string{"record A", "record B"}
ctx := testutils.TestContext(t)
source, teardown := testSource(ctx, is, map[string]string{
"tables": "table_without_pk",
"snapshot.unsafe": "true",
})
defer teardown()
var recs []opencdc.Record
for range expectedData {
rec, err := source.ReadN(ctx, 1)
is.NoErr(err)
is.True(len(rec) == 1)
is.NoErr(source.Ack(ctx, rec[0].Position))
recs = append(recs, rec[0])
}
for i, expectedData := range expectedData {
actual := recs[i]
is.Equal(actual.Operation, opencdc.OperationSnapshot)
is.Equal(actual.Payload.After.(opencdc.StructuredData)["data"].(string), expectedData)
}
}
func TestSource_CompositeKey(t *testing.T) {
is := is.New(t)
ctx := testutils.TestContext(t)
db := testutils.NewDB(t)
type CompositeKeyTable struct {
UserID int `gorm:"primaryKey;autoIncrement:false"`
TenantID string `gorm:"primaryKey;size:50"`
Email string `gorm:"size:100;uniqueIndex"`
FirstName string `gorm:"size:50"`
LastName string `gorm:"size:50"`
}
tableName := testutils.TableName(is, db, &CompositeKeyTable{})
testutils.CreateTables(is, db, &CompositeKeyTable{})
testData := []CompositeKeyTable{
{UserID: 1, TenantID: "tenant1", Email: "user1@example.com", FirstName: "John", LastName: "Doe"},
{UserID: 2, TenantID: "tenant1", Email: "user2@example.com", FirstName: "Jane", LastName: "Smith"},
{UserID: 1, TenantID: "tenant2", Email: "user3@example.com", FirstName: "Alice", LastName: "Johnson"},
{UserID: 2, TenantID: "tenant2", Email: "user4@example.com", FirstName: "Bob", LastName: "Brown"},
}
is.NoErr(db.Create(&testData).Error)
source, teardown := testSource(ctx, is, map[string]string{
"tables": tableName,
})
defer teardown()
var snapshotRecords []opencdc.Record
for range testData {
recs, err := source.ReadN(ctx, 1)
is.NoErr(err)
is.True(len(recs) == 1)
rec := recs[0]
is.NoErr(source.Ack(ctx, rec.Position))
is.Equal(rec.Operation, opencdc.OperationSnapshot)
collection, err := rec.Metadata.GetCollection()
is.NoErr(err)
is.Equal(collection, tableName)
payloadSchemaSubject, err := rec.Metadata.GetPayloadSchemaSubject()
is.NoErr(err)
payloadSchemaVersion, err := rec.Metadata.GetPayloadSchemaVersion()
is.NoErr(err)
keySchemaSubject, err := rec.Metadata.GetKeySchemaSubject()
is.NoErr(err)
keySchemaVersion, err := rec.Metadata.GetKeySchemaVersion()
is.NoErr(err)
payloadSchema, err := schema.Get(ctx, payloadSchemaSubject, payloadSchemaVersion)
is.NoErr(err)
keySchema, err := schema.Get(ctx, keySchemaSubject, keySchemaVersion)
is.NoErr(err)
var parsedPayloadSchema testutils.AvroSchema
is.NoErr(json.Unmarshal(payloadSchema.Bytes, &parsedPayloadSchema))
var parsedKeySchema testutils.AvroSchema
is.NoErr(json.Unmarshal(keySchema.Bytes, &parsedKeySchema))
is.Equal(parsedPayloadSchema.Type, "record")
is.Equal(len(parsedPayloadSchema.Fields), 5)
is.Equal(parsedKeySchema.Type, "record")
is.Equal(len(parsedKeySchema.Fields), 2)
var keyFieldNames []string
for _, field := range parsedKeySchema.Fields {
keyFieldNames = append(keyFieldNames, field.Name)
}
is.True(slices.Contains(keyFieldNames, "user_id"))
is.True(slices.Contains(keyFieldNames, "tenant_id"))
snapshotRecords = append(snapshotRecords, rec)
}
is.Equal(len(snapshotRecords), len(testData))
for _, rec := range snapshotRecords {
key, ok := rec.Key.(opencdc.StructuredData)
is.True(ok)
_, hasUserID := key["user_id"]
_, hasTenantID := key["tenant_id"]
is.True(hasUserID)
is.True(hasTenantID)
}
updatedRecord := testData[0]
updatedRecord.FirstName = "Johnny"
updatedRecord.LastName = "Doeson"
is.NoErr(db.Save(&updatedRecord).Error)
newRecord := CompositeKeyTable{
UserID: 3, TenantID: "tenant1", Email: "user5@example.com",
FirstName: "Sarah", LastName: "Wilson",
}
is.NoErr(db.Create(&newRecord).Error)
is.NoErr(db.Delete(&testData[3]).Error)
recs, err := source.ReadN(ctx, 1)
is.NoErr(err)
is.True(len(recs) == 1)
updateRec := recs[0]
is.NoErr(source.Ack(ctx, updateRec.Position))
is.Equal(updateRec.Operation, opencdc.OperationUpdate)
is.True(updateRec.Payload.Before != nil)
is.True(updateRec.Payload.After != nil)
createRecs, err := source.ReadN(ctx, 1)
is.NoErr(err)
is.True(len(createRecs) == 1)
createRec := createRecs[0]
is.NoErr(source.Ack(ctx, createRec.Position))
is.Equal(createRec.Operation, opencdc.OperationCreate)
deleteRecs, err := source.ReadN(ctx, 1)
is.NoErr(err)
is.True(len(deleteRecs) == 1)
deleteRec := deleteRecs[0]
is.NoErr(source.Ack(ctx, deleteRec.Position))
is.Equal(deleteRec.Operation, opencdc.OperationDelete)
is.True(deleteRec.Payload.Before != nil)
is.True(deleteRec.Payload.After == nil)
for _, rec := range []opencdc.Record{updateRec, createRec, deleteRec} {
key, ok := rec.Key.(opencdc.StructuredData)
is.True(ok)
_, hasUserID := key["user_id"]
_, hasTenantID := key["tenant_id"]
is.True(hasUserID)
is.True(hasTenantID)
keySchemaSubject, err := rec.Metadata.GetKeySchemaSubject()
is.NoErr(err)
keySchemaVersion, err := rec.Metadata.GetKeySchemaVersion()
is.NoErr(err)
keySchema, err := schema.Get(ctx, keySchemaSubject, keySchemaVersion)
is.NoErr(err)
var parsedKeySchema testutils.AvroSchema
is.NoErr(json.Unmarshal(keySchema.Bytes, &parsedKeySchema))
is.Equal(parsedKeySchema.Type, "record")
is.Equal(len(parsedKeySchema.Fields), 2) // Both parts of the composite key
}
}
func TestSnapshotEnabled(t *testing.T) {
ctx := testutils.TestContext(t)
is := is.New(t)
db := testutils.NewDB(t)
testutils.CreateUserTable(is, db)
user1 := testutils.InsertUser(is, db, 1)
user2 := testutils.InsertUser(is, db, 2)
source, teardown := testSource(ctx, is, map[string]string{
"tables": "users",
"snapshot.enabled": "false",
})
defer teardown()
user3 := testutils.InsertUser(is, db, 3)
user1Before := user1
user1Updated := user1.Update()
testutils.UpdateUser(is, db, user1Updated)
testutils.DeleteUser(is, db, user2)
// We should only get CDC events (no snapshot records)
testutils.ReadAndAssertCreate(ctx, is, source, user3)
testutils.ReadAndAssertUpdate(ctx, is, source, user1Before, user1Updated)
testutils.ReadAndAssertDelete(ctx, is, source, user2)
ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
defer cancel()
_, err := source.ReadN(ctx, 1)
fmt.Println(err)
is.True(errors.Is(err, context.DeadlineExceeded))
}