prysm-pulse/shared/pagination/pagination_test.go
terence tsao c1eeeef853 Implement ListValidatorAssignments RPC function (#3067)
* Need to sync latest ethereum API, will do it in master

* Added pagination wrapper

* Implemented ListValidatorAssignments

* Finished tests

* Fixed tests

* Raul's feedback

* Fmt

* Pagination test

* Removed extra loggings
2019-07-25 15:45:31 -04:00

81 lines
1.7 KiB
Go

package pagination
import (
"strings"
"testing"
)
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,
nextToken: "4",
start: 99,
end: 100,
},
}
for _, test := range tests {
start, end, next, err := StartAndEndPage(test.token, test.pageSize, test.totalSize)
if err != nil {
t.Fatal(err)
}
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"
if _, _, _, err := StartAndEndPage("bad", 0, 0); !strings.Contains(err.Error(), wanted) {
t.Fatalf("wanted error: %v, got error: %v", wanted, err.Error())
}
}
func TestStartAndEndPage_ExceedsMaxPage(t *testing.T) {
wanted := "page start 0 >= validator list 0"
if _, _, _, err := StartAndEndPage("", 0, 0); !strings.Contains(err.Error(), wanted) {
t.Fatalf("wanted error: %v, got error: %v", wanted, err.Error())
}
}