Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamer-zq committed Mar 13, 2024
1 parent aee2a7d commit ece68a5
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 71 deletions.
2 changes: 1 addition & 1 deletion modules/guardian/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (s *IntegrationTestSuite) TestGuardian() {
),
}

result := apptestutil.ExecTxCmdWithResult(s.T(), s.network, clientCtx, bankcli.NewSendTxCmd(), args)
result := apptestutil.ExecCommand(s.T(), s.network, clientCtx, bankcli.NewSendTxCmd(), args)
s.Require().Equal(uint32(0), result.TxResult.Code, result.TxResult.Log)

//------test GetCmdQuerySupers()-------------
Expand Down
4 changes: 2 additions & 2 deletions modules/guardian/client/testutil/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func CreateSuperExec(
}
args = append(args, extraArgs...)

return apptestutil.ExecTxCmdWithResult(t, network, clientCtx, guardiancli.GetCmdCreateSuper(), args)
return apptestutil.ExecCommand(t, network, clientCtx, guardiancli.GetCmdCreateSuper(), args)
}

// DeleteSuperExec deletes a super
Expand All @@ -46,7 +46,7 @@ func DeleteSuperExec(
}
args = append(args, extraArgs...)

return apptestutil.ExecTxCmdWithResult(t, network, clientCtx, guardiancli.GetCmdDeleteSuper(), args)
return apptestutil.ExecCommand(t, network, clientCtx, guardiancli.GetCmdDeleteSuper(), args)
}

// QuerySupersExec queries supers
Expand Down
2 changes: 1 addition & 1 deletion modules/guardian/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type TestSuite struct {
}

func (suite *TestSuite) SetupTest() {
app := testutil.Setup(suite.T(), false)
app := testutil.CreateApp(suite.T())

suite.cdc = codec.NewAminoCodec(app.LegacyAmino())
suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{})
Expand Down
8 changes: 3 additions & 5 deletions modules/guardian/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@ import (
)

func (suite *KeeperTestSuite) TestGRPCQuerySupers() {
app, ctx := suite.app, suite.ctx
_, _, addr := testdata.KeyTestPubAddr()
guardian := types.NewSuper("test", types.Ordinary, addr, addr)

queryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, app.GuardianKeeper)
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.ifr)
types.RegisterQueryServer(queryHelper, suite.keeper)
queryClient := types.NewQueryClient(queryHelper)

_, err := queryClient.Supers(gocontext.Background(), &types.QuerySupersRequest{})
suite.Require().NoError(err)

app.GuardianKeeper.AddSuper(ctx, guardian)

suite.keeper.AddSuper(suite.ctx, guardian)
supersResp, err := queryClient.Supers(gocontext.Background(), &types.QuerySupersRequest{})
suite.Require().NoError(err)
suite.Len(supersResp.Supers, 1)
Expand Down
7 changes: 4 additions & 3 deletions modules/guardian/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -35,16 +36,16 @@ type KeeperTestSuite struct {
suite.Suite

cdc *codec.LegacyAmino
ifr codectypes.InterfaceRegistry
ctx sdk.Context
keeper keeper.Keeper
app *testutil.AppBuilder
}

func (suite *KeeperTestSuite) SetupTest() {
app := testutil.Setup(suite.T(), false)
app := testutil.CreateApp(suite.T())

suite.app = app
suite.cdc = app.LegacyAmino()
suite.ifr = app.InterfaceRegistry()
suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{})
suite.keeper = app.GuardianKeeper
}
Expand Down
4 changes: 2 additions & 2 deletions modules/mint/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func TestBeginBlocker(t *testing.T) {
}

// returns context and an app with updated mint keeper
func createTestApp(t *testing.T, isCheckTx bool) (*apptestutil.AppBuilder, sdk.Context) {
app := apptestutil.Setup(t, false)
func createTestApp(t *testing.T, isCheckTx bool) (*apptestutil.AppWrapper, sdk.Context) {
app := apptestutil.CreateApp(t)

ctx := app.BaseApp.NewContext(isCheckTx, tmproto.Header{Height: 2})
app.MintKeeper.SetParams(ctx, types.NewParams(
Expand Down
7 changes: 2 additions & 5 deletions modules/mint/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

tmproto "github.com/cometbft/cometbft/proto/tendermint/types"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/irisnet/irishub/v3/modules/mint/types"
Expand All @@ -19,15 +18,13 @@ import (
type KeeperTestSuite struct {
suite.Suite

cdc *codec.LegacyAmino
ctx sdk.Context
app *apptestutil.AppBuilder
app *apptestutil.AppWrapper
}

func (suite *KeeperTestSuite) SetupTest() {
app := apptestutil.Setup(suite.T(), false)
app := apptestutil.CreateApp(suite.T())

suite.cdc = app.LegacyAmino()
suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{})
suite.app = app

Expand Down
14 changes: 7 additions & 7 deletions testutil/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ import (
)

var (
_ runtime.AppI = (*AppBuilder)(nil)
_ servertypes.Application = (*AppBuilder)(nil)
_ runtime.AppI = (*AppWrapper)(nil)
_ servertypes.Application = (*AppWrapper)(nil)
)

// AppBuilder extends an ABCI application, but with most of its parameters exported.
// AppWrapper extends an ABCI application, but with most of its parameters exported.
// They are exported for convenience in creating helper functions, as object
// capabilities aren't needed for testing.
type AppBuilder struct {
type AppWrapper struct {
*app.IrisApp
}

func (ab AppBuilder) build(
func setup(
appOpts servertypes.AppOptions,
baseAppOptions ...func(*baseapp.BaseApp),
) *app.IrisApp {
) *AppWrapper {
db := dbm.NewMemDB()
encCdc := app.RegisterEncodingConfig()
if appOpts == nil {
Expand All @@ -45,7 +45,7 @@ func (ab AppBuilder) build(
appOpts,
baseAppOptions...,
)
return app
return &AppWrapper{app}
}

// MakeCodecs returns the application codec and tx codec
Expand Down
13 changes: 0 additions & 13 deletions testutil/export.go

This file was deleted.

14 changes: 0 additions & 14 deletions testutil/genesis.go

This file was deleted.

33 changes: 15 additions & 18 deletions testutil/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ import (
"github.com/irisnet/irishub/v3/app"
)

// Setup initializes a new SimApp. A Nop logger is set in SimApp.
func Setup(t *testing.T, _ bool) *AppBuilder {
// CreateApp initializes a new SimApp. A Nop logger is set in SimApp.
func CreateApp(t *testing.T) *AppWrapper {
t.Helper()

privVal := mock.NewPV()
Expand All @@ -65,7 +65,7 @@ func Setup(t *testing.T, _ bool) *AppBuilder {
Address: acc.GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000000000))),
}
return SetupWithGenesisValSet(t, valSet, []authtypes.GenesisAccount{acc}, balance)
return CreateAppWithGenesisValSet(t, valSet, []authtypes.GenesisAccount{acc}, balance)
}

// NewConfig returns a new app config
Expand All @@ -77,8 +77,7 @@ func NewConfig() network.Config {
cfg.LegacyAmino = encCfg.Amino
cfg.InterfaceRegistry = encCfg.InterfaceRegistry
cfg.AppConstructor = func(val network.ValidatorI) servertypes.Application {
builder := &AppBuilder{}
return builder.build(
return setup(
nil,
bam.SetMinGasPrices(val.GetAppConfig().MinGasPrices),
bam.SetChainID(cfg.ChainID),
Expand All @@ -88,22 +87,21 @@ func NewConfig() network.Config {
return cfg
}

// SetupWithGenesisValSet initializes a new SimApp with a validator set and genesis accounts
// CreateAppWithGenesisValSet initializes a new SimApp with a validator set and genesis accounts
// that also act as delegators. For simplicity, each validator is bonded with a delegation
// of one consensus engine unit (10^6) in the default token of the simapp from first genesis
// account. A Nop logger is set in SimApp.
func SetupWithGenesisValSet(
func CreateAppWithGenesisValSet(
t *testing.T,
valSet *tmtypes.ValidatorSet,
genAccs []authtypes.GenesisAccount,
balances ...banktypes.Balance,
) *AppBuilder {
) *AppWrapper {
t.Helper()

builder := &AppBuilder{}
app := builder.build(nil)
app := setup(nil)

genesisState, err := GenesisStateWithValSet(
genesisState, err := genesisStateWithValSet(
app.AppCodec(),
app.DefaultGenesis(),
valSet,
Expand Down Expand Up @@ -133,11 +131,11 @@ func SetupWithGenesisValSet(
NextValidatorsHash: valSet.Hash(),
}})

return &AppBuilder{app}
return app
}

// GenesisStateWithValSet returns a new genesis state with the validator set
func GenesisStateWithValSet(codec codec.Codec, genesisState map[string]json.RawMessage,
// genesisStateWithValSet returns a new genesis state with the validator set
func genesisStateWithValSet(codec codec.Codec, genesisState map[string]json.RawMessage,
valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount,
balances ...banktypes.Balance,
) (map[string]json.RawMessage, error) {
Expand Down Expand Up @@ -240,8 +238,7 @@ func NewTestNetworkFixture() network.TestFixture {
defer os.RemoveAll(dir)

appCtr := func(val network.ValidatorI) servertypes.Application {
builder := &AppBuilder{}
return builder.build(
return setup(
simtestutil.NewAppOptionsWithFlagHome(val.GetCtx().Config.RootDir),
bam.SetPruning(pruningtypes.NewPruningOptionsFromString(val.GetAppConfig().Pruning)),
bam.SetMinGasPrices(val.GetAppConfig().MinGasPrices),
Expand All @@ -262,8 +259,8 @@ func NewTestNetworkFixture() network.TestFixture {
}
}

// ExecTxCmdWithResult executes a tx command and returns the result
func ExecTxCmdWithResult(
// ExecCommand executes a tx command and returns the result
func ExecCommand(
t *testing.T,
network *network.Network,
clientCtx client.Context,
Expand Down

0 comments on commit ece68a5

Please sign in to comment.