2019-10-04 22:46:49 +00:00
|
|
|
package pagination_test
|
2019-07-25 19:45:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-10-04 22:46:49 +00:00
|
|
|
|
2024-02-15 05:46:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v5/api/pagination"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/require"
|
2019-07-25 19:45:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStartAndEndPage(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
token string
|
|
|
|
pageSize int
|
|
|
|
totalSize int
|
|
|
|
nextToken string
|
|
|
|
start int
|
|
|
|
end int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
token: "0",
|
|
|
|
pageSize: 9,
|
|
|
|
totalSize: 100,
|
|
|
|
nextToken: "1",
|
|
|
|
start: 0,
|
|
|
|
end: 9,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
token: "10",
|
|
|
|
pageSize: 4,
|
|
|
|
totalSize: 100,
|
|
|
|
nextToken: "11",
|
|
|
|
start: 40,
|
|
|
|
end: 44,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
token: "100",
|
|
|
|
pageSize: 5,
|
|
|
|
totalSize: 1000,
|
|
|
|
nextToken: "101",
|
|
|
|
start: 500,
|
|
|
|
end: 505,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
token: "3",
|
|
|
|
pageSize: 33,
|
|
|
|
totalSize: 100,
|
2019-12-03 17:45:43 +00:00
|
|
|
nextToken: "",
|
2019-07-25 19:45:31 +00:00
|
|
|
start: 99,
|
|
|
|
end: 100,
|
|
|
|
},
|
2020-06-19 14:39:12 +00:00
|
|
|
{
|
|
|
|
token: "34",
|
|
|
|
pageSize: 500,
|
|
|
|
totalSize: 17500,
|
|
|
|
nextToken: "",
|
|
|
|
start: 17000,
|
|
|
|
end: 17500,
|
|
|
|
},
|
2019-07-25 19:45:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2019-10-04 22:46:49 +00:00
|
|
|
start, end, next, err := pagination.StartAndEndPage(test.token, test.pageSize, test.totalSize)
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
2019-07-25 19:45:31 +00:00
|
|
|
if test.start != start {
|
|
|
|
t.Errorf("expected start and computed start are not equal %d, %d", test.start, start)
|
|
|
|
}
|
|
|
|
if test.end != end {
|
|
|
|
t.Errorf("expected end and computed end are not equal %d, %d", test.end, end)
|
|
|
|
}
|
|
|
|
if test.nextToken != next {
|
|
|
|
t.Errorf("expected next token and computed next token are not equal %v, %v", test.nextToken, next)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStartAndEndPage_CannotConvertPage(t *testing.T) {
|
|
|
|
wanted := "could not convert page token: strconv.Atoi: parsing"
|
2020-10-01 14:38:53 +00:00
|
|
|
_, _, _, err := pagination.StartAndEndPage("bad", 0, 0)
|
|
|
|
assert.ErrorContains(t, wanted, err)
|
2019-07-25 19:45:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStartAndEndPage_ExceedsMaxPage(t *testing.T) {
|
2019-07-26 17:07:20 +00:00
|
|
|
wanted := "page start 0 >= list 0"
|
2020-10-01 14:38:53 +00:00
|
|
|
_, _, _, err := pagination.StartAndEndPage("", 0, 0)
|
|
|
|
assert.ErrorContains(t, wanted, err)
|
2019-07-25 19:45:31 +00:00
|
|
|
}
|
2023-09-21 03:40:03 +00:00
|
|
|
|
|
|
|
func TestStartAndEndPage_InvalidPageValues(t *testing.T) {
|
|
|
|
_, _, _, err := pagination.StartAndEndPage("10", -1, 10)
|
|
|
|
assert.ErrorContains(t, "invalid page and total sizes provided", err)
|
|
|
|
|
|
|
|
_, _, _, err = pagination.StartAndEndPage("12", 10, -10)
|
|
|
|
assert.ErrorContains(t, "invalid page and total sizes provided", err)
|
|
|
|
|
|
|
|
_, _, _, err = pagination.StartAndEndPage("12", -50, -60)
|
|
|
|
assert.ErrorContains(t, "invalid page and total sizes provided", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStartAndEndPage_InvalidTokenValue(t *testing.T) {
|
|
|
|
_, _, _, err := pagination.StartAndEndPage("-12", 50, 60)
|
|
|
|
assert.ErrorContains(t, "invalid token value provided", err)
|
|
|
|
}
|