Skip to content

Commit 446c2e1

Browse files
committed
GODRIVER-3370 Add bypassEmptyTsReplacement option.
1 parent 957d13a commit 446c2e1

File tree

10 files changed

+656
-3
lines changed

10 files changed

+656
-3
lines changed

internal/integration/client_test.go

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore"
3333
"go.mongodb.org/mongo-driver/v2/x/mongo/driver"
3434
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/wiremessage"
35+
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/xoptions"
3536
"golang.org/x/sync/errgroup"
3637
)
3738

@@ -818,6 +819,87 @@ func TestClient_BulkWrite(t *testing.T) {
818819
})
819820
}
820821
})
822+
mt.RunOpts("bulk write with bypassEmptyTsReplacement", mtBulkWriteOpts, func(mt *mtest.T) {
823+
mt.Parallel()
824+
825+
newOpts := func(option bson.D) *options.ClientBulkWriteOptionsBuilder {
826+
opts := options.ClientBulkWrite()
827+
err := xoptions.SetInternalClientBulkWriteOptions(opts, "addCommandFields", option)
828+
require.NoError(mt, err, "unexpected error: %v", err)
829+
return opts
830+
}
831+
832+
marshalValue := func(val interface{}) bson.RawValue {
833+
t.Helper()
834+
835+
valType, data, err := bson.MarshalValue(val)
836+
require.Nil(t, err, "MarshalValue error: %v", err)
837+
return bson.RawValue{
838+
Type: valType,
839+
Value: data,
840+
}
841+
}
842+
843+
models := []struct {
844+
name string
845+
model mongo.ClientWriteModel
846+
}{
847+
{
848+
"insert one",
849+
mongo.NewClientInsertOneModel().SetDocument(bson.D{{"x", 1}}),
850+
},
851+
{
852+
"update one",
853+
mongo.NewClientUpdateOneModel().SetFilter(bson.D{{"x", 1}}).SetUpdate(bson.D{{"$set", bson.D{{"x", 3.14159}}}}),
854+
},
855+
{
856+
"update many",
857+
mongo.NewClientUpdateManyModel().SetFilter(bson.D{{"x", 1}}).SetUpdate(bson.D{{"$set", bson.D{{"x", 3.14159}}}}),
858+
},
859+
{
860+
"replace one",
861+
mongo.NewClientReplaceOneModel().SetFilter(bson.D{{"x", 1}}).SetReplacement(bson.D{{"x", 3.14159}}),
862+
},
863+
}
864+
865+
testCases := []struct {
866+
name string
867+
opts *options.ClientBulkWriteOptionsBuilder
868+
expected bson.RawValue
869+
}{
870+
{
871+
"empty",
872+
options.ClientBulkWrite(),
873+
bson.RawValue{},
874+
},
875+
{
876+
"false",
877+
newOpts(bson.D{{"bypassEmptyTsReplacement", false}}),
878+
marshalValue(false),
879+
},
880+
{
881+
"true",
882+
newOpts(bson.D{{"bypassEmptyTsReplacement", true}}),
883+
marshalValue(true),
884+
},
885+
}
886+
for _, m := range models {
887+
for _, tc := range testCases {
888+
mt.Run(fmt.Sprintf("%s %s", m.name, tc.name), func(mt *mtest.T) {
889+
writes := []mongo.ClientBulkWrite{{
890+
Database: "foo",
891+
Collection: "bar",
892+
Model: m.model,
893+
}}
894+
_, err := mt.Client.BulkWrite(context.Background(), writes, tc.opts)
895+
require.NoError(mt, err, "BulkWrite error: %v", err)
896+
evt := mt.GetStartedEvent()
897+
val := evt.Command.Lookup("bypassEmptyTsReplacement")
898+
assert.Equal(mt, tc.expected, val, "expected bypassEmptyTsReplacement to be %s", tc.expected.String())
899+
})
900+
}
901+
}
902+
})
821903
var bulkWrites int
822904
cmdMonitor := &event.CommandMonitor{
823905
Started: func(_ context.Context, evt *event.CommandStartedEvent) {
@@ -838,8 +920,8 @@ func TestClient_BulkWrite(t *testing.T) {
838920
}
839921

840922
_, err := mt.Client.BulkWrite(context.Background(), writes)
841-
require.NoError(t, err)
842-
assert.Equal(t, 2, bulkWrites, "expected %d bulkWrites, got %d", 2, bulkWrites)
923+
require.NoError(mt, err)
924+
assert.Equal(mt, 2, bulkWrites, "expected %d bulkWrites, got %d", 2, bulkWrites)
843925
})
844926
}
845927

0 commit comments

Comments
 (0)