Skip to content

Commit

Permalink
types: tests -> test suites migration (#7494)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessio Treglia authored Oct 9, 2020
1 parent 278eaa7 commit f8e3fcb
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 90 deletions.
73 changes: 35 additions & 38 deletions types/query/filtered_pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package query_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
Expand All @@ -16,7 +13,7 @@ import (

var addr1 = sdk.AccAddress([]byte("addr1"))

func TestFilteredPaginations(t *testing.T) {
func (s *paginationTestSuite) TestFilteredPaginations() {
app, ctx, appCodec := setupTest()

var balances sdk.Coins
Expand All @@ -33,61 +30,61 @@ func TestFilteredPaginations(t *testing.T) {
addr1 := sdk.AccAddress([]byte("addr1"))
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
app.AccountKeeper.SetAccount(ctx, acc1)
require.NoError(t, app.BankKeeper.SetBalances(ctx, addr1, balances))
s.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, balances))
store := ctx.KVStore(app.GetKey(authtypes.StoreKey))

// verify pagination with limit > total values
pageReq := &query.PageRequest{Key: nil, Limit: 5, CountTotal: true}
balances, res, err := execFilterPaginate(store, pageReq, appCodec)
require.NoError(t, err)
require.NotNil(t, res)
require.Equal(t, 4, len(balances))
s.Require().NoError(err)
s.Require().NotNil(res)
s.Require().Equal(4, len(balances))

t.Log("verify empty request")
s.T().Log("verify empty request")
balances, res, err = execFilterPaginate(store, nil, appCodec)
require.NoError(t, err)
require.NotNil(t, res)
require.Equal(t, 4, len(balances))
require.Equal(t, uint64(4), res.Total)
require.Nil(t, res.NextKey)
s.Require().NoError(err)
s.Require().NotNil(res)
s.Require().Equal(4, len(balances))
s.Require().Equal(uint64(4), res.Total)
s.Require().Nil(res.NextKey)

t.Log("verify nextKey is returned if there are more results")
s.T().Log("verify nextKey is returned if there are more results")
pageReq = &query.PageRequest{Key: nil, Limit: 2, CountTotal: true}
balances, res, err = execFilterPaginate(store, pageReq, appCodec)
require.NoError(t, err)
require.NotNil(t, res)
require.Equal(t, 2, len(balances))
require.NotNil(t, res.NextKey)
require.Equal(t, string(res.NextKey), fmt.Sprintf("test2denom"))
require.Equal(t, uint64(4), res.Total)

t.Log("verify both key and offset can't be given")
s.Require().NoError(err)
s.Require().NotNil(res)
s.Require().Equal(2, len(balances))
s.Require().NotNil(res.NextKey)
s.Require().Equal(string(res.NextKey), fmt.Sprintf("test2denom"))
s.Require().Equal(uint64(4), res.Total)

s.T().Log("verify both key and offset can't be given")
pageReq = &query.PageRequest{Key: res.NextKey, Limit: 1, Offset: 2, CountTotal: true}
_, _, err = execFilterPaginate(store, pageReq, appCodec)
require.Error(t, err)
s.Require().Error(err)

t.Log("use nextKey for query")
s.T().Log("use nextKey for query")
pageReq = &query.PageRequest{Key: res.NextKey, Limit: 2, CountTotal: true}
balances, res, err = execFilterPaginate(store, pageReq, appCodec)
require.NoError(t, err)
require.NotNil(t, res)
require.Equal(t, 2, len(balances))
require.Nil(t, res.NextKey)
s.Require().NoError(err)
s.Require().NotNil(res)
s.Require().Equal(2, len(balances))
s.Require().Nil(res.NextKey)

t.Log("verify default limit")
s.T().Log("verify default limit")
pageReq = &query.PageRequest{Key: nil, Limit: 0}
balances, res, err = execFilterPaginate(store, pageReq, appCodec)
require.NoError(t, err)
require.NotNil(t, res)
require.Equal(t, 4, len(balances))
require.Equal(t, uint64(4), res.Total)
s.Require().NoError(err)
s.Require().NotNil(res)
s.Require().Equal(4, len(balances))
s.Require().Equal(uint64(4), res.Total)

t.Log("verify with offset")
s.T().Log("verify with offset")
pageReq = &query.PageRequest{Offset: 2, Limit: 2}
balances, res, err = execFilterPaginate(store, pageReq, appCodec)
require.NoError(t, err)
require.NotNil(t, res)
require.LessOrEqual(t, len(balances), 2)
s.Require().NoError(err)
s.Require().NotNil(res)
s.Require().LessOrEqual(len(balances), 2)
}

func ExampleFilteredPaginate() {
Expand Down
113 changes: 61 additions & 52 deletions types/query/pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"

Expand Down Expand Up @@ -34,7 +35,15 @@ const (
lastPageRecords = 35
)

func TestPagination(t *testing.T) {
type paginationTestSuite struct {
suite.Suite
}

func TestPaginationTestSuite(t *testing.T) {
suite.Run(t, new(paginationTestSuite))
}

func (s *paginationTestSuite) TestPagination() {
app, ctx, _ := setupTest()
queryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, app.BankKeeper)
Expand All @@ -50,96 +59,96 @@ func TestPagination(t *testing.T) {
addr1 := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
app.AccountKeeper.SetAccount(ctx, acc1)
require.NoError(t, app.BankKeeper.SetBalances(ctx, addr1, balances))
s.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, balances))

t.Log("verify empty page request results a max of defaultLimit records and counts total records")
s.T().Log("verify empty page request results a max of defaultLimit records and counts total records")
pageReq := &query.PageRequest{}
request := types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err := queryClient.AllBalances(gocontext.Background(), request)
require.NoError(t, err)
require.Equal(t, res.Pagination.Total, uint64(numBalances))
require.NotNil(t, res.Pagination.NextKey)
require.LessOrEqual(t, res.Balances.Len(), defaultLimit)
s.Require().NoError(err)
s.Require().Equal(res.Pagination.Total, uint64(numBalances))
s.Require().NotNil(res.Pagination.NextKey)
s.Require().LessOrEqual(res.Balances.Len(), defaultLimit)

t.Log("verify page request with limit > defaultLimit, returns less or equal to `limit` records")
s.T().Log("verify page request with limit > defaultLimit, returns less or equal to `limit` records")
pageReq = &query.PageRequest{Limit: overLimit}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
require.NoError(t, err)
require.Equal(t, res.Pagination.Total, uint64(0))
require.NotNil(t, res.Pagination.NextKey)
require.LessOrEqual(t, res.Balances.Len(), overLimit)
s.Require().NoError(err)
s.Require().Equal(res.Pagination.Total, uint64(0))
s.Require().NotNil(res.Pagination.NextKey)
s.Require().LessOrEqual(res.Balances.Len(), overLimit)

t.Log("verify paginate with custom limit and countTotal true")
s.T().Log("verify paginate with custom limit and countTotal true")
pageReq = &query.PageRequest{Limit: underLimit, CountTotal: true}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
require.NoError(t, err)
require.Equal(t, res.Balances.Len(), underLimit)
require.NotNil(t, res.Pagination.NextKey)
require.Equal(t, res.Pagination.Total, uint64(numBalances))
s.Require().NoError(err)
s.Require().Equal(res.Balances.Len(), underLimit)
s.Require().NotNil(res.Pagination.NextKey)
s.Require().Equal(res.Pagination.Total, uint64(numBalances))

t.Log("verify paginate with custom limit and countTotal false")
s.T().Log("verify paginate with custom limit and countTotal false")
pageReq = &query.PageRequest{Limit: defaultLimit, CountTotal: false}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
require.NoError(t, err)
require.Equal(t, res.Balances.Len(), defaultLimit)
require.NotNil(t, res.Pagination.NextKey)
require.Equal(t, res.Pagination.Total, uint64(0))
s.Require().NoError(err)
s.Require().Equal(res.Balances.Len(), defaultLimit)
s.Require().NotNil(res.Pagination.NextKey)
s.Require().Equal(res.Pagination.Total, uint64(0))

t.Log("verify paginate with custom limit, key and countTotal false")
s.T().Log("verify paginate with custom limit, key and countTotal false")
pageReq = &query.PageRequest{Key: res.Pagination.NextKey, Limit: defaultLimit, CountTotal: false}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
require.NoError(t, err)
require.Equal(t, res.Balances.Len(), defaultLimit)
require.NotNil(t, res.Pagination.NextKey)
require.Equal(t, res.Pagination.Total, uint64(0))
s.Require().NoError(err)
s.Require().Equal(res.Balances.Len(), defaultLimit)
s.Require().NotNil(res.Pagination.NextKey)
s.Require().Equal(res.Pagination.Total, uint64(0))

t.Log("verify paginate for last page, results in records less than max limit")
s.T().Log("verify paginate for last page, results in records less than max limit")
pageReq = &query.PageRequest{Key: res.Pagination.NextKey, Limit: defaultLimit, CountTotal: false}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
require.NoError(t, err)
require.LessOrEqual(t, res.Balances.Len(), defaultLimit)
require.Equal(t, res.Balances.Len(), lastPageRecords)
require.Nil(t, res.Pagination.NextKey)
require.Equal(t, res.Pagination.Total, uint64(0))
s.Require().NoError(err)
s.Require().LessOrEqual(res.Balances.Len(), defaultLimit)
s.Require().Equal(res.Balances.Len(), lastPageRecords)
s.Require().Nil(res.Pagination.NextKey)
s.Require().Equal(res.Pagination.Total, uint64(0))

t.Log("verify paginate with offset and limit")
s.T().Log("verify paginate with offset and limit")
pageReq = &query.PageRequest{Offset: 200, Limit: defaultLimit, CountTotal: false}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
require.NoError(t, err)
require.LessOrEqual(t, res.Balances.Len(), defaultLimit)
require.Equal(t, res.Balances.Len(), lastPageRecords)
require.Nil(t, res.Pagination.NextKey)
require.Equal(t, res.Pagination.Total, uint64(0))
s.Require().NoError(err)
s.Require().LessOrEqual(res.Balances.Len(), defaultLimit)
s.Require().Equal(res.Balances.Len(), lastPageRecords)
s.Require().Nil(res.Pagination.NextKey)
s.Require().Equal(res.Pagination.Total, uint64(0))

t.Log("verify paginate with offset and limit")
s.T().Log("verify paginate with offset and limit")
pageReq = &query.PageRequest{Offset: 100, Limit: defaultLimit, CountTotal: false}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
require.NoError(t, err)
require.LessOrEqual(t, res.Balances.Len(), defaultLimit)
require.NotNil(t, res.Pagination.NextKey)
require.Equal(t, res.Pagination.Total, uint64(0))
s.Require().NoError(err)
s.Require().LessOrEqual(res.Balances.Len(), defaultLimit)
s.Require().NotNil(res.Pagination.NextKey)
s.Require().Equal(res.Pagination.Total, uint64(0))

t.Log("verify paginate with offset and key - error")
s.T().Log("verify paginate with offset and key - error")
pageReq = &query.PageRequest{Key: res.Pagination.NextKey, Offset: 100, Limit: defaultLimit, CountTotal: false}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
require.Error(t, err)
require.Equal(t, err.Error(), "invalid request, either offset or key is expected, got both")
s.Require().Error(err)
s.Require().Equal(err.Error(), "invalid request, either offset or key is expected, got both")

t.Log("verify paginate with offset greater than total results")
s.T().Log("verify paginate with offset greater than total results")
pageReq = &query.PageRequest{Offset: 300, Limit: defaultLimit, CountTotal: false}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
require.NoError(t, err)
require.LessOrEqual(t, res.Balances.Len(), 0)
require.Nil(t, res.Pagination.NextKey)
s.Require().NoError(err)
s.Require().LessOrEqual(res.Balances.Len(), 0)
s.Require().Nil(res.Pagination.NextKey)
}

func ExamplePaginate() {
Expand Down

0 comments on commit f8e3fcb

Please sign in to comment.