package mongo import ( "context" "testing" "time" "go.mongodb.org/mongo-driver/v2/bson" ) func TestCreateIndexes(t *testing.T) { cd := &CollectionDefinition{ IndexSpecs: []map[string]any{ { "keys": map[string]any{ "code": 1, }, "name": "idx_1", }, }, } c := GetMongoClient() collection := c.GetCollection("mydb", "mycol") c.CreateIndexes(collection, cd) indexView := collection.Indexes() // Specify a timeout to limit the amount of time the operation can run on // the server. ctx, cancel := context.WithTimeout(context.TODO(), time.Second) defer cancel() cursor, err := indexView.List(ctx, nil) if err != nil { t.Fatal(err) } // Get a slice of all indexes returned and print them out. var results []bson.M if err = cursor.All(ctx, &results); err != nil { t.Fatal(err) } idx1 := results[1] if len(results) != 2 { t.Fatal("Unexpected number of indexes") } if idx1["name"] != "idx_1" { t.Fatal("Should have register index idx_1") } }