Initial import

This commit is contained in:
George Suntres
2026-03-29 11:38:57 -04:00
commit 38265c15d1
25 changed files with 1763 additions and 0 deletions

56
main_index_test.go Normal file
View 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")
}
}