-
Notifications
You must be signed in to change notification settings - Fork 36
/
pagingQuery_test.go
295 lines (261 loc) · 7.94 KB
/
pagingQuery_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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package mongopagination
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
"strings"
"sync"
"testing"
"time"
)
type TodoTest struct {
Title string `json:"title" bson:"title"`
Status string `json:"status" bson:"status"`
CreatedAt time.Time `json:"createdAt" bson:"createdAt"`
}
const (
DatabaseHost string = "mongodb://localhost:27017"
DatabaseName string = "todo"
DatabaseCollection string = "TodoTest"
)
func cleanup(db *mongo.Database) (err error) {
err = db.Collection(DatabaseCollection).Drop(context.Background())
return
}
func insertExamples(db *mongo.Database) (insertedIds []interface{}, err error) {
var data []interface{}
for i := 0; i < 20; i++ {
data = append(data, bson.M{
"title": fmt.Sprintf("todo-%d", i),
"status": "active",
"createdAt": time.Now(),
})
}
result, err := db.Collection(DatabaseCollection).InsertMany(
context.Background(), data)
if err != nil {
return nil, err
}
return result.InsertedIDs, nil
}
func insertExamplesForSortingTest(db *mongo.Database) (insertedIds []interface{}, err error) {
var data []interface{}
data = append(data, bson.M{"title": "a"})
data = append(data, bson.M{"title": "c"})
data = append(data, bson.M{"title": "b"})
data = append(data, bson.M{"title": "A"})
data = append(data, bson.M{"title": "C"})
data = append(data, bson.M{"title": "B"})
result, err := db.Collection(DatabaseCollection).InsertMany(
context.Background(), data)
if err != nil {
return nil, err
}
return result.InsertedIDs, nil
}
func TestPagingQuery_FindWithCollation(t *testing.T) {
_, session := NewConnection()
db := session.Database(DatabaseName)
defer cleanup(db)
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
insertedIds, err := insertExamplesForSortingTest(db)
if len(insertedIds) < 1 {
t.Errorf("Empty insert ids")
}
if err != nil {
t.Errorf("Data insert error. Error: %s", err.Error())
}
filter := bson.M{}
var limit int64 = 10
var page int64
collection := db.Collection(DatabaseCollection)
var todos []TodoTest
collation := options.Collation{
Locale: "en",
CaseLevel: true,
}
paginatedData, err := New(collection).SetCollation(&collation).Context(ctx).Limit(limit).Page(page).Sort("title", 1).Filter(filter).Decode(&todos).Find()
if err != nil {
t.Errorf("Error while pagination. Error: %s", err.Error())
}
if paginatedData == nil {
t.Errorf("Empty Pagination data error")
return
}
if len(todos) < 1 {
t.Errorf("Error fetching data")
}
if paginatedData.Pagination.Total != 6 || paginatedData.Pagination.Page != 1 {
t.Errorf("False Pagination data should be 6 but got: %d", paginatedData.Pagination.Total)
}
//Check if all title are in the right order
if !strings.EqualFold(todos[0].Title, "a") || !strings.EqualFold(todos[1].Title, "a") {
t.Errorf("Index 0 and 1 should be a and A. But index 0 was %s and index 1 was %s", todos[0].Title, todos[1].Title)
}
if !strings.EqualFold(todos[2].Title, "b") || !strings.EqualFold(todos[3].Title, "b") {
t.Errorf("Index 2 and 3 should be b and B. But index 2 was %s and index 3 was %s", todos[2].Title, todos[3].Title)
}
if !strings.EqualFold(todos[4].Title, "c") || !strings.EqualFold(todos[5].Title, "c") {
t.Errorf("Index 4 and 5 should be c and C. But index 4 was %s and index 5 was %s", todos[4].Title, todos[5].Title)
}
}
func TestPagingQuery_Find(t *testing.T) {
_, session := NewConnection()
db := session.Database(DatabaseName)
defer cleanup(db)
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
insertedIds, err := insertExamples(db)
if len(insertedIds) < 1 {
t.Errorf("Empty insert ids")
}
if err != nil {
t.Errorf("Data insert error. Error: %s", err.Error())
}
filter := bson.M{}
var limit int64 = 10
var page int64
projection := bson.D{
{"title", 1},
{"status", 1},
}
collection := db.Collection(DatabaseCollection)
var todos []TodoTest
paginatedData, err := New(collection).Context(ctx).Limit(limit).Page(page).Sort("price", -1).Select(projection).Filter(filter).Decode(&todos).Find()
if err != nil {
t.Errorf("Error while pagination. Error: %s", err.Error())
}
if paginatedData == nil {
t.Errorf("Empty Pagination data error")
return
}
if len(todos) < 1 {
t.Errorf("Error fetching data")
}
if paginatedData.Pagination.Total != 20 || paginatedData.Pagination.Page != 1 {
t.Errorf("False Pagination data should be 20 but got: %d", paginatedData.Pagination.Total)
}
// no limit or page provided error
_, noLimitOrPageError := New(collection).Sort("price", -1).Select(projection).Filter(filter).Find()
if noLimitOrPageError == nil {
t.Errorf("Error expected but got no error")
}
// no filter error
_, noFilterError := New(collection).Limit(limit).Page(page).Sort("price", -1).Select(projection).Find()
if noFilterError == nil {
t.Errorf("Error expected but got no error")
}
}
func TestPagingQuery_Aggregate(t *testing.T) {
_, session := NewConnection()
db := session.Database(DatabaseName)
collection := db.Collection(DatabaseCollection)
defer cleanup(db)
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
insertedIds, err := insertExamples(db)
if len(insertedIds) < 1 {
t.Errorf("Empty insert ids")
}
if err != nil {
t.Errorf("Data insert error. Error: %s", err.Error())
}
// getting page 2 data
var limit int64 = 10
var page int64
// Aggregate pipeline pagination test
match := bson.M{"$match": bson.M{"status": "active"}}
filter := bson.M{}
//check Aggregate Error if decoder is being used which is not supported yet
var todos []TodoTest
_, decodeErrorTest := New(collection).Context(ctx).Limit(limit).Page(page).Decode(todos).Aggregate(match)
if decodeErrorTest == nil {
t.Errorf("error expected because Decode feature is not supported")
return
}
aggPaginatedData, err := New(collection).Context(ctx).Limit(limit).Page(page).Sort("price", -1).Aggregate(match)
if err != nil {
t.Errorf("Error while Aggregation pagination. Error: %s", err.Error())
}
if aggPaginatedData == nil {
t.Errorf("Empty Aggregated Pagination data error")
return
}
// Aggregation error match query test
faultyMatch := bson.M{"$matches": bson.M{"status": "active"}}
_, faultyMatchQuery := New(collection).Sort("price", -1).Aggregate(faultyMatch)
if faultyMatchQuery == nil {
t.Errorf("Error expected but got no error")
}
// no limit or page provided error
_, noLimitOrPageAggError := New(collection).Sort("price", -1).Aggregate(match)
if noLimitOrPageAggError == nil {
t.Errorf("Error expected but got no error")
}
// filter in aggregate error
_, noFilterAggError := New(collection).Limit(limit).Page(page).Filter(filter).Sort("price", -1).Aggregate(match)
if noFilterAggError == nil {
t.Errorf("Error expected but got no error")
}
// without sorting test
_, sortProvideTest := New(collection).Aggregate(match)
if sortProvideTest == nil {
t.Errorf("data expected")
return
}
}
func NewConnection() (a *mongo.Database, b *mongo.Client) {
var connectOnce sync.Once
var db *mongo.Database
var session *mongo.Client
connectOnce.Do(func() {
db, session = connect()
})
return db, session
}
func connect() (a *mongo.Database, b *mongo.Client) {
var err error
session, err := mongo.NewClient(options.Client().ApplyURI(DatabaseHost))
if err != nil {
log.Fatal(err)
}
err = session.Connect(context.TODO())
if err != nil {
log.Fatal(err)
}
var db = session.Database(DatabaseName)
return db, session
}
func TestGetSkip(t *testing.T) {
tc := []struct {
limit int64
page int64
expected int64
}{
{
limit: 10,
page: -1,
expected: 0,
},
{
limit: 10,
page: 1,
expected: 0,
}, {
limit: 10,
page: 2,
expected: 10,
}, {
limit: 10,
page: 3,
expected: 20,
},
}
for _, tt := range tc {
skip := getSkip(tt.page, tt.limit)
if skip != tt.expected {
t.Fatalf("expected skip to be %d, got %d", tt.expected, skip)
}
}
}