@@ -137,6 +137,55 @@ func TestIndexView(t *testing.T) {
137
137
})
138
138
assert .NotNil (mt , err , "expected CreateOne error, got nil" )
139
139
})
140
+ // Only run on replica sets as commitQuorum is not supported on standalones.
141
+ mt .RunOpts ("commit quorum" , mtest .NewOptions ().Topologies (mtest .ReplicaSet ).CreateClient (false ), func (mt * mtest.T ) {
142
+ intVal := options .CreateIndexes ().SetCommitQuorumInt (1 )
143
+ stringVal := options .CreateIndexes ().SetCommitQuorumString ("majority" )
144
+ majority := options .CreateIndexes ().SetCommitQuorumMajority ()
145
+ votingMembers := options .CreateIndexes ().SetCommitQuorumVotingMembers ()
146
+
147
+ indexModel := mongo.IndexModel {
148
+ Keys : bson.D {{"x" , 1 }},
149
+ }
150
+
151
+ testCases := []struct {
152
+ name string
153
+ opts * options.CreateIndexesOptions
154
+ expectError bool
155
+ expectedValue interface {} // ignored if expectError is true
156
+ minServerVersion string
157
+ maxServerVersion string
158
+ }{
159
+ {"error on server versions before 4.4" , majority , true , nil , "" , "4.2" },
160
+ {"integer value" , intVal , false , int32 (1 ), "4.4" , "" },
161
+ {"string value" , stringVal , false , "majority" , "4.4" , "" },
162
+ {"majority" , majority , false , "majority" , "4.4" , "" },
163
+ {"votingMembers" , votingMembers , false , "votingMembers" , "4.4" , "" },
164
+ }
165
+ for _ , tc := range testCases {
166
+ mtOpts := mtest .NewOptions ().MinServerVersion (tc .minServerVersion ).MaxServerVersion (tc .maxServerVersion )
167
+ mt .RunOpts (tc .name , mtOpts , func (mt * mtest.T ) {
168
+ mt .ClearEvents ()
169
+ _ , err := mt .Coll .Indexes ().CreateOne (mtest .Background , indexModel , tc .opts )
170
+ if tc .expectError {
171
+ assert .NotNil (mt , err , "expected CreateOne error, got nil" )
172
+ return
173
+ }
174
+
175
+ assert .Nil (mt , err , "CreateOne error: %v" , err )
176
+ cmd := mt .GetStartedEvent ().Command
177
+ sentBSONValue , err := cmd .LookupErr ("commitQuorum" )
178
+ assert .Nil (mt , err , "expected commitQuorum in command %s" , cmd )
179
+
180
+ var sentValue interface {}
181
+ err = sentBSONValue .Unmarshal (& sentValue )
182
+ assert .Nil (mt , err , "Unmarshal error: %v" , err )
183
+
184
+ assert .Equal (mt , tc .expectedValue , sentValue , "expected commitQuorum value %v, got %v" ,
185
+ tc .expectedValue , sentValue )
186
+ })
187
+ }
188
+ })
140
189
})
141
190
mt .Run ("create many" , func (mt * mtest.T ) {
142
191
mt .Run ("success" , func (mt * mtest.T ) {
@@ -162,31 +211,79 @@ func TestIndexView(t *testing.T) {
162
211
})
163
212
})
164
213
wc := writeconcern .New (writeconcern .W (1 ))
165
- mt .RunOpts ("uses writeconcern" ,
166
- mtest .NewOptions ().CollectionOptions (options .Collection ().SetWriteConcern (wc )),
167
- func (mt * mtest.T ) {
168
- iv := mt .Coll .Indexes ()
169
- _ , err := iv .CreateMany (mtest .Background , []mongo.IndexModel {
170
- {
171
- Keys : bson.D {{"foo" , - 1 }},
172
- },
173
- {
174
- Keys : bson.D {{"bar" , 1 }, {"baz" , - 1 }},
175
- },
176
- })
177
- assert .Nil (mt , err , "CreateMany error: %v" , err )
214
+ wcMtOpts := mtest .NewOptions ().CollectionOptions (options .Collection ().SetWriteConcern (wc ))
215
+ mt .RunOpts ("uses writeconcern" , wcMtOpts , func (mt * mtest.T ) {
216
+ iv := mt .Coll .Indexes ()
217
+ _ , err := iv .CreateMany (mtest .Background , []mongo.IndexModel {
218
+ {
219
+ Keys : bson.D {{"foo" , - 1 }},
220
+ },
221
+ {
222
+ Keys : bson.D {{"bar" , 1 }, {"baz" , - 1 }},
223
+ },
224
+ })
225
+ assert .Nil (mt , err , "CreateMany error: %v" , err )
178
226
179
- evt := mt .GetStartedEvent ()
180
- assert .NotNil (mt , evt , "expected CommandStartedEvent, got nil" )
227
+ evt := mt .GetStartedEvent ()
228
+ assert .NotNil (mt , evt , "expected CommandStartedEvent, got nil" )
181
229
182
- assert .Equal (mt , "createIndexes" , evt .CommandName , "command name mismatch; expected createIndexes, got %s" , evt .CommandName )
230
+ assert .Equal (mt , "createIndexes" , evt .CommandName , "command name mismatch; expected createIndexes, got %s" , evt .CommandName )
183
231
184
- actual , err := evt .Command .LookupErr ("writeConcern" , "w" )
185
- assert .Nil (mt , err , "error getting writeConcern.w: %s" , err )
232
+ actual , err := evt .Command .LookupErr ("writeConcern" , "w" )
233
+ assert .Nil (mt , err , "error getting writeConcern.w: %s" , err )
186
234
187
- wcVal := numberFromValue (mt , actual )
188
- assert .Equal (mt , int64 (1 ), wcVal , "expected writeConcern to be 1, got: %v" , wcVal )
189
- })
235
+ wcVal := numberFromValue (mt , actual )
236
+ assert .Equal (mt , int64 (1 ), wcVal , "expected writeConcern to be 1, got: %v" , wcVal )
237
+ })
238
+ // Only run on replica sets as commitQuorum is not supported on standalones.
239
+ mt .RunOpts ("commit quorum" , mtest .NewOptions ().Topologies (mtest .ReplicaSet ).CreateClient (false ), func (mt * mtest.T ) {
240
+ intVal := options .CreateIndexes ().SetCommitQuorumInt (1 )
241
+ stringVal := options .CreateIndexes ().SetCommitQuorumString ("majority" )
242
+ majority := options .CreateIndexes ().SetCommitQuorumMajority ()
243
+ votingMembers := options .CreateIndexes ().SetCommitQuorumVotingMembers ()
244
+
245
+ indexModel := mongo.IndexModel {
246
+ Keys : bson.D {{"x" , 1 }},
247
+ }
248
+
249
+ testCases := []struct {
250
+ name string
251
+ opts * options.CreateIndexesOptions
252
+ expectError bool
253
+ expectedValue interface {} // ignored if expectError is true
254
+ minServerVersion string
255
+ maxServerVersion string
256
+ }{
257
+ {"error on server versions before 4.4" , majority , true , nil , "" , "4.2" },
258
+ {"integer value" , intVal , false , int32 (1 ), "4.4" , "" },
259
+ {"string value" , stringVal , false , "majority" , "4.4" , "" },
260
+ {"majority" , majority , false , "majority" , "4.4" , "" },
261
+ {"votingMembers" , votingMembers , false , "votingMembers" , "4.4" , "" },
262
+ }
263
+ for _ , tc := range testCases {
264
+ mtOpts := mtest .NewOptions ().MinServerVersion (tc .minServerVersion ).MaxServerVersion (tc .maxServerVersion )
265
+ mt .RunOpts (tc .name , mtOpts , func (mt * mtest.T ) {
266
+ mt .ClearEvents ()
267
+ _ , err := mt .Coll .Indexes ().CreateOne (mtest .Background , indexModel , tc .opts )
268
+ if tc .expectError {
269
+ assert .NotNil (mt , err , "expected CreateOne error, got nil" )
270
+ return
271
+ }
272
+
273
+ assert .Nil (mt , err , "CreateOne error: %v" , err )
274
+ cmd := mt .GetStartedEvent ().Command
275
+ sentBSONValue , err := cmd .LookupErr ("commitQuorum" )
276
+ assert .Nil (mt , err , "expected commitQuorum in command %s" , cmd )
277
+
278
+ var sentValue interface {}
279
+ err = sentBSONValue .Unmarshal (& sentValue )
280
+ assert .Nil (mt , err , "Unmarshal error: %v" , err )
281
+
282
+ assert .Equal (mt , tc .expectedValue , sentValue , "expected commitQuorum value %v, got %v" ,
283
+ tc .expectedValue , sentValue )
284
+ })
285
+ }
286
+ })
190
287
})
191
288
mt .Run ("drop one" , func (mt * mtest.T ) {
192
289
iv := mt .Coll .Indexes ()
0 commit comments