@@ -29,6 +29,9 @@ func TestIndexView(t *testing.T) {
29
29
mt := mtest .New (t , noClientOpts )
30
30
defer mt .Close ()
31
31
32
+ var pbool = func (b bool ) * bool { return & b }
33
+ var pint32 = func (i int32 ) * int32 { return & i }
34
+
32
35
mt .Run ("list" , func (mt * mtest.T ) {
33
36
createIndexes := func (mt * mtest.T , numIndexes int ) {
34
37
mt .Helper ()
@@ -472,21 +475,84 @@ func TestIndexView(t *testing.T) {
472
475
})
473
476
mt .RunOpts ("list specifications" , noClientOpts , func (mt * mtest.T ) {
474
477
mt .Run ("verify results" , func (mt * mtest.T ) {
475
- keysDoc := bsoncore .NewDocumentBuilder ().AppendInt32 ("_id" , 1 ).Build ()
476
- expectedSpec := & mongo.IndexSpecification {
477
- Name : "_id_" ,
478
- Namespace : mt .DB .Name () + "." + mt .Coll .Name (),
479
- KeysDocument : bson .Raw (keysDoc ),
480
- Version : 2 ,
478
+ // Create a handful of indexes
479
+ _ , err := mt .Coll .Indexes ().CreateMany (mtest .Background , []mongo.IndexModel {
480
+ {
481
+ Keys : bson.D {{"foo" , int32 (- 1 )}},
482
+ Options : options .Index ().SetUnique (true ),
483
+ },
484
+ {
485
+ Keys : bson.D {{"bar" , int32 (1 )}},
486
+ Options : options .Index ().SetExpireAfterSeconds (120 ),
487
+ },
488
+ {
489
+ Keys : bson.D {{"baz" , int32 (1 )}},
490
+ Options : options .Index ().SetSparse (true ),
491
+ },
492
+ {
493
+ Keys : bson.D {{"bar" , int32 (1 )}, {"baz" , int32 (- 1 )}},
494
+ },
495
+ })
496
+ assert .Nil (mt , err , "CreateMany error: %v" , err )
497
+
498
+ expectedSpecs := []* mongo.IndexSpecification {
499
+ {
500
+ Name : "_id_" ,
501
+ Namespace : mt .DB .Name () + "." + mt .Coll .Name (),
502
+ KeysDocument : bson .Raw (bsoncore .NewDocumentBuilder ().AppendInt32 ("_id" , 1 ).Build ()),
503
+ Version : 2 ,
504
+ ExpireAfterSeconds : nil ,
505
+ Sparse : nil ,
506
+ // ID index is special and does not return 'true', despite being unique.
507
+ Unique : nil ,
508
+ },
509
+ {
510
+ Name : "foo_-1" ,
511
+ Namespace : mt .DB .Name () + "." + mt .Coll .Name (),
512
+ KeysDocument : bson .Raw (bsoncore .NewDocumentBuilder ().AppendInt32 ("foo" , - 1 ).Build ()),
513
+ Version : 2 ,
514
+ ExpireAfterSeconds : nil ,
515
+ Sparse : nil ,
516
+ Unique : pbool (true ),
517
+ },
518
+ {
519
+ Name : "bar_1" ,
520
+ Namespace : mt .DB .Name () + "." + mt .Coll .Name (),
521
+ KeysDocument : bson .Raw (bsoncore .NewDocumentBuilder ().AppendInt32 ("bar" , 1 ).Build ()),
522
+ Version : 2 ,
523
+ ExpireAfterSeconds : pint32 (120 ),
524
+ Sparse : nil ,
525
+ Unique : nil ,
526
+ },
527
+ {
528
+ Name : "baz_1" ,
529
+ Namespace : mt .DB .Name () + "." + mt .Coll .Name (),
530
+ KeysDocument : bson .Raw (bsoncore .NewDocumentBuilder ().AppendInt32 ("baz" , 1 ).Build ()),
531
+ Version : 2 ,
532
+ ExpireAfterSeconds : nil ,
533
+ Sparse : pbool (true ),
534
+ Unique : nil ,
535
+ },
536
+ {
537
+ Name : "bar_1_baz_-1" ,
538
+ Namespace : mt .DB .Name () + "." + mt .Coll .Name (),
539
+ KeysDocument : bson .Raw (bsoncore .NewDocumentBuilder ().AppendInt32 ("bar" , 1 ).AppendInt32 ("baz" , - 1 ).Build ()),
540
+ Version : 2 ,
541
+ ExpireAfterSeconds : nil ,
542
+ Sparse : nil ,
543
+ Unique : nil ,
544
+ },
481
545
}
482
546
if mtest .CompareServerVersions (mtest .ServerVersion (), "3.4" ) < 0 {
483
- expectedSpec .Version = 1
547
+ for _ , expectedSpec := range expectedSpecs {
548
+ expectedSpec .Version = 1
549
+ }
484
550
}
485
551
486
552
specs , err := mt .Coll .Indexes ().ListSpecifications (mtest .Background )
487
553
assert .Nil (mt , err , "ListSpecifications error: %v" , err )
488
- assert .Equal (mt , 1 , len (specs ), "expected 1 specification, got %d" , len (specs ))
489
- assert .Equal (mt , expectedSpec , specs [ 0 ] , "expected specification %v, got %v" , expectedSpec , specs [ 0 ] )
554
+ assert .Equal (mt , len ( expectedSpecs ) , len (specs ), "expected %d specification, got %d" , len ( expectedSpecs ) , len (specs ))
555
+ assert .True (mt , cmp . Equal ( specs , expectedSpecs ) , "expected specifications to match: %v" , cmp . Diff ( specs , expectedSpecs ) )
490
556
})
491
557
mt .RunOpts ("options passed to listIndexes" , mtest .NewOptions ().MinServerVersion ("3.0" ), func (mt * mtest.T ) {
492
558
opts := options .ListIndexes ().SetMaxTime (100 * time .Millisecond )
0 commit comments