-
Notifications
You must be signed in to change notification settings - Fork 5
/
indexes_test.go
61 lines (44 loc) · 1.58 KB
/
indexes_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package colt
import (
"fmt"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson"
"math/rand"
"testing"
"time"
)
func TestCollection_CreateIndex(t *testing.T) {
rand.Seed(time.Now().UnixNano())
mockDb.Connect("mongodb://localhost:27017/colt?readPreference=primary&directConnection=true&ssl=false", "colt")
collection := GetCollection[*testdoc](&mockDb, "testdocs")
var indxs = []interface{}{}
indexCursor, _ := collection.collection.Indexes().List(DefaultContext())
indexCursor.All(DefaultContext(), &indxs)
indexCountBefore := len(indxs)
err := collection.CreateIndex(bson.D{
{fmt.Sprint(rand.Int()), 1},
})
assert.Nil(t, err)
indexCursor2, _ := collection.collection.Indexes().List(DefaultContext())
indexCursor2.All(DefaultContext(), &indxs)
// new index
assert.Equal(t, len(indxs), indexCountBefore+1)
}
func TestCollection_CreateMultiKeyIndex(t *testing.T) {
rand.Seed(time.Now().UnixNano())
mockDb.Connect("mongodb://localhost:27017/colt?readPreference=primary&directConnection=true&ssl=false", "colt")
collection := GetCollection[*testdoc](&mockDb, "testdocs")
var indxs = []interface{}{}
indexCursor, _ := collection.collection.Indexes().List(DefaultContext())
indexCursor.All(DefaultContext(), &indxs)
indexCountBefore := len(indxs)
err := collection.CreateIndex(bson.D{
{fmt.Sprint(rand.Int()), 1},
{fmt.Sprint(rand.Int()), 1},
})
assert.Nil(t, err)
indexCursor2, _ := collection.collection.Indexes().List(DefaultContext())
indexCursor2.All(DefaultContext(), &indxs)
// new index
assert.Equal(t, len(indxs), indexCountBefore+1)
}