Skip to content

Commit

Permalink
test(x/bank): write integration tests (#16052)
Browse files Browse the repository at this point in the history
  • Loading branch information
likhita-809 authored May 10, 2023
1 parent bb2d859 commit 52212cf
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1,885 deletions.
101 changes: 65 additions & 36 deletions tests/integration/bank/keeper/deterministic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{}

var interfaceRegistry codectypes.InterfaceRegistry

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,
keys := storetypes.NewKVStoreKeys(authtypes.StoreKey, banktypes.StoreKey)
cdc := moduletestutil.MakeTestEncodingConfig(auth.AppModuleBasic{}, bank.AppModuleBasic{}).Codec

logger := log.NewTestLogger(t)
cms := integration.CreateMultiStore(keys, logger)

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(),
)

authModule := auth.NewAppModule(cdc, accountKeeper, authsims.RandomGenesisAccounts, nil)
bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper, nil)

integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc, authModule, bankModule)

queryHelper := baseapp.NewQueryServerTestHelper(ctx, interfaceRegistry)
banktypes.RegisterQueryServer(queryHelper, f.bankKeeper)
f.queryClient = banktypes.NewQueryClient(queryHelper)
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
return &f
}

func fundAccount(f *deterministicFixture, addr sdk.AccAddress, coin ...sdk.Coin) {
Expand Down Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit 52212cf

Please sign in to comment.