diff --git a/cdc/store_test.go b/cdc/store_test.go index bbde0a3..a423fd1 100644 --- a/cdc/store_test.go +++ b/cdc/store_test.go @@ -113,8 +113,10 @@ func TestRead(t *testing.T) { func TestWriteSuccess(t *testing.T) { cdcs := setup(t, "", cdc.NoRetryPolicy) + // Note we don't explicitly set ID here, so that MongoDB will generate an _id for us. + // This is the same behavior as the actual store implementation, which does not set the _id and + // allows MongoDB to generate it. event := etre.CDCEvent{ - Id: "abc", Ts: 54, Op: "i", Caller: "mike", @@ -140,6 +142,19 @@ func TestWriteSuccess(t *testing.T) { actualEvents, err := cdcs.Read(filter) require.NoError(t, err) assert.Len(t, actualEvents, 1) + + // MongoDB auto-generates the _id column since we didn't provide one in `event` above. + // This is the same behavior as the actual store implementation, which does not set the _id and + // allows MongoDB to generate it. + // + // Since we don't know what the generated _id will be, we can't compare the entire event directly. + // Instead, verify that the _id is a valid ObjectID and then clear it so we can compare the rest + // of the event we read (in actualEvents[0]) with the original `event` above. + _, err = bson.ObjectIDFromHex(actualEvents[0].Id) + require.NoError(t, err) + actualEvents[0].Id = "" + + // Compare the rest of the event assert.Equal(t, event, actualEvents[0]) } diff --git a/server/server.go b/server/server.go index 2a0fe00..1737475 100644 --- a/server/server.go +++ b/server/server.go @@ -9,6 +9,7 @@ import ( "time" "go.mongodb.org/mongo-driver/v2/mongo" + "go.mongodb.org/mongo-driver/v2/mongo/options" "github.com/square/etre" "github.com/square/etre/api" @@ -64,7 +65,8 @@ func (s *Server) Boot(configFile string) error { return fmt.Errorf("cannot connect to CDC datasource: %s", err) } s.cdcDbClient = cdcClient - cdcColl := cdcClient.Database(cfg.Datasource.Database).Collection(config.CDC_COLLECTION) + cdcOpts := options.Collection().SetBSONOptions(&options.BSONOptions{ObjectIDAsHexString: true}) // Because etre.CDCEvent has string _id, not bson.ObjectID + cdcColl := cdcClient.Database(cfg.Datasource.Database).Collection(config.CDC_COLLECTION, cdcOpts) // Store wrp := cdc.RetryPolicy{ diff --git a/test/db.go b/test/db.go index f5cc523..b8f4a40 100644 --- a/test/db.go +++ b/test/db.go @@ -24,7 +24,12 @@ func DbCollections(entityTypes []string) (*mongo.Client, map[string]*mongo.Colle } coll := map[string]*mongo.Collection{} for _, t := range entityTypes { - coll[t] = client.Database(database).Collection(t) + if t == "cdc" { + cdcOpts := options.Collection().SetBSONOptions(&options.BSONOptions{ObjectIDAsHexString: true}) // Because etre.CDCEvent has string _id, not bson.ObjectID + coll[t] = client.Database(database).Collection(t, cdcOpts) + } else { + coll[t] = client.Database(database).Collection(t) + } } return client, coll, nil }