Skip to content

Commit 2fbb028

Browse files
committed
GODRIVER-1793 Use merge helper and test nil opts for InsertOne (#538)
1 parent 3069e04 commit 2fbb028

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

mongo/collection.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -326,15 +326,13 @@ func (coll *Collection) insert(ctx context.Context, documents []interface{},
326326
func (coll *Collection) InsertOne(ctx context.Context, document interface{},
327327
opts ...*options.InsertOneOptions) (*InsertOneResult, error) {
328328

329-
imOpts := make([]*options.InsertManyOptions, len(opts))
330-
for i, opt := range opts {
331-
imo := options.InsertMany()
332-
if opt != nil && opt.BypassDocumentValidation != nil && *opt.BypassDocumentValidation {
333-
imo = imo.SetBypassDocumentValidation(*opt.BypassDocumentValidation)
334-
}
335-
imOpts[i] = imo
329+
ioOpts := options.MergeInsertOneOptions(opts...)
330+
imOpts := options.InsertMany()
331+
332+
if ioOpts.BypassDocumentValidation != nil && *ioOpts.BypassDocumentValidation {
333+
imOpts.SetBypassDocumentValidation(*ioOpts.BypassDocumentValidation)
336334
}
337-
res, err := coll.insert(ctx, []interface{}{document}, imOpts...)
335+
res, err := coll.insert(ctx, []interface{}{document}, imOpts)
338336

339337
rr, err := processWriteError(err)
340338
if rr&rrOne == 0 {

mongo/integration/collection_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,50 @@ func TestCollection(t *testing.T) {
7474
assert.True(mt, ok, "expected error type %v, got %v", mongo.WriteException{}, err)
7575
assert.NotNil(mt, we.WriteConcernError, "expected write concern error, got %+v", we)
7676
})
77+
78+
// Require 3.2 servers for bypassDocumentValidation support.
79+
convertedOptsOpts := mtest.NewOptions().MinServerVersion("3.2")
80+
mt.RunOpts("options are converted", convertedOptsOpts, func(mt *mtest.T) {
81+
nilOptsTestCases := []struct {
82+
name string
83+
opts []*options.InsertOneOptions
84+
expectOptionSet bool
85+
}{
86+
{
87+
"only nil is passed",
88+
[]*options.InsertOneOptions{nil},
89+
false,
90+
},
91+
{
92+
"non-nil options is passed before nil",
93+
[]*options.InsertOneOptions{options.InsertOne().SetBypassDocumentValidation(true), nil},
94+
true,
95+
},
96+
{
97+
"non-nil options is passed after nil",
98+
[]*options.InsertOneOptions{nil, options.InsertOne().SetBypassDocumentValidation(true)},
99+
true,
100+
},
101+
}
102+
103+
for _, testCase := range nilOptsTestCases {
104+
mt.Run(testCase.name, func(mt *mtest.T) {
105+
doc := bson.D{{"x", 1}}
106+
_, err := mt.Coll.InsertOne(mtest.Background, doc, testCase.opts...)
107+
assert.Nil(mt, err, "InsertOne error: %v", err)
108+
optName := "bypassDocumentValidation"
109+
evt := mt.GetStartedEvent()
110+
111+
val, err := evt.Command.LookupErr(optName)
112+
if testCase.expectOptionSet {
113+
assert.Nil(mt, err, "expected %v to be set but got: %v", optName, err)
114+
assert.True(mt, val.Boolean(), "expected %v to be true but got: %v", optName, val.Boolean())
115+
return
116+
}
117+
assert.NotNil(mt, err, "expected %v to be unset but got nil", optName)
118+
})
119+
}
120+
})
77121
})
78122
mt.RunOpts("insert many", noClientOpts, func(mt *mtest.T) {
79123
mt.Run("success", func(mt *mtest.T) {

mongo/options/insertoptions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (imo *InsertManyOptions) SetOrdered(b bool) *InsertManyOptions {
7373
return imo
7474
}
7575

76-
// MergeInsertManyOptions combines the givent InsertManyOptions instances into a single InsertManyOptions in a last one
76+
// MergeInsertManyOptions combines the given InsertManyOptions instances into a single InsertManyOptions in a last one
7777
// wins fashion.
7878
func MergeInsertManyOptions(opts ...*InsertManyOptions) *InsertManyOptions {
7979
imOpts := InsertMany()

0 commit comments

Comments
 (0)