Skip to content

Commit 843e33f

Browse files
committed
updates
1 parent 446c2e1 commit 843e33f

File tree

7 files changed

+154
-198
lines changed

7 files changed

+154
-198
lines changed

internal/integration/client_test.go

Lines changed: 85 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -819,87 +819,6 @@ func TestClient_BulkWrite(t *testing.T) {
819819
})
820820
}
821821
})
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-
})
903822
var bulkWrites int
904823
cmdMonitor := &event.CommandMonitor{
905824
Started: func(_ context.Context, evt *event.CommandStartedEvent) {
@@ -925,6 +844,91 @@ func TestClient_BulkWrite(t *testing.T) {
925844
})
926845
}
927846

847+
func TestClient_BulkWrite_AddCommandFields(t *testing.T) {
848+
newOpts := func(option bson.D) *options.ClientBulkWriteOptionsBuilder {
849+
opts := options.ClientBulkWrite()
850+
err := xoptions.SetInternalClientBulkWriteOptions(opts, "addCommandFields", option)
851+
require.NoError(t, err, "unexpected error: %v", err)
852+
return opts
853+
}
854+
855+
marshalValue := func(val interface{}) bson.RawValue {
856+
t.Helper()
857+
858+
valType, data, err := bson.MarshalValue(val)
859+
require.NoError(t, err, "MarshalValue error: %v", err)
860+
return bson.RawValue{
861+
Type: valType,
862+
Value: data,
863+
}
864+
}
865+
866+
models := []struct {
867+
name string
868+
model mongo.ClientWriteModel
869+
}{
870+
{
871+
name: "insert one",
872+
model: mongo.NewClientInsertOneModel().SetDocument(bson.D{{"x", 1}}),
873+
},
874+
{
875+
name: "update one",
876+
model: mongo.NewClientUpdateOneModel().SetFilter(bson.D{{"x", 1}}).SetUpdate(bson.D{{"$set", bson.D{{"x", 3.14159}}}}),
877+
},
878+
{
879+
name: "update many",
880+
model: mongo.NewClientUpdateManyModel().SetFilter(bson.D{{"x", 1}}).SetUpdate(bson.D{{"$set", bson.D{{"x", 3.14159}}}}),
881+
},
882+
{
883+
name: "replace one",
884+
model: mongo.NewClientReplaceOneModel().SetFilter(bson.D{{"x", 1}}).SetReplacement(bson.D{{"x", 3.14159}}),
885+
},
886+
}
887+
888+
testCases := []struct {
889+
name string
890+
opts *options.ClientBulkWriteOptionsBuilder
891+
expected bson.RawValue
892+
}{
893+
{
894+
name: "empty",
895+
opts: options.ClientBulkWrite(),
896+
expected: bson.RawValue{},
897+
},
898+
{
899+
name: "false",
900+
opts: newOpts(bson.D{{"bypassEmptyTsReplacement", false}}),
901+
expected: marshalValue(false),
902+
},
903+
{
904+
name: "true",
905+
opts: newOpts(bson.D{{"bypassEmptyTsReplacement", true}}),
906+
expected: marshalValue(true),
907+
},
908+
}
909+
910+
mtBulkWriteOpts := mtest.NewOptions().MinServerVersion("8.0").ClientType(mtest.Pinned)
911+
mt := mtest.New(t, noClientOpts)
912+
for _, m := range models {
913+
for _, tc := range testCases {
914+
mt.RunOpts(fmt.Sprintf("%s %s", m.name, tc.name), mtBulkWriteOpts, func(mt *mtest.T) {
915+
mt.Parallel()
916+
917+
writes := []mongo.ClientBulkWrite{{
918+
Database: "foo",
919+
Collection: "bar",
920+
Model: m.model,
921+
}}
922+
_, err := mt.Client.BulkWrite(context.Background(), writes, tc.opts)
923+
require.NoError(mt, err, "BulkWrite error: %v", err)
924+
evt := mt.GetStartedEvent()
925+
val := evt.Command.Lookup("bypassEmptyTsReplacement")
926+
assert.Equal(mt, tc.expected, val, "expected bypassEmptyTsReplacement to be %s", tc.expected.String())
927+
})
928+
}
929+
}
930+
}
931+
928932
func TestClient_BSONOptions(t *testing.T) {
929933
t.Parallel()
930934

internal/integration/collection_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2358,20 +2358,20 @@ func TestBypassEmptyTsReplacement(t *testing.T) {
23582358
model mongo.WriteModel
23592359
}{
23602360
{
2361-
"insert one",
2362-
mongo.NewInsertOneModel().SetDocument(bson.D{{"_id", "id1"}}),
2361+
name: "insert one",
2362+
model: mongo.NewInsertOneModel().SetDocument(bson.D{{"_id", "id1"}}),
23632363
},
23642364
{
2365-
"update one",
2366-
mongo.NewUpdateOneModel().SetFilter(bson.D{{"_id", "id3"}}).SetUpdate(bson.D{{"$set", bson.D{{"_id", 3.14159}}}}),
2365+
name: "update one",
2366+
model: mongo.NewUpdateOneModel().SetFilter(bson.D{{"_id", "id3"}}).SetUpdate(bson.D{{"$set", bson.D{{"_id", 3.14159}}}}),
23672367
},
23682368
{
2369-
"update many",
2370-
mongo.NewUpdateManyModel().SetFilter(bson.D{{"_id", "id3"}}).SetUpdate(bson.D{{"$set", bson.D{{"_id", 3.14159}}}}),
2369+
name: "update many",
2370+
model: mongo.NewUpdateManyModel().SetFilter(bson.D{{"_id", "id3"}}).SetUpdate(bson.D{{"$set", bson.D{{"_id", 3.14159}}}}),
23712371
},
23722372
{
2373-
"replace one",
2374-
mongo.NewReplaceOneModel().SetFilter(bson.D{{"_id", "id3"}}).SetReplacement(bson.D{{"_id", 3.14159}}),
2373+
name: "replace one",
2374+
model: mongo.NewReplaceOneModel().SetFilter(bson.D{{"_id", "id3"}}).SetReplacement(bson.D{{"_id", 3.14159}}),
23752375
},
23762376
}
23772377

@@ -2381,19 +2381,19 @@ func TestBypassEmptyTsReplacement(t *testing.T) {
23812381
expected bson.RawValue
23822382
}{
23832383
{
2384-
"empty",
2385-
options.BulkWrite(),
2386-
bson.RawValue{},
2384+
name: "empty",
2385+
opts: options.BulkWrite(),
2386+
expected: bson.RawValue{},
23872387
},
23882388
{
2389-
"false",
2390-
newOpts(bson.D{{"bypassEmptyTsReplacement", false}}),
2391-
marshalValue(false),
2389+
name: "false",
2390+
opts: newOpts(bson.D{{"bypassEmptyTsReplacement", false}}),
2391+
expected: marshalValue(false),
23922392
},
23932393
{
2394-
"true",
2395-
newOpts(bson.D{{"bypassEmptyTsReplacement", true}}),
2396-
marshalValue(true),
2394+
name: "true",
2395+
opts: newOpts(bson.D{{"bypassEmptyTsReplacement", true}}),
2396+
expected: marshalValue(true),
23972397
},
23982398
}
23992399
for _, m := range models {

mongo/client.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -950,15 +950,11 @@ func (c *Client) BulkWrite(ctx context.Context, writes []ClientBulkWrite,
950950
selector: selector,
951951
writeConcern: wc,
952952
}
953-
if rawDataOpt := optionsutil.Value(bwo.Internal, "rawData"); rawDataOpt != nil {
954-
if rawData, ok := rawDataOpt.(bool); ok {
955-
op.rawData = &rawData
956-
}
953+
if rawData, ok := optionsutil.Value(bwo.Internal, "rawData").(bool); ok {
954+
op.rawData = &rawData
957955
}
958-
if additionalCmd := optionsutil.Value(bwo.Internal, "addCommandFields"); additionalCmd != nil {
959-
if ac, ok := additionalCmd.(bson.D); ok {
960-
op.additionalCmd = ac
961-
}
956+
if additionalCmd, ok := optionsutil.Value(bwo.Internal, "addCommandFields").(bson.D); ok {
957+
op.additionalCmd = additionalCmd
962958
}
963959
if bwo.VerboseResults == nil || !(*bwo.VerboseResults) {
964960
op.errorsOnly = true

0 commit comments

Comments
 (0)