github.com/adoriasoft/tendermint@v0.34.0-dev1.0.20200722151356-96d84601a75a/rpc/core/env_test.go (about)

     1  package core
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestPaginationPage(t *testing.T) {
    11  	cases := []struct {
    12  		totalCount int
    13  		perPage    int
    14  		page       int
    15  		newPage    int
    16  		expErr     bool
    17  	}{
    18  		{0, 10, 1, 1, false},
    19  
    20  		{0, 10, 0, 1, false},
    21  		{0, 10, 1, 1, false},
    22  		{0, 10, 2, 0, true},
    23  
    24  		{5, 10, -1, 0, true},
    25  		{5, 10, 0, 1, false},
    26  		{5, 10, 1, 1, false},
    27  		{5, 10, 2, 0, true},
    28  		{5, 10, 2, 0, true},
    29  
    30  		{5, 5, 1, 1, false},
    31  		{5, 5, 2, 0, true},
    32  		{5, 5, 3, 0, true},
    33  
    34  		{5, 3, 2, 2, false},
    35  		{5, 3, 3, 0, true},
    36  
    37  		{5, 2, 2, 2, false},
    38  		{5, 2, 3, 3, false},
    39  		{5, 2, 4, 0, true},
    40  	}
    41  
    42  	for _, c := range cases {
    43  		p, err := validatePage(&c.page, c.perPage, c.totalCount)
    44  		if c.expErr {
    45  			assert.Error(t, err)
    46  			continue
    47  		}
    48  
    49  		assert.Equal(t, c.newPage, p, fmt.Sprintf("%v", c))
    50  	}
    51  
    52  	// nil case
    53  	p, err := validatePage(nil, 1, 1)
    54  	if assert.NoError(t, err) {
    55  		assert.Equal(t, 1, p)
    56  	}
    57  }
    58  
    59  func TestPaginationPerPage(t *testing.T) {
    60  	cases := []struct {
    61  		totalCount int
    62  		perPage    int
    63  		newPerPage int
    64  	}{
    65  		{5, 0, defaultPerPage},
    66  		{5, 1, 1},
    67  		{5, 2, 2},
    68  		{5, defaultPerPage, defaultPerPage},
    69  		{5, maxPerPage - 1, maxPerPage - 1},
    70  		{5, maxPerPage, maxPerPage},
    71  		{5, maxPerPage + 1, maxPerPage},
    72  	}
    73  
    74  	for _, c := range cases {
    75  		p := validatePerPage(&c.perPage)
    76  		assert.Equal(t, c.newPerPage, p, fmt.Sprintf("%v", c))
    77  	}
    78  
    79  	// nil case
    80  	p := validatePerPage(nil)
    81  	assert.Equal(t, defaultPerPage, p)
    82  }