@@ -15,6 +15,7 @@ import (
1515 "github.com/google/go-cmp/cmp"
1616 "go.mongodb.org/mongo-driver/bson"
1717 "go.mongodb.org/mongo-driver/internal/assert"
18+ "go.mongodb.org/mongo-driver/internal/require"
1819 "go.mongodb.org/mongo-driver/mongo"
1920 "go.mongodb.org/mongo-driver/mongo/integration/mtest"
2021 "go.mongodb.org/mongo-driver/mongo/options"
@@ -607,36 +608,109 @@ func TestIndexView(t *testing.T) {
607608 }
608609 assert .Nil (mt , cursor .Err (), "cursor error: %v" , cursor .Err ())
609610 })
610- mt .Run ("dropKey one" , func (mt * mtest.T ) {
611- iv := mt .Coll .Indexes ()
612- indexNames , err := iv .CreateMany (context .Background (), []mongo.IndexModel {
611+ mt .Run ("drop with key" , func (mt * mtest.T ) {
612+ tests := []struct {
613+ name string
614+ models []mongo.IndexModel
615+ index any
616+ }{
613617 {
614- Keys : bson .Raw (bsoncore .NewDocumentBuilder ().AppendInt32 ("_id" , 1 ).Build ()),
618+ name : "custom index name and unique indexes" ,
619+ models : []mongo.IndexModel {
620+ {
621+ Keys : bson.D {{"_id" , 1 }},
622+ },
623+ {
624+ Keys : bson.D {{"username" , 1 }},
625+ Options : options .Index ().SetUnique (true ).SetName ("myidx" ),
626+ },
627+ },
628+ index : bson.D {{"username" , 1 }},
615629 },
616630 {
617- Keys : bson .Raw (bsoncore .NewDocumentBuilder ().AppendInt32 ("username" , 1 ).Build ()),
618- Options : options .Index ().SetUnique (true ).SetName ("myidx" ),
631+ name : "normal generated index name" ,
632+ models : []mongo.IndexModel {
633+ {
634+ Keys : bson.D {{"_id" , 1 }},
635+ },
636+ {
637+ Keys : bson.D {{"foo" , - 1 }},
638+ },
639+ },
640+ index : bson.D {{"foo" , - 1 }},
641+ },
642+ {
643+ name : "compound index" ,
644+ models : []mongo.IndexModel {
645+ {
646+ Keys : bson.D {{"_id" , 1 }},
647+ },
648+ {
649+ Keys : bson.D {{"foo" , 1 }, {"bar" , 1 }},
650+ },
651+ },
652+ index : bson.D {{"foo" , 1 }, {"bar" , 1 }},
653+ },
654+ /*{
655+ name: "clustered indexes",
656+ models: []mongo.IndexModel{
657+ {
658+ Keys: bson.Raw(bsoncore.NewDocumentBuilder().AppendInt32("foo", 1).Build()),
659+ },
660+ {
661+ Keys: map[string]interface{}{"key": bson.D{{"_id", 1}}},
662+ },
663+ },
664+ index: map[string]interface{}{"key": bson.D{{"_id", 1}}},
665+ },*/
666+ {
667+ name : "text index" ,
668+ models : []mongo.IndexModel {
669+ {
670+ Keys : bson.D {{"_id" , 1 }},
671+ },
672+ {
673+ Keys : bson.D {{"plot" , "text" }},
674+ },
675+ },
676+ index : map [string ]interface {}{"_fts" : "text" , "_ftsx" : 1 },
677+ //index: map[string]interface{}{"plot": "text"},
619678 },
620- })
621-
622- key := map [string ]interface {}{
623- "username" : 1 ,
624679 }
625- assert .Nil (mt , err , "CreateMany error: %v" , err )
626- assert .Equal (mt , 2 , len (indexNames ), "expected 2 index names, got %v" , len (indexNames ))
627680
628- _ , err = iv .DropKeyOne (context .Background (), key )
629- assert .Nil (mt , err , "DropOne error: %v" , err )
681+ for _ , test := range tests {
682+ mt .Run (test .name , func (mt * mtest.T ) {
683+ iv := mt .Coll .Indexes ()
684+ indexNames , err := iv .CreateMany (context .Background (), test .models )
630685
631- cursor , err := iv .List (context .Background ())
632- assert .Nil (mt , err , "List error: %v" , err )
633- for cursor .Next (context .Background ()) {
634- var idx index
635- err = cursor .Decode (& idx )
636- assert .Nil (mt , err , "Decode error: %v (document %v)" , err , cursor .Current )
637- assert .NotEqual (mt , indexNames [1 ], idx .Name , "found index %v after dropping" , indexNames [1 ])
686+ assert .NoError (mt , err )
687+ assert .Equal (mt , 2 , len (indexNames ), "expected 2 index names, got %v" , len (indexNames ))
688+
689+ // List existing indexes
690+ cursor , err := mt .Coll .Indexes ().List (context .Background ())
691+ require .NoError (t , err )
692+
693+ var indexes []bson.M
694+ err = cursor .All (context .Background (), & indexes )
695+ require .NoError (t , err )
696+
697+ t .Logf ("Existing indexes: %v" , indexes )
698+ //////
699+
700+ _ , err = iv .DropWithKey (context .Background (), test .index )
701+ assert .Nil (mt , err , "DropOne error: %v" , err )
702+
703+ cursor , err = iv .List (context .Background ())
704+ assert .Nil (mt , err , "List error: %v" , err )
705+ for cursor .Next (context .Background ()) {
706+ var idx index
707+ err = cursor .Decode (& idx )
708+ assert .Nil (mt , err , "Decode error: %v (document %v)" , err , cursor .Current )
709+ assert .NotEqual (mt , indexNames [1 ], idx .Name , "found index %v after dropping" , indexNames [1 ])
710+ }
711+ assert .Nil (mt , cursor .Err (), "cursor error: %v" , cursor .Err ())
712+ })
638713 }
639- assert .Nil (mt , cursor .Err (), "cursor error: %v" , cursor .Err ())
640714 })
641715 mt .Run ("drop all" , func (mt * mtest.T ) {
642716 iv := mt .Coll .Indexes ()
0 commit comments