github.com/prysmaticlabs/prysm@v1.4.4/shared/pagination/pagination_test.go (about)

     1  package pagination_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/prysmaticlabs/prysm/shared/pagination"
     7  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
     8  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
     9  )
    10  
    11  func TestStartAndEndPage(t *testing.T) {
    12  	tests := []struct {
    13  		token     string
    14  		pageSize  int
    15  		totalSize int
    16  		nextToken string
    17  		start     int
    18  		end       int
    19  	}{
    20  		{
    21  			token:     "0",
    22  			pageSize:  9,
    23  			totalSize: 100,
    24  			nextToken: "1",
    25  			start:     0,
    26  			end:       9,
    27  		},
    28  		{
    29  			token:     "10",
    30  			pageSize:  4,
    31  			totalSize: 100,
    32  			nextToken: "11",
    33  			start:     40,
    34  			end:       44,
    35  		},
    36  		{
    37  			token:     "100",
    38  			pageSize:  5,
    39  			totalSize: 1000,
    40  			nextToken: "101",
    41  			start:     500,
    42  			end:       505,
    43  		},
    44  		{
    45  			token:     "3",
    46  			pageSize:  33,
    47  			totalSize: 100,
    48  			nextToken: "",
    49  			start:     99,
    50  			end:       100,
    51  		},
    52  		{
    53  			token:     "34",
    54  			pageSize:  500,
    55  			totalSize: 17500,
    56  			nextToken: "",
    57  			start:     17000,
    58  			end:       17500,
    59  		},
    60  	}
    61  
    62  	for _, test := range tests {
    63  		start, end, next, err := pagination.StartAndEndPage(test.token, test.pageSize, test.totalSize)
    64  		require.NoError(t, err)
    65  		if test.start != start {
    66  			t.Errorf("expected start and computed start are not equal %d, %d", test.start, start)
    67  		}
    68  		if test.end != end {
    69  			t.Errorf("expected end and computed end are not equal %d, %d", test.end, end)
    70  		}
    71  		if test.nextToken != next {
    72  			t.Errorf("expected next token and computed next token are not equal %v, %v", test.nextToken, next)
    73  		}
    74  	}
    75  }
    76  
    77  func TestStartAndEndPage_CannotConvertPage(t *testing.T) {
    78  	wanted := "could not convert page token: strconv.Atoi: parsing"
    79  	_, _, _, err := pagination.StartAndEndPage("bad", 0, 0)
    80  	assert.ErrorContains(t, wanted, err)
    81  }
    82  
    83  func TestStartAndEndPage_ExceedsMaxPage(t *testing.T) {
    84  	wanted := "page start 0 >= list 0"
    85  	_, _, _, err := pagination.StartAndEndPage("", 0, 0)
    86  	assert.ErrorContains(t, wanted, err)
    87  }