github.com/cosmos/cosmos-sdk@v0.50.10/client/utils_test.go (about)

     1  package client_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/cosmos/cosmos-sdk/client"
     9  )
    10  
    11  func TestPaginate(t *testing.T) {
    12  	testCases := []struct {
    13  		name                           string
    14  		numObjs, page, limit, defLimit int
    15  		expectedStart, expectedEnd     int
    16  	}{
    17  		{
    18  			"all objects in a single page",
    19  			100, 1, 100, 100,
    20  			0, 100,
    21  		},
    22  		{
    23  			"page one of three",
    24  			75, 1, 25, 100,
    25  			0, 25,
    26  		},
    27  		{
    28  			"page two of three",
    29  			75, 2, 25, 100,
    30  			25, 50,
    31  		},
    32  		{
    33  			"page three of three",
    34  			75, 3, 25, 100,
    35  			50, 75,
    36  		},
    37  		{
    38  			"end is greater than total number of objects",
    39  			75, 2, 50, 100,
    40  			50, 75,
    41  		},
    42  		{
    43  			"fallback to default limit",
    44  			75, 5, 0, 10,
    45  			40, 50,
    46  		},
    47  		{
    48  			"invalid start page",
    49  			75, 4, 25, 100,
    50  			-1, -1,
    51  		},
    52  		{
    53  			"invalid zero start page",
    54  			75, 0, 25, 100,
    55  			-1, -1,
    56  		},
    57  		{
    58  			"invalid negative start page",
    59  			75, -1, 25, 100,
    60  			-1, -1,
    61  		},
    62  		{
    63  			"invalid default limit",
    64  			75, 2, 0, -10,
    65  			-1, -1,
    66  		},
    67  	}
    68  
    69  	for i, tc := range testCases {
    70  		i, tc := i, tc
    71  		t.Run(tc.name, func(t *testing.T) {
    72  			start, end := client.Paginate(tc.numObjs, tc.page, tc.limit, tc.defLimit)
    73  			require.Equal(t, tc.expectedStart, start, "invalid result; test case #%d", i)
    74  			require.Equal(t, tc.expectedEnd, end, "invalid result; test case #%d", i)
    75  		})
    76  	}
    77  }