cosmos-sdk/x/bank/keeper/querier_test.go

164 lines
5.4 KiB
Go
Raw Normal View History

2019-08-19 06:29:17 -07:00
package keeper_test
import (
"fmt"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/bank/testutil"
"github.com/cosmos/cosmos-sdk/x/bank/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
)
2020-01-30 13:31:16 -08:00
func (suite *IntegrationTestSuite) TestQuerier_QueryBalance() {
app, ctx := suite.app, suite.ctx
legacyAmino := app.LegacyAmino()
_, _, addr := testdata.KeyTestPubAddr()
req := abci.RequestQuery{
2020-01-30 13:31:16 -08:00
Path: fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryBalance),
Data: []byte{},
}
querier := keeper.NewQuerier(app.BankKeeper, legacyAmino)
2020-01-30 13:31:16 -08:00
res, err := querier(ctx, []string{types.QueryBalance}, req)
suite.Require().NotNil(err)
suite.Require().Nil(res)
req.Data = legacyAmino.MustMarshalJSON(types.NewQueryBalanceRequest(addr, fooDenom))
2020-01-30 13:31:16 -08:00
res, err = querier(ctx, []string{types.QueryBalance}, req)
suite.Require().NoError(err)
suite.Require().NotNil(res)
var balance sdk.Coin
suite.Require().NoError(legacyAmino.UnmarshalJSON(res, &balance))
2020-01-30 13:31:16 -08:00
suite.True(balance.IsZero())
origCoins := sdk.NewCoins(newFooCoin(50), newBarCoin(30))
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr)
app.AccountKeeper.SetAccount(ctx, acc)
suite.Require().NoError(testutil.FundAccount(app.BankKeeper, ctx, acc.GetAddress(), origCoins))
2020-01-30 13:31:16 -08:00
res, err = querier(ctx, []string{types.QueryBalance}, req)
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().NoError(legacyAmino.UnmarshalJSON(res, &balance))
2020-01-30 13:31:16 -08:00
suite.True(balance.IsEqual(newFooCoin(50)))
}
2020-01-30 13:31:16 -08:00
func (suite *IntegrationTestSuite) TestQuerier_QueryAllBalances() {
app, ctx := suite.app, suite.ctx
legacyAmino := app.LegacyAmino()
_, _, addr := testdata.KeyTestPubAddr()
2020-01-30 13:31:16 -08:00
req := abci.RequestQuery{
Path: fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryAllBalances),
Data: []byte{},
}
querier := keeper.NewQuerier(app.BankKeeper, legacyAmino)
2020-01-30 13:31:16 -08:00
res, err := querier(ctx, []string{types.QueryAllBalances}, req)
suite.Require().NotNil(err)
suite.Require().Nil(res)
req.Data = legacyAmino.MustMarshalJSON(types.NewQueryAllBalancesRequest(addr, nil))
2020-01-30 13:31:16 -08:00
res, err = querier(ctx, []string{types.QueryAllBalances}, req)
suite.Require().NoError(err)
suite.Require().NotNil(res)
var balances sdk.Coins
suite.Require().NoError(legacyAmino.UnmarshalJSON(res, &balances))
2020-01-30 13:31:16 -08:00
suite.True(balances.IsZero())
origCoins := sdk.NewCoins(newFooCoin(50), newBarCoin(30))
2019-08-19 06:29:17 -07:00
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr)
2020-01-30 13:31:16 -08:00
2019-08-19 06:29:17 -07:00
app.AccountKeeper.SetAccount(ctx, acc)
suite.Require().NoError(testutil.FundAccount(app.BankKeeper, ctx, acc.GetAddress(), origCoins))
2020-01-30 13:31:16 -08:00
res, err = querier(ctx, []string{types.QueryAllBalances}, req)
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().NoError(legacyAmino.UnmarshalJSON(res, &balances))
2020-01-30 13:31:16 -08:00
suite.True(balances.IsEqual(origCoins))
}
func (suite *IntegrationTestSuite) TestQuerier_QueryTotalSupply() {
app, ctx := suite.app, suite.ctx
legacyAmino := app.LegacyAmino()
expectedTotalSupply := sdk.NewCoins(sdk.NewInt64Coin("test", 400000000))
[Bank] Remove the unsafe balance changing API (#8473) * temp commit * setbalance now is internal * remove set balances in genesis * feedback test commit * update tests * fix: genesis panic message * fix not bonded pool * fix(staking): genesis test * fix(simapp): rollback state fix change * fix(staking): genesis large val set test * [Bank Refactor] Frojdi jonathan/remove setsupply (#8491) * init supply in a different way * remove external usage of set supply * change(staking): replace SetSupply with MintCoins in tests * change(evidence): replace SetSupply with MintCoins in tests * change(crisis): remove SetSupply in tests * change(bank): remove set supply from genesis tests * change(bank): remove set supply from keeper tests * change(bank): remove remaining set supply usage from keeper tests * change(bank): remove set supply usage from grpc query and querier tests * change(bank): remove SetSupply from keeper interface Co-authored-by: Frojdi Dymylja <frojdi.dymylja@gmail.com> * remove setbalances from genesis in gov * remove keyring * add init genesis state * change(staking): make genesis checks coherent and add tests * remove setbalances on distribution * fix(staking): genesis tests * [Bank Refactor]: Remove SetBalances usage from the code and tests (#8509) * change(distribution): remove SetBalances usage from keeper tests * add(simapp): FundAccount utility function * chore(staking): use FundAccount in keeper tests * change(staking): remove usage of SetBalance in allocation tests * change(staking): remove usage of SetBalance in delegation tests * change(staking): remove usage of SetBalance in proposal handler tests * change(staking): remove usage of SetBalances in grpc query tests * change(staking): remove usage of SetBalances in operations tests * change(distribution): remove usage of SetBalances in genesis * change(authz): remove usage of SetBalances keeper and operations test * fix(authz): TestKeeperFees failing test * change(slashing): remove SetBalances from expected BankKeeper * change(slashing): remove usage of SetBalances in tests * change(distribution): remove SetBalances from expected BankKeeper * change(genutil): remove usage of SetBalances from tests * change(gov): remove SetBalances from expected BankKeeper * change(gov): remove usage of SetBalances from tests * change(staking): remove usage of SetBalances from slash tests * change(staking): remove SetBalances from expected BankKeeper * change(staking): remove usage of SetBalances from delegation tests * change(staking): remove usage of SetBalances from operations tests * change(staking): remove usage of SetBalances from validator tests * change(bank): remove usage of SetBalances from app tests * change(bank): remove usage of SetBalances from bench tests * change(bank): remove usage of SetBalances from querier tests * change(bank): remove usage of SetBalances from grpc query tests * change(bank): remove usage of SetBalances from operations tests * change(bank): partially remove usage of SetBalances from keeper tests * change(bank): finalize removal of usage of SetBalances from keeper tests * change(auth): remove usage of SetBalances from verify tests * change(auth): partially remove usage of SetBalances from tests * [Bank refactor]: finalize removal of setbalances from auth (#8527) * add tests with is check tx * temp commit * fix test * fix other test and remove setbalances * change(auth): remove usage of SetBalances is vesting tests Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com> * change(types): remove usage of SetBalances in queries * fix(types): pagination tests * [Bank refactor] fix pagination tests (#8550) * fix tests * lint * change(bank): remove SetBalances from keeper public API Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com> Co-authored-by: SaReN <sahithnarahari@gmail.com> * change(bank): remove SubtractCoins from keeper public API * change(ibc/transfer): remove AddCoins from relay tests * change(bank): remove AddCoins from public keeper API * fix imports * remove set balances * fix fee test * remove set balances * fix(staking): remove dependency on minter authorization for staking pools * chore: update CHANGELOG.md * update: x/distribution/keeper/keeper_test.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update simapp/test_helpers.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update x/staking/genesis_test.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * fix(simapp): FundAccount amount variable name * fix some PR issues Co-authored-by: Frojdi Dymylja <frojdi.dymylja@gmail.com> Co-authored-by: Frojdi Dymylja <33157909+fdymylja@users.noreply.github.com> Co-authored-by: SaReN <sahithnarahari@gmail.com> Co-authored-by: Alessio Treglia <alessio@tendermint.com> Co-authored-by: Robert Zaremba <robert@zaremba.ch>
2021-02-17 10:20:33 -08:00
suite.
Require().
NoError(app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, expectedTotalSupply))
req := abci.RequestQuery{
Path: fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryTotalSupply),
Data: []byte{},
}
querier := keeper.NewQuerier(app.BankKeeper, legacyAmino)
res, err := querier(ctx, []string{types.QueryTotalSupply}, req)
suite.Require().NotNil(err)
suite.Require().Nil(res)
req.Data = legacyAmino.MustMarshalJSON(types.QueryTotalSupplyRequest{Pagination: &query.PageRequest{
Limit: 100,
}})
res, err = querier(ctx, []string{types.QueryTotalSupply}, req)
suite.Require().NoError(err)
suite.Require().NotNil(res)
var resp types.QueryTotalSupplyResponse
suite.Require().NoError(legacyAmino.UnmarshalJSON(res, &resp))
suite.Require().Equal(expectedTotalSupply, resp.Supply)
}
func (suite *IntegrationTestSuite) TestQuerier_QueryTotalSupplyOf() {
app, ctx := suite.app, suite.ctx
legacyAmino := app.LegacyAmino()
test1Supply := sdk.NewInt64Coin("test1", 4000000)
test2Supply := sdk.NewInt64Coin("test2", 700000000)
expectedTotalSupply := sdk.NewCoins(test1Supply, test2Supply)
[Bank] Remove the unsafe balance changing API (#8473) * temp commit * setbalance now is internal * remove set balances in genesis * feedback test commit * update tests * fix: genesis panic message * fix not bonded pool * fix(staking): genesis test * fix(simapp): rollback state fix change * fix(staking): genesis large val set test * [Bank Refactor] Frojdi jonathan/remove setsupply (#8491) * init supply in a different way * remove external usage of set supply * change(staking): replace SetSupply with MintCoins in tests * change(evidence): replace SetSupply with MintCoins in tests * change(crisis): remove SetSupply in tests * change(bank): remove set supply from genesis tests * change(bank): remove set supply from keeper tests * change(bank): remove remaining set supply usage from keeper tests * change(bank): remove set supply usage from grpc query and querier tests * change(bank): remove SetSupply from keeper interface Co-authored-by: Frojdi Dymylja <frojdi.dymylja@gmail.com> * remove setbalances from genesis in gov * remove keyring * add init genesis state * change(staking): make genesis checks coherent and add tests * remove setbalances on distribution * fix(staking): genesis tests * [Bank Refactor]: Remove SetBalances usage from the code and tests (#8509) * change(distribution): remove SetBalances usage from keeper tests * add(simapp): FundAccount utility function * chore(staking): use FundAccount in keeper tests * change(staking): remove usage of SetBalance in allocation tests * change(staking): remove usage of SetBalance in delegation tests * change(staking): remove usage of SetBalance in proposal handler tests * change(staking): remove usage of SetBalances in grpc query tests * change(staking): remove usage of SetBalances in operations tests * change(distribution): remove usage of SetBalances in genesis * change(authz): remove usage of SetBalances keeper and operations test * fix(authz): TestKeeperFees failing test * change(slashing): remove SetBalances from expected BankKeeper * change(slashing): remove usage of SetBalances in tests * change(distribution): remove SetBalances from expected BankKeeper * change(genutil): remove usage of SetBalances from tests * change(gov): remove SetBalances from expected BankKeeper * change(gov): remove usage of SetBalances from tests * change(staking): remove usage of SetBalances from slash tests * change(staking): remove SetBalances from expected BankKeeper * change(staking): remove usage of SetBalances from delegation tests * change(staking): remove usage of SetBalances from operations tests * change(staking): remove usage of SetBalances from validator tests * change(bank): remove usage of SetBalances from app tests * change(bank): remove usage of SetBalances from bench tests * change(bank): remove usage of SetBalances from querier tests * change(bank): remove usage of SetBalances from grpc query tests * change(bank): remove usage of SetBalances from operations tests * change(bank): partially remove usage of SetBalances from keeper tests * change(bank): finalize removal of usage of SetBalances from keeper tests * change(auth): remove usage of SetBalances from verify tests * change(auth): partially remove usage of SetBalances from tests * [Bank refactor]: finalize removal of setbalances from auth (#8527) * add tests with is check tx * temp commit * fix test * fix other test and remove setbalances * change(auth): remove usage of SetBalances is vesting tests Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com> * change(types): remove usage of SetBalances in queries * fix(types): pagination tests * [Bank refactor] fix pagination tests (#8550) * fix tests * lint * change(bank): remove SetBalances from keeper public API Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com> Co-authored-by: SaReN <sahithnarahari@gmail.com> * change(bank): remove SubtractCoins from keeper public API * change(ibc/transfer): remove AddCoins from relay tests * change(bank): remove AddCoins from public keeper API * fix imports * remove set balances * fix fee test * remove set balances * fix(staking): remove dependency on minter authorization for staking pools * chore: update CHANGELOG.md * update: x/distribution/keeper/keeper_test.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update simapp/test_helpers.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update x/staking/genesis_test.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * fix(simapp): FundAccount amount variable name * fix some PR issues Co-authored-by: Frojdi Dymylja <frojdi.dymylja@gmail.com> Co-authored-by: Frojdi Dymylja <33157909+fdymylja@users.noreply.github.com> Co-authored-by: SaReN <sahithnarahari@gmail.com> Co-authored-by: Alessio Treglia <alessio@tendermint.com> Co-authored-by: Robert Zaremba <robert@zaremba.ch>
2021-02-17 10:20:33 -08:00
suite.
Require().
NoError(app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, expectedTotalSupply))
req := abci.RequestQuery{
Path: fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QuerySupplyOf),
Data: []byte{},
}
querier := keeper.NewQuerier(app.BankKeeper, legacyAmino)
res, err := querier(ctx, []string{types.QuerySupplyOf}, req)
suite.Require().NotNil(err)
suite.Require().Nil(res)
req.Data = legacyAmino.MustMarshalJSON(types.NewQuerySupplyOfParams(test1Supply.Denom))
res, err = querier(ctx, []string{types.QuerySupplyOf}, req)
suite.Require().NoError(err)
suite.Require().NotNil(res)
var resp sdk.Coin
suite.Require().NoError(legacyAmino.UnmarshalJSON(res, &resp))
suite.Require().Equal(test1Supply, resp)
}
2020-01-30 13:31:16 -08:00
func (suite *IntegrationTestSuite) TestQuerierRouteNotFound() {
app, ctx := suite.app, suite.ctx
legacyAmino := app.LegacyAmino()
req := abci.RequestQuery{
2020-01-30 13:31:16 -08:00
Path: fmt.Sprintf("custom/%s/invalid", types.ModuleName),
Data: []byte{},
}
querier := keeper.NewQuerier(app.BankKeeper, legacyAmino)
2020-01-30 13:31:16 -08:00
_, err := querier(ctx, []string{"invalid"}, req)
suite.Error(err)
}