test(x/bank): write integration tests (#16052)

This commit is contained in:
Likhita Polavarapu
2023-05-10 11:27:01 +00:00
committed by GitHub
parent bb2d859e3c
commit 52212cf3e9
3 changed files with 71 additions and 1883 deletions
@@ -7,22 +7,25 @@ import (
"gotest.tools/v3/assert"
"pgregory.net/rapid"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"github.com/cosmos/cosmos-sdk/baseapp"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/testutil/configurator"
simstestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil/integration"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/auth"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authsims "github.com/cosmos/cosmos-sdk/x/auth/simulation"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktestutil "github.com/cosmos/cosmos-sdk/x/bank/testutil"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
_ "github.com/cosmos/cosmos-sdk/x/auth"
_ "github.com/cosmos/cosmos-sdk/x/auth/tx/config"
_ "github.com/cosmos/cosmos-sdk/x/bank"
_ "github.com/cosmos/cosmos-sdk/x/consensus"
_ "github.com/cosmos/cosmos-sdk/x/params"
_ "github.com/cosmos/cosmos-sdk/x/staking"
@@ -52,42 +55,68 @@ var (
)
type deterministicFixture struct {
ctx sdk.Context
bankKeeper keeper.BaseKeeper
ctx sdk.Context
bankKeeper keeper.BaseKeeper
queryClient banktypes.QueryClient
}
func initDeterministicFixture(t *testing.T) *deterministicFixture {
f := &deterministicFixture{}
keys := storetypes.NewKVStoreKeys(authtypes.StoreKey, banktypes.StoreKey)
cdc := moduletestutil.MakeTestEncodingConfig(auth.AppModuleBasic{}, bank.AppModuleBasic{}).Codec
var interfaceRegistry codectypes.InterfaceRegistry
logger := log.NewTestLogger(t)
cms := integration.CreateMultiStore(keys, logger)
app, err := simstestutil.Setup(
depinject.Configs(
configurator.NewAppConfig(
configurator.AuthModule(),
configurator.TxModule(),
configurator.ParamsModule(),
configurator.ConsensusModule(),
configurator.BankModule(),
configurator.StakingModule(),
),
depinject.Supply(log.NewNopLogger()),
),
&f.bankKeeper,
&interfaceRegistry,
newCtx := sdk.NewContext(cms, cmtproto.Header{}, true, logger)
authority := authtypes.NewModuleAddress("gov")
maccPerms := map[string][]string{
minttypes.ModuleName: {authtypes.Minter},
}
accountKeeper := authkeeper.NewAccountKeeper(
cdc,
runtime.NewKVStoreService(keys[authtypes.StoreKey]),
authtypes.ProtoBaseAccount,
maccPerms,
sdk.Bech32MainPrefix,
authority.String(),
)
assert.NilError(t, err)
ctx := app.BaseApp.NewContext(false, cmtproto.Header{})
f.ctx = ctx
blockedAddresses := map[string]bool{
accountKeeper.GetAuthority(): false,
}
bankKeeper := keeper.NewBaseKeeper(
cdc,
runtime.NewKVStoreService(keys[banktypes.StoreKey]),
accountKeeper,
blockedAddresses,
authority.String(),
log.NewNopLogger(),
)
queryHelper := baseapp.NewQueryServerTestHelper(ctx, interfaceRegistry)
banktypes.RegisterQueryServer(queryHelper, f.bankKeeper)
f.queryClient = banktypes.NewQueryClient(queryHelper)
authModule := auth.NewAppModule(cdc, accountKeeper, authsims.RandomGenesisAccounts, nil)
bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper, nil)
return f
integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc, authModule, bankModule)
sdkCtx := sdk.UnwrapSDKContext(integrationApp.Context())
// Register MsgServer and QueryServer
banktypes.RegisterMsgServer(integrationApp.MsgServiceRouter(), keeper.NewMsgServerImpl(bankKeeper))
banktypes.RegisterQueryServer(integrationApp.QueryHelper(), keeper.NewQuerier(&bankKeeper))
qr := integrationApp.QueryHelper()
queryClient := banktypes.NewQueryClient(qr)
f := deterministicFixture{
ctx: sdkCtx,
bankKeeper: bankKeeper,
queryClient: queryClient,
}
return &f
}
func fundAccount(f *deterministicFixture, addr sdk.AccAddress, coin ...sdk.Coin) {
@@ -235,7 +264,7 @@ func TestGRPCQueryTotalSupply(t *testing.T) {
assert.NilError(t, f.bankKeeper.MintCoins(f.ctx, minttypes.ModuleName, coins))
req := &banktypes.QueryTotalSupplyRequest{}
testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.TotalSupply, 243, false)
testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.TotalSupply, 150, false)
}
func TestGRPCQueryTotalSupplyOf(t *testing.T) {
File diff suppressed because it is too large Load Diff
+8
View File
@@ -16,8 +16,16 @@ import (
"github.com/cosmos/cosmos-sdk/x/bank/types"
)
type Querier struct {
BaseKeeper
}
var _ types.QueryServer = BaseKeeper{}
func NewQuerier(keeper *BaseKeeper) Querier {
return Querier{BaseKeeper: *keeper}
}
// Balance implements the Query/Balance gRPC method
func (k BaseKeeper) Balance(ctx context.Context, req *types.QueryBalanceRequest) (*types.QueryBalanceResponse, error) {
if req == nil {