Initial import
This commit is contained in:
56
main_index_test.go
Normal file
56
main_index_test.go
Normal file
@@ -0,0 +1,56 @@
|
||||
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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user