Skip to content

Commit eeb7f65

Browse files
committed
add test
1 parent a316448 commit eeb7f65

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

mongox/pagination_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,89 @@ func TestClientCollection_PaginateAggregation(t *testing.T) {
261261
assert.Equal(t, []usecasex.Cursor{"c", "b", "a"}, con.Cursors)
262262
}
263263

264+
func TestClientCollection_PaginateWithUpdatedAtSort(t *testing.T) {
265+
ctx := context.Background()
266+
initDB := mongotest.Connect(t)
267+
c := NewCollection(initDB(t).Collection("test"))
268+
269+
seeds := []struct {
270+
id string
271+
updatedAt int64
272+
}{
273+
{"a", 1000},
274+
{"b", 2000},
275+
{"c", 3000},
276+
{"d", 4000},
277+
{"e", 5000},
278+
}
279+
280+
_, _ = c.Client().InsertMany(ctx, lo.Map(seeds, func(s struct {
281+
id string
282+
updatedAt int64
283+
}, i int) any {
284+
return bson.M{"id": s.id, "updatedAt": s.updatedAt}
285+
}))
286+
287+
sortOpt := &usecasex.Sort{Key: "updatedAt", Reverted: false}
288+
289+
p := usecasex.CursorPagination{
290+
First: lo.ToPtr(int64(2)),
291+
}
292+
293+
con := &consumer{}
294+
got, err := c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con)
295+
assert.NoError(t, err)
296+
assert.Equal(t, []usecasex.Cursor{"a", "b"}, con.Cursors)
297+
assert.True(t, got.HasNextPage)
298+
assert.False(t, got.HasPreviousPage)
299+
300+
p = usecasex.CursorPagination{
301+
Last: lo.ToPtr(int64(2)),
302+
}
303+
304+
con = &consumer{}
305+
got, err = c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con)
306+
assert.NoError(t, err)
307+
assert.Equal(t, []usecasex.Cursor{"d", "e"}, con.Cursors)
308+
assert.False(t, got.HasNextPage)
309+
assert.True(t, got.HasPreviousPage)
310+
311+
p = usecasex.CursorPagination{
312+
First: lo.ToPtr(int64(2)),
313+
After: usecasex.Cursor("b").Ref(),
314+
}
315+
316+
con = &consumer{}
317+
got, err = c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con)
318+
assert.NoError(t, err)
319+
assert.Equal(t, []usecasex.Cursor{"c", "d"}, con.Cursors)
320+
assert.True(t, got.HasNextPage)
321+
assert.True(t, got.HasPreviousPage)
322+
323+
p = usecasex.CursorPagination{
324+
Last: lo.ToPtr(int64(2)),
325+
Before: usecasex.Cursor("d").Ref(),
326+
}
327+
328+
con = &consumer{}
329+
got, err = c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con)
330+
assert.NoError(t, err)
331+
assert.Equal(t, []usecasex.Cursor{"b", "c"}, con.Cursors)
332+
assert.True(t, got.HasNextPage)
333+
assert.True(t, got.HasPreviousPage)
334+
335+
p = usecasex.CursorPagination{
336+
Last: lo.ToPtr(int64(3)),
337+
}
338+
339+
con = &consumer{}
340+
got, err = c.Paginate(ctx, bson.M{}, sortOpt, p.Wrap(), con)
341+
assert.NoError(t, err)
342+
assert.Equal(t, []usecasex.Cursor{"c", "d", "e"}, con.Cursors)
343+
assert.False(t, got.HasNextPage)
344+
assert.True(t, got.HasPreviousPage)
345+
}
346+
264347
type consumer struct {
265348
Cursors []usecasex.Cursor
266349
}

0 commit comments

Comments
 (0)