@@ -16,11 +16,19 @@ import (
16
16
"time"
17
17
18
18
"github.com/mongodb/mongo-go-driver/bson"
19
+ "github.com/mongodb/mongo-go-driver/core/options"
20
+ "github.com/mongodb/mongo-go-driver/core/writeconcern"
19
21
"github.com/stretchr/testify/require"
20
22
)
21
23
22
24
var seed = time .Now ().UnixNano ()
23
25
26
+ type index struct {
27
+ Key map [string ]int
28
+ NS string
29
+ Name string
30
+ }
31
+
24
32
func getIndexableCollection (t * testing.T ) (string , * Collection ) {
25
33
atomic .AddInt64 (& seed , 1 )
26
34
rand .Seed (atomic .LoadInt64 (& seed ))
@@ -60,11 +68,7 @@ func TestIndexView_List(t *testing.T) {
60
68
require .NoError (t , err )
61
69
62
70
found := false
63
- var index struct {
64
- Key map [string ]int
65
- NS string
66
- Name string
67
- }
71
+ var index = index {}
68
72
69
73
for cursor .Next (context .Background ()) {
70
74
err := cursor .Decode (& index )
@@ -107,11 +111,7 @@ func TestIndexView_CreateOne(t *testing.T) {
107
111
require .NoError (t , err )
108
112
109
113
found := false
110
- var index struct {
111
- Key map [string ]int
112
- NS string
113
- Name string
114
- }
114
+ var index = index {}
115
115
116
116
for cursor .Next (context .Background ()) {
117
117
err := cursor .Decode (& index )
@@ -142,6 +142,7 @@ func TestIndexView_CreateMany(t *testing.T) {
142
142
143
143
indexNames , err := indexView .CreateMany (
144
144
context .Background (),
145
+ []options.CreateIndexesOptioner {},
145
146
IndexModel {
146
147
Keys : bson .NewDocument (
147
148
bson .EC .Int32 ("foo" , - 1 ),
@@ -166,11 +167,7 @@ func TestIndexView_CreateMany(t *testing.T) {
166
167
167
168
fooFound := false
168
169
barBazFound := false
169
- var index struct {
170
- Key map [string ]int
171
- NS string
172
- Name string
173
- }
170
+ var index = index {}
174
171
175
172
for cursor .Next (context .Background ()) {
176
173
err := cursor .Decode (& index )
@@ -209,6 +206,7 @@ func TestIndexView_DropOne(t *testing.T) {
209
206
210
207
indexNames , err := indexView .CreateMany (
211
208
context .Background (),
209
+ []options.CreateIndexesOptioner {},
212
210
IndexModel {
213
211
Keys : bson .NewDocument (
214
212
bson .EC .Int32 ("foo" , - 1 ),
@@ -234,11 +232,7 @@ func TestIndexView_DropOne(t *testing.T) {
234
232
cursor , err := indexView .List (context .Background ())
235
233
require .NoError (t , err )
236
234
237
- var index struct {
238
- Key map [string ]int
239
- NS string
240
- Name string
241
- }
235
+ var index = index {}
242
236
243
237
for cursor .Next (context .Background ()) {
244
238
err := cursor .Decode (& index )
@@ -263,6 +257,7 @@ func TestIndexView_DropAll(t *testing.T) {
263
257
264
258
indexNames , err := indexView .CreateMany (
265
259
context .Background (),
260
+ []options.CreateIndexesOptioner {},
266
261
IndexModel {
267
262
Keys : bson .NewDocument (
268
263
bson .EC .Int32 ("foo" , - 1 ),
@@ -287,16 +282,142 @@ func TestIndexView_DropAll(t *testing.T) {
287
282
cursor , err := indexView .List (context .Background ())
288
283
require .NoError (t , err )
289
284
290
- var index struct {
291
- Key map [string ]int
292
- NS string
293
- Name string
285
+ var index = index {}
286
+
287
+ for cursor .Next (context .Background ()) {
288
+ err := cursor .Decode (& index )
289
+ require .NoError (t , err )
290
+
291
+ require .Equal (t , expectedNS , index .NS )
292
+ require .NotEqual (t , indexNames [0 ], index .Name )
293
+ require .NotEqual (t , indexNames [1 ], index .Name )
294
294
}
295
+ require .NoError (t , cursor .Err ())
296
+ }
297
+
298
+ func TestIndexView_CreateIndexesOptioner (t * testing.T ) {
299
+ t .Parallel ()
300
+
301
+ if testing .Short () {
302
+ t .Skip ()
303
+ }
304
+
305
+ dbName , coll := getIndexableCollection (t )
306
+ expectedNS := fmt .Sprintf ("IndexView.%s" , dbName )
307
+ indexView := coll .Indexes ()
308
+ var opts []options.CreateIndexesOptioner
309
+ wc := writeconcern .New (writeconcern .W (1 ))
310
+ elem , err := wc .MarshalBSONElement ()
311
+ require .NoError (t , err )
312
+ optwc := options.OptWriteConcern {WriteConcern : elem , Acknowledged : wc .Acknowledged ()}
313
+ opts = append (opts , optwc )
314
+ indexNames , err := indexView .CreateMany (
315
+ context .Background (),
316
+ opts ,
317
+ IndexModel {
318
+ Keys : bson .NewDocument (
319
+ bson .EC .Int32 ("foo" , - 1 ),
320
+ ),
321
+ },
322
+ IndexModel {
323
+ Keys : bson .NewDocument (
324
+ bson .EC .Int32 ("bar" , 1 ),
325
+ bson .EC .Int32 ("baz" , - 1 ),
326
+ ),
327
+ },
328
+ )
329
+ require .NoError (t , err )
330
+ require .NoError (t , err )
331
+
332
+ require .Len (t , indexNames , 2 )
333
+
334
+ fooName := indexNames [0 ]
335
+ barBazName := indexNames [1 ]
336
+
337
+ cursor , err := indexView .List (context .Background ())
338
+ require .NoError (t , err )
339
+
340
+ fooFound := false
341
+ barBazFound := false
342
+ var index = index {}
295
343
296
344
for cursor .Next (context .Background ()) {
297
345
err := cursor .Decode (& index )
298
346
require .NoError (t , err )
299
347
348
+ require .Equal (t , expectedNS , index .NS )
349
+
350
+ if index .Name == fooName {
351
+ require .Len (t , index .Key , 1 )
352
+ require .Equal (t , - 1 , index .Key ["foo" ])
353
+ fooFound = true
354
+ }
355
+
356
+ if index .Name == barBazName {
357
+ require .Len (t , index .Key , 2 )
358
+ require .Equal (t , 1 , index .Key ["bar" ])
359
+ require .Equal (t , - 1 , index .Key ["baz" ])
360
+ barBazFound = true
361
+ }
362
+ }
363
+ require .NoError (t , cursor .Err ())
364
+ require .True (t , fooFound )
365
+ require .True (t , barBazFound )
366
+ defer func () {
367
+ _ , err := indexView .DropAll (context .Background ())
368
+ require .NoError (t , err )
369
+ }()
370
+ }
371
+
372
+ func TestIndexView_DropIndexesOptioner (t * testing.T ) {
373
+ t .Parallel ()
374
+
375
+ if testing .Short () {
376
+ t .Skip ()
377
+ }
378
+
379
+ dbName , coll := getIndexableCollection (t )
380
+ expectedNS := fmt .Sprintf ("IndexView.%s" , dbName )
381
+ indexView := coll .Indexes ()
382
+ var opts []options.DropIndexesOptioner
383
+ wc := writeconcern .New (writeconcern .W (1 ))
384
+ elem , err := wc .MarshalBSONElement ()
385
+ require .NoError (t , err )
386
+ optwc := options.OptWriteConcern {WriteConcern : elem , Acknowledged : wc .Acknowledged ()}
387
+ opts = append (opts , optwc )
388
+ indexNames , err := indexView .CreateMany (
389
+ context .Background (),
390
+ []options.CreateIndexesOptioner {},
391
+ IndexModel {
392
+ Keys : bson .NewDocument (
393
+ bson .EC .Int32 ("foo" , - 1 ),
394
+ ),
395
+ },
396
+ IndexModel {
397
+ Keys : bson .NewDocument (
398
+ bson .EC .Int32 ("bar" , 1 ),
399
+ bson .EC .Int32 ("baz" , - 1 ),
400
+ ),
401
+ },
402
+ )
403
+ require .NoError (t , err )
404
+
405
+ require .Len (t , indexNames , 2 )
406
+
407
+ _ , err = indexView .DropAll (
408
+ context .Background (),
409
+ opts ... ,
410
+ )
411
+ require .NoError (t , err )
412
+
413
+ cursor , err := indexView .List (context .Background ())
414
+ require .NoError (t , err )
415
+
416
+ var index = index {}
417
+
418
+ for cursor .Next (context .Background ()) {
419
+ err := cursor .Decode (& index )
420
+ require .NoError (t , err )
300
421
require .Equal (t , expectedNS , index .NS )
301
422
require .NotEqual (t , indexNames [0 ], index .Name )
302
423
require .NotEqual (t , indexNames [1 ], index .Name )
0 commit comments