Skip to content

Commit

Permalink
[Bank refactor] fix pagination tests (#8550)
Browse files Browse the repository at this point in the history
* fix tests

* lint
  • Loading branch information
sahith-narahari authored Feb 10, 2021
1 parent 5dc1e88 commit cc0df5c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
8 changes: 5 additions & 3 deletions types/query/filtered_pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)

Expand Down Expand Up @@ -101,6 +100,8 @@ func ExampleFilteredPaginate() {
denom := fmt.Sprintf("test%ddenom", i)
balances = append(balances, sdk.NewInt64Coin(denom, 250))
}

balances = balances.Sort()
addr1 := sdk.AccAddress([]byte("addr1"))
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
app.AccountKeeper.SetAccount(ctx, acc1)
Expand All @@ -110,7 +111,7 @@ func ExampleFilteredPaginate() {
}

pageReq := &query.PageRequest{Key: nil, Limit: 1, CountTotal: true}
store := ctx.KVStore(app.GetKey(authtypes.StoreKey))
store := ctx.KVStore(app.GetKey(types.StoreKey))
balancesStore := prefix.NewStore(store, types.BalancesPrefix)
accountStore := prefix.NewStore(balancesStore, addr1.Bytes())

Expand Down Expand Up @@ -144,9 +145,10 @@ func ExampleFilteredPaginate() {

func execFilterPaginate(store sdk.KVStore, pageReq *query.PageRequest, appCodec codec.Marshaler) (balances sdk.Coins, res *query.PageResponse, err error) {
balancesStore := prefix.NewStore(store, types.BalancesPrefix)
accountStore := prefix.NewStore(balancesStore, addr1.Bytes())

var balResult sdk.Coins
res, err = query.FilteredPaginate(balancesStore, pageReq, func(key []byte, value []byte, accumulate bool) (bool, error) {
res, err = query.FilteredPaginate(accountStore, pageReq, func(key []byte, value []byte, accumulate bool) (bool, error) {
var bal sdk.Coin
err := appCodec.UnmarshalBinaryBare(value, &bal)
if err != nil {
Expand Down
21 changes: 16 additions & 5 deletions types/query/pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)

Expand Down Expand Up @@ -74,12 +76,11 @@ func (s *paginationTestSuite) TestPagination() {
}

balances = balances.Sort()

addr1 := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
app.AccountKeeper.SetAccount(ctx, acc1)
// s.Require().NoError(app.BankKeeper.SetBalances(ctx, addr1, balances))
s.Require().NoError(simapp.FundAccount(app, ctx, addr1, balances))

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)
Expand Down Expand Up @@ -180,18 +181,19 @@ func ExamplePaginate() {
balances = append(balances, sdk.NewInt64Coin(denom, 100))
}

balances = balances.Sort()
addr1 := sdk.AccAddress([]byte("addr1"))
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
app.AccountKeeper.SetAccount(ctx, acc1)
err := app.BankKeeper.SetBalances(ctx, addr1, balances)
if err != nil {
err := simapp.FundAccount(app, ctx, addr1, balances)
if err != nil { // should return no error
fmt.Println(err)
}
// Paginate example
pageReq := &query.PageRequest{Key: nil, Limit: 1, CountTotal: true}
request := types.NewQueryAllBalancesRequest(addr1, pageReq)
balResult := sdk.NewCoins()
authStore := ctx.KVStore(app.GetKey(authtypes.StoreKey))
authStore := ctx.KVStore(app.GetKey(types.StoreKey))
balancesStore := prefix.NewStore(authStore, types.BalancesPrefix)
accountStore := prefix.NewStore(balancesStore, addr1.Bytes())
pageRes, err := query.Paginate(accountStore, request.Pagination, func(key []byte, value []byte) error {
Expand Down Expand Up @@ -228,5 +230,14 @@ func setupTest() (*simapp.SimApp, sdk.Context, codec.Marshaler) {
maccPerms[multiPerm] = []string{authtypes.Burner, authtypes.Minter, authtypes.Staking}
maccPerms[randomPerm] = []string{"random"}

app.AccountKeeper = authkeeper.NewAccountKeeper(
appCodec, app.GetKey(authtypes.StoreKey), app.GetSubspace(authtypes.ModuleName),
authtypes.ProtoBaseAccount, maccPerms,
)
app.BankKeeper = bankkeeper.NewBaseKeeper(
appCodec, app.GetKey(types.StoreKey), app.AccountKeeper,
app.GetSubspace(types.ModuleName), make(map[string]bool),
)

return app, ctx, appCodec
}

0 comments on commit cc0df5c

Please sign in to comment.