1
0
forked from baron/baron-sso
Files
baron-sso/backend/internal/pagination/cursor_test.go

56 lines
1.3 KiB
Go

package pagination
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
type testItem struct {
id string
createdAt time.Time
}
func TestPageByCursorReturnsNextCursorAndNextPage(t *testing.T) {
now := time.Date(2026, 5, 13, 8, 0, 0, 0, time.UTC)
items := []testItem{
{id: "c", createdAt: now},
{id: "b", createdAt: now.Add(-time.Minute)},
{id: "a", createdAt: now.Add(-2 * time.Minute)},
}
key := func(item testItem) (time.Time, string) {
return item.createdAt, item.id
}
firstPage, nextCursor, err := PageByCursor(items, 2, "", key)
require.NoError(t, err)
require.Len(t, firstPage, 2)
require.NotEmpty(t, nextCursor)
secondPage, nextCursor, err := PageByCursor(items, 2, nextCursor, key)
require.NoError(t, err)
require.Len(t, secondPage, 1)
require.Empty(t, nextCursor)
require.Equal(t, "a", secondPage[0].id)
}
func TestSortByKeyDescUsesIDAsTieBreaker(t *testing.T) {
now := time.Date(2026, 5, 13, 8, 0, 0, 0, time.UTC)
items := []testItem{
{id: "a", createdAt: now},
{id: "c", createdAt: now},
{id: "b", createdAt: now},
}
SortByKeyDesc(items, func(item testItem) (time.Time, string) {
return item.createdAt, item.id
})
require.Equal(t, []string{"c", "b", "a"}, []string{
items[0].id,
items[1].id,
items[2].id,
})
}