Skip to content

Commit 87b8245

Browse files
committed
tests
1 parent a1debd7 commit 87b8245

2 files changed

Lines changed: 78 additions & 16 deletions

File tree

flow/connectors/mongo/qrep.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,13 @@ func hasObjectIDAsKey(ctx context.Context, collection *mongo.Collection) bool {
110110
if !cursor.Next(ctx) {
111111
return false
112112
}
113-
var oid bson.ObjectID
114-
if err := bson.Unmarshal(cursor.Current, &oid); err != nil {
113+
var doc struct {
114+
ID bson.ObjectID `bson:"_id"`
115+
}
116+
if err := bson.Unmarshal(cursor.Current, &doc); err != nil {
115117
return false
116118
}
117-
return !oid.IsZero()
119+
return !doc.ID.IsZero()
118120
}
119121

120122
func (c *MongoConnector) PullQRepRecords(

flow/e2e/mongo_test.go

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
connclickhouse "github.com/PeerDB-io/peerdb/flow/connectors/clickhouse"
2020
"github.com/PeerDB-io/peerdb/flow/e2eshared"
2121
"github.com/PeerDB-io/peerdb/flow/generated/protos"
22+
"github.com/PeerDB-io/peerdb/flow/internal"
2223
"github.com/PeerDB-io/peerdb/flow/model"
2324
"github.com/PeerDB-io/peerdb/flow/shared"
2425
)
@@ -107,40 +108,99 @@ func (s MongoClickhouseSuite) Test_Simple_Flow_Partitioned() {
107108
}
108109
flowConnConfig := s.generateFlowConnectionConfigsDefaultEnv(connectionGen)
109110
flowConnConfig.DoInitialSnapshot = true
110-
flowConnConfig.TableMappings[0].PartitionKey = "_id"
111111
flowConnConfig.SnapshotNumRowsPerPartition = 10
112112

113113
adminClient := s.Source().(*MongoSource).AdminClient()
114114
collection := adminClient.Database(srcDatabase).Collection(srcTable)
115-
// insert 1000 rows into the source table for initial load
115+
116+
baseTime := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
117+
docs := make([]any, 1000)
116118
for i := range 1000 {
117-
testKey := fmt.Sprintf("init_key_%d", i)
118-
testValue := fmt.Sprintf("init_value_%d", i)
119-
res, err := collection.InsertOne(t.Context(), bson.D{bson.E{Key: testKey, Value: testValue}}, options.InsertOne())
120-
require.NoError(t, err)
121-
require.True(t, res.Acknowledged)
119+
oid := bson.NewObjectIDFromTimestamp(baseTime.Add(time.Duration(i) * time.Second))
120+
docs[i] = bson.D{
121+
{Key: "_id", Value: oid},
122+
{Key: fmt.Sprintf("init_key_%d", i), Value: fmt.Sprintf("init_value_%d", i)},
123+
}
122124
}
125+
_, err := collection.InsertMany(t.Context(), docs)
126+
require.NoError(t, err)
123127

124128
tc := NewTemporalClient(t)
125129
env := ExecutePeerflow(t, tc, flowConnConfig)
126130

127131
EnvWaitForEqualTablesWithNames(env, s, "initial load to match", srcTable, dstTable, "_id,doc")
128132

133+
catalogPool, err := internal.GetCatalogConnectionPoolFromEnv(t.Context())
134+
require.NoError(t, err)
135+
var partitionCount int
136+
require.NoError(t, catalogPool.QueryRow(t.Context(),
137+
`SELECT COUNT(*) FROM peerdb_stats.qrep_partitions WHERE parent_mirror_name = $1`,
138+
flowConnConfig.FlowJobName).Scan(&partitionCount))
139+
require.Equal(t, 100, partitionCount, "expected 100 partitions for 1000 rows with 10 rows per partition")
140+
129141
SetupCDCFlowStatusQuery(t, env, flowConnConfig)
130-
// insert 10 rows into the source table for cdc
142+
cdcBaseTime := time.Date(2026, 2, 1, 0, 0, 0, 0, time.UTC)
143+
cdcDocs := make([]any, 10)
131144
for i := range 10 {
132-
testKey := fmt.Sprintf("test_key_%d", i)
133-
testValue := fmt.Sprintf("test_value_%d", i)
134-
res, err := collection.InsertOne(t.Context(), bson.D{bson.E{Key: testKey, Value: testValue}}, options.InsertOne())
135-
require.NoError(t, err)
136-
require.True(t, res.Acknowledged)
145+
oid := bson.NewObjectIDFromTimestamp(cdcBaseTime.Add(time.Duration(i) * time.Second))
146+
cdcDocs[i] = bson.D{
147+
{Key: "_id", Value: oid},
148+
{Key: fmt.Sprintf("cdc_key_%d", i), Value: fmt.Sprintf("cdc_value_%d", i)},
149+
}
137150
}
151+
_, err = collection.InsertMany(t.Context(), cdcDocs)
152+
require.NoError(t, err)
138153

139154
EnvWaitForEqualTablesWithNames(env, s, "cdc events to match", srcTable, dstTable, "_id,doc")
140155
env.Cancel(t.Context())
141156
RequireEnvCanceled(t, env)
142157
}
143158

159+
func (s MongoClickhouseSuite) Test_Snapshot_Non_ObjectID_Falls_Back_To_Single_Partition() {
160+
t := s.T()
161+
srcDatabase := GetTestDatabase(s.Suffix())
162+
srcTable := "test_non_objectid_snapshot"
163+
dstTable := "test_non_objectid_snapshot_dst"
164+
165+
connectionGen := FlowConnectionGenerationConfig{
166+
FlowJobName: AddSuffix(s, srcTable),
167+
TableMappings: TableMappings(s, srcTable, dstTable),
168+
Destination: s.Peer().Name,
169+
}
170+
flowConnConfig := s.generateFlowConnectionConfigsDefaultEnv(connectionGen)
171+
flowConnConfig.DoInitialSnapshot = true
172+
flowConnConfig.SnapshotNumRowsPerPartition = 5
173+
174+
adminClient := s.Source().(*MongoSource).AdminClient()
175+
collection := adminClient.Database(srcDatabase).Collection(srcTable)
176+
177+
docs := make([]any, 10)
178+
for i := range 10 {
179+
docs[i] = bson.D{
180+
{Key: "_id", Value: fmt.Sprintf("string_id_%d", i)},
181+
{Key: "value", Value: fmt.Sprintf("value_%d", i)},
182+
}
183+
}
184+
_, err := collection.InsertMany(t.Context(), docs)
185+
require.NoError(t, err)
186+
187+
tc := NewTemporalClient(t)
188+
env := ExecutePeerflow(t, tc, flowConnConfig)
189+
190+
EnvWaitForEqualTablesWithNames(env, s, "initial load", srcTable, dstTable, "_id,doc")
191+
192+
catalogPool, err := internal.GetCatalogConnectionPoolFromEnv(t.Context())
193+
require.NoError(t, err)
194+
var partitionCount int
195+
require.NoError(t, catalogPool.QueryRow(t.Context(),
196+
`SELECT COUNT(*) FROM peerdb_stats.qrep_partitions WHERE parent_mirror_name = $1`,
197+
flowConnConfig.FlowJobName).Scan(&partitionCount))
198+
require.Equal(t, 1, partitionCount)
199+
200+
env.Cancel(t.Context())
201+
RequireEnvCanceled(t, env)
202+
}
203+
144204
func (s MongoClickhouseSuite) Test_Inconsistent_Schema() {
145205
t := s.T()
146206

0 commit comments

Comments
 (0)