@@ -32,6 +32,7 @@ import (
32
32
"go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore"
33
33
"go.mongodb.org/mongo-driver/v2/x/mongo/driver"
34
34
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/wiremessage"
35
+ "go.mongodb.org/mongo-driver/v2/x/mongo/driver/xoptions"
35
36
"golang.org/x/sync/errgroup"
36
37
)
37
38
@@ -818,6 +819,87 @@ func TestClient_BulkWrite(t *testing.T) {
818
819
})
819
820
}
820
821
})
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
+ })
821
903
var bulkWrites int
822
904
cmdMonitor := & event.CommandMonitor {
823
905
Started : func (_ context.Context , evt * event.CommandStartedEvent ) {
@@ -838,8 +920,8 @@ func TestClient_BulkWrite(t *testing.T) {
838
920
}
839
921
840
922
_ , 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 )
843
925
})
844
926
}
845
927
0 commit comments