@@ -74,6 +74,50 @@ func TestCollection(t *testing.T) {
74
74
assert .True (mt , ok , "expected error type %v, got %v" , mongo.WriteException {}, err )
75
75
assert .NotNil (mt , we .WriteConcernError , "expected write concern error, got %+v" , we )
76
76
})
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
+ })
77
121
})
78
122
mt .RunOpts ("insert many" , noClientOpts , func (mt * mtest.T ) {
79
123
mt .Run ("success" , func (mt * mtest.T ) {
0 commit comments