-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexes_test.go
167 lines (148 loc) · 4.07 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package modm
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type testIndex struct {
Key bson.D
Name string
Unique bool
}
func verifyIndexExists(t *testing.T, iv mongo.IndexView, expected testIndex) {
cursor, err := iv.List(context.Background())
assert.Nil(t, err, "List error: %v", err)
var found bool
for cursor.Next(context.Background()) {
var idx testIndex
err = cursor.Decode(&idx)
assert.Nil(t, err, "Decode error: %v", err)
if idx.Name == expected.Name {
if expected.Key != nil {
assert.Equal(t, expected.Key, idx.Key, "key document mismatch; expected %v, got %v", expected.Key, idx.Key)
}
assert.Equal(t, expected.Unique, idx.Unique)
found = true
}
}
assert.Nil(t, cursor.Err(), "cursor error: %v", err)
assert.True(t, found, "expected to find index %v but was not found", expected.Name)
}
func TestRepo_EnsureIndexes(t *testing.T) {
// Create a test Repo instance
db, cleanup := setupTestDatabase(t)
defer cleanup()
repo := NewRepo[*TestUser](db.Collection(testColl))
// Define unique and non-unique indexes for testing
uniques := []string{"name"}
indexes := []string{"name,-age", "-name", "age,-name"}
indexModel := mongo.IndexModel{
Keys: bson.D{{"foo", "text"}},
Options: options.Index().
SetExpireAfterSeconds(10).
SetName("a").
SetSparse(false).
SetUnique(false).
SetVersion(1).
SetDefaultLanguage("english").
SetLanguageOverride("english").
SetTextVersion(1).
SetWeights(bson.D{}).
SetSphereVersion(1).
SetBits(2).
SetMax(10).
SetMin(1).
SetPartialFilterExpression(bson.D{}).
SetStorageEngine(bson.D{
{"wiredTiger", bson.D{
{"configString", "block_compressor=zlib"},
}},
}),
}
// Call the EnsureIndexes function
ctx := context.TODO()
err := repo.EnsureIndexes(ctx, uniques, indexes, indexModel)
require.NoError(t, err)
// Validate that indexes were created
indexView := repo.collection.Indexes()
// Validate unique indexes
for _, item := range IndexesToModel(uniques, indexes) {
key := item.Keys.(bson.D)
name := ""
for i, kv := range key {
if i >= 1 {
name += "_"
}
name += fmt.Sprintf("%s_%d", kv.Key, kv.Value)
}
unique := false
if item.Options != nil {
unique = *item.Options.Unique
}
verifyIndexExists(t, indexView, testIndex{
Key: key,
Name: name,
Unique: unique,
})
}
}
func (u *TestUser) Uniques() []string {
return []string{"name"}
}
func (u *TestUser) Indexes() []string {
return []string{"name,-age", "-name", "age,-name"}
}
func (u *TestUser) IndexModels() []mongo.IndexModel {
return []mongo.IndexModel{
{
Keys: bson.D{
{Key: "area_code", Value: int32(1)},
{Key: "phone_number", Value: int32(1)},
},
Options: options.Index().SetUnique(true).SetPartialFilterExpression(bson.D{
{Key: "area_code", Value: bson.D{{Key: "$exists", Value: true}}},
{Key: "phone_number", Value: bson.D{{Key: "$exists", Value: true}}},
}),
},
}
}
func TestRepo_EnsureIndexesByModel(t *testing.T) {
// Create a test Repo instance
db, cleanup := setupTestDatabase(t)
defer cleanup()
repo := NewRepo[*TestUser](db.Collection(testColl))
// Call the EnsureIndexesByModel function
ctx := context.TODO()
testUserModel := TestUser{}
err := repo.EnsureIndexesByModel(ctx, &testUserModel)
require.NoError(t, err)
// Validate that indexes were created
indexView := repo.collection.Indexes()
indexModexs := IndexesToModel(testUserModel.Uniques(), testUserModel.Indexes())
indexModexs = append(indexModexs, testUserModel.IndexModels()...)
// Validate unique indexes
for _, item := range indexModexs {
key := item.Keys.(bson.D)
name := ""
for i, kv := range key {
if i >= 1 {
name += "_"
}
name += fmt.Sprintf("%s_%d", kv.Key, kv.Value)
}
unique := false
if item.Options != nil {
unique = *item.Options.Unique
}
verifyIndexExists(t, indexView, testIndex{
Key: key,
Name: name,
Unique: unique,
})
}
}