From 7bc29076fb04a82b93a70348f9b9ddbd765b1d9f Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Tue, 9 Jan 2024 10:42:35 +0700 Subject: [PATCH] fix(test): fix cycle import --- app/app_test.go | 146 ------------------------------ app/{testing => }/mock.go | 2 +- app/sim_test.go | 11 ++- app/test_access.go | 73 --------------- app/{testing => }/test_helpers.go | 28 +++--- x/feeburn/ante/utils_test.go | 2 +- x/feeburn/keeper/keeper_test.go | 2 +- 7 files changed, 23 insertions(+), 241 deletions(-) delete mode 100644 app/app_test.go rename app/{testing => }/mock.go (98%) delete mode 100644 app/test_access.go rename app/{testing => }/test_helpers.go (93%) diff --git a/app/app_test.go b/app/app_test.go deleted file mode 100644 index 00b7d779..00000000 --- a/app/app_test.go +++ /dev/null @@ -1,146 +0,0 @@ -package app - -import ( - "encoding/json" - wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" - "os" - "testing" - "time" - - db "github.com/cometbft/cometbft-db" - abci "github.com/cometbft/cometbft/abci/types" - "github.com/cometbft/cometbft/libs/log" - tmtypes "github.com/cometbft/cometbft/types" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" - cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" - "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" - sdk "github.com/cosmos/cosmos-sdk/types" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/cosmos/ibc-go/v7/testing/mock" - "github.com/stretchr/testify/require" - - helpers "github.com/White-Whale-Defi-Platform/migaloo-chain/v3/app/testing" -) - -var emptyWasmOpts []wasmkeeper.Option - -func TestWasmdExport(t *testing.T) { - db := db.NewMemDB() - gapp := NewMigalooApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, make(map[int64]bool), DefaultNodeHome, 0, MakeEncodingConfig(), helpers.EmptyBaseAppOptions{}, emptyWasmOpts) - // generate validator private/public key - privVal := mock.NewPV() - pubKey, err := privVal.GetPubKey() - require.NoError(t, err) - - // create validator set with single validator - validator := tmtypes.NewValidator(pubKey, 1) - valSet := tmtypes.NewValidatorSet([]*tmtypes.Validator{validator}) - - // generate genesis account - senderPrivKey := secp256k1.GenPrivKey() - acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0) - amount, ok := sdk.NewIntFromString("10000000000000000000") - require.True(t, ok) - - balance := banktypes.Balance{ - Address: acc.GetAddress().String(), - Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, amount)), - } - - genesisState := SetupGenesisValSet(t, valSet, []authtypes.GenesisAccount{acc}, nil, gapp, balance) - stateBytes, err := json.MarshalIndent(genesisState, "", " ") - require.NoError(t, err) - - // Initialize the chain - gapp.InitChain( - abci.RequestInitChain{ - Validators: []abci.ValidatorUpdate{}, - AppStateBytes: stateBytes, - }, - ) - gapp.Commit() - - // Making a new app object with the db, so that initchain hasn't been called - newGapp := NewMigalooApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, MakeEncodingConfig(), helpers.EmptyBaseAppOptions{}, emptyWasmOpts) - _, err = newGapp.ExportAppStateAndValidators(false, []string{}, []string{}) - require.NoError(t, err, "ExportAppStateAndValidators should not have an error") -} - -// ensure that blocked addresses are properly set in bank keeper -func TestBlockedAddrs(t *testing.T) { - db := db.NewMemDB() - gapp := NewMigalooApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, make(map[int64]bool), DefaultNodeHome, 0, MakeEncodingConfig(), helpers.EmptyBaseAppOptions{}, emptyWasmOpts) - - for acc := range maccPerms { - t.Run(acc, func(t *testing.T) { - require.True(t, gapp.BankKeeper.BlockedAddr(gapp.AccountKeeper.GetModuleAddress(acc)), - "ensure that blocked addresses are properly set in bank keeper", - ) - }) - } -} - -func TestGetMaccPerms(t *testing.T) { - dup := GetMaccPerms() - require.Equal(t, maccPerms, dup, "duplicated module account permissions differed from actual module account permissions") -} - -func SetupGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, _ []wasmkeeper.Option, app *MigalooApp, balances ...banktypes.Balance) GenesisState { - genesisState := NewDefaultGenesisState() - // set genesis accounts - authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs) - genesisState[authtypes.ModuleName] = app.appCodec.MustMarshalJSON(authGenesis) - - validators := make([]stakingtypes.Validator, 0, len(valSet.Validators)) - delegations := make([]stakingtypes.Delegation, 0, len(valSet.Validators)) - - bondAmt := sdk.NewInt(1000000) - - for _, val := range valSet.Validators { - pk, err := cryptocodec.FromTmPubKeyInterface(val.PubKey) - require.NoError(t, err) - pkAny, err := codectypes.NewAnyWithValue(pk) - require.NoError(t, err) - validator := stakingtypes.Validator{ - OperatorAddress: sdk.ValAddress(val.Address).String(), - ConsensusPubkey: pkAny, - Jailed: false, - Status: stakingtypes.Bonded, - Tokens: bondAmt, - DelegatorShares: sdk.OneDec(), - Description: stakingtypes.Description{}, - UnbondingHeight: int64(0), - UnbondingTime: time.Unix(0, 0).UTC(), - Commission: stakingtypes.NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), - MinSelfDelegation: sdk.ZeroInt(), - } - validators = append(validators, validator) - delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdk.OneDec())) - - } - - // set validators and delegations - stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations) - genesisState[stakingtypes.ModuleName] = app.appCodec.MustMarshalJSON(stakingGenesis) - - totalSupply := sdk.NewCoins() - for _, b := range balances { - // add genesis acc tokens and delegated tokens to total supply - totalSupply = totalSupply.Add(b.Coins.Add(sdk.NewCoin(sdk.DefaultBondDenom, bondAmt))...) - } - - // add bonded amount to bonded pool module account - balances = append(balances, banktypes.Balance{ - Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(), - Coins: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, bondAmt)}, - }) - - // update total supply - bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, - []banktypes.Metadata{}, []banktypes.SendEnabled{}) - genesisState[banktypes.ModuleName] = app.appCodec.MustMarshalJSON(bankGenesis) - - return genesisState -} diff --git a/app/testing/mock.go b/app/mock.go similarity index 98% rename from app/testing/mock.go rename to app/mock.go index 164dca92..a70a7c1e 100644 --- a/app/testing/mock.go +++ b/app/mock.go @@ -1,4 +1,4 @@ -package helpers +package app import ( "github.com/cometbft/cometbft/crypto" diff --git a/app/sim_test.go b/app/sim_test.go index a86ea278..478ca50d 100644 --- a/app/sim_test.go +++ b/app/sim_test.go @@ -1,9 +1,14 @@ package app import ( - "cosmossdk.io/simapp" "encoding/json" "fmt" + "os" + "path/filepath" + "testing" + "time" + + "cosmossdk.io/simapp" "github.com/CosmWasm/wasmd/x/wasm" wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" dbm "github.com/cometbft/cometbft-db" @@ -36,10 +41,6 @@ import ( ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types" ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported" "github.com/stretchr/testify/require" - "os" - "path/filepath" - "testing" - "time" ) // Get flags every time the simulator is run diff --git a/app/test_access.go b/app/test_access.go deleted file mode 100644 index d57b9f74..00000000 --- a/app/test_access.go +++ /dev/null @@ -1,73 +0,0 @@ -package app - -import ( - "testing" - - "github.com/cosmos/cosmos-sdk/baseapp" - "github.com/cosmos/cosmos-sdk/client" - - "github.com/White-Whale-Defi-Platform/migaloo-chain/v3/app/params" - - "github.com/cosmos/cosmos-sdk/codec" - bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" - capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper" - stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" - ibctransferkeeper "github.com/cosmos/ibc-go/v7/modules/apps/transfer/keeper" - ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper" - - "github.com/CosmWasm/wasmd/x/wasm" -) - -// Deprecated: use public app attributes directly -type TestSupport struct { - t testing.TB - app *MigalooApp -} - -func NewTestSupport(t testing.TB, app *MigalooApp) *TestSupport { - return &TestSupport{t: t, app: app} -} - -func (s TestSupport) IBCKeeper() *ibckeeper.Keeper { - return s.app.IBCKeeper -} - -func (s TestSupport) WasmKeeper() wasm.Keeper { - return s.app.WasmKeeper -} - -func (s TestSupport) AppCodec() codec.Codec { - return s.app.appCodec -} - -func (s TestSupport) ScopedWasmIBCKeeper() capabilitykeeper.ScopedKeeper { - return s.app.ScopedWasmKeeper -} - -func (s TestSupport) ScopeIBCKeeper() capabilitykeeper.ScopedKeeper { - return s.app.ScopedIBCKeeper -} - -func (s TestSupport) ScopedTransferKeeper() capabilitykeeper.ScopedKeeper { - return s.app.ScopedTransferKeeper -} - -func (s TestSupport) StakingKeeper() *stakingkeeper.Keeper { - return s.app.StakingKeeper -} - -func (s TestSupport) BankKeeper() bankkeeper.Keeper { - return s.app.BankKeeper -} - -func (s TestSupport) TransferKeeper() ibctransferkeeper.Keeper { - return s.app.TransferKeeper -} - -func (s TestSupport) GetBaseApp() *baseapp.BaseApp { - return s.app.BaseApp -} - -func (s TestSupport) GetTxConfig() client.TxConfig { - return params.MakeEncodingConfig().TxConfig -} diff --git a/app/testing/test_helpers.go b/app/test_helpers.go similarity index 93% rename from app/testing/test_helpers.go rename to app/test_helpers.go index c84c2b3e..70e2bbd9 100644 --- a/app/testing/test_helpers.go +++ b/app/test_helpers.go @@ -1,10 +1,12 @@ -package helpers +package app import ( "encoding/json" + "testing" + "time" + "github.com/CosmWasm/wasmd/x/wasm" wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" - "github.com/White-Whale-Defi-Platform/migaloo-chain/v3/app" config "github.com/White-Whale-Defi-Platform/migaloo-chain/v3/app/params" dbm "github.com/cometbft/cometbft-db" abci "github.com/cometbft/cometbft/abci/types" @@ -25,8 +27,6 @@ import ( stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" - "testing" - "time" ) // SimAppChainID hardcoded chainID for simulation @@ -39,7 +39,7 @@ var emptyWasmOpts []wasm.Option type KeeperTestHelper struct { suite.Suite - App *app.MigalooApp + App *MigalooApp Ctx sdk.Context // ctx is deliver ctx CheckCtx sdk.Context QueryHelper *baseapp.QueryServiceTestHelper @@ -81,7 +81,7 @@ type EmptyAppOptions struct{} func (EmptyAppOptions) Get(_ string) interface{} { return nil } -func SetupApp(t *testing.T) *app.MigalooApp { +func SetupApp(t *testing.T) *MigalooApp { t.Helper() privVal := NewPV() @@ -108,7 +108,7 @@ func SetupApp(t *testing.T) *app.MigalooApp { // that also act as delegators. For simplicity, each validator is bonded with a delegation // of one consensus engine unit in the default token of the app from first genesis // account. A Nop logger is set in app. -func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *app.MigalooApp { +func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *MigalooApp { t.Helper() migalooApp, genesisState := setup(true) @@ -140,23 +140,23 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs return migalooApp } -func setup(withGenesis bool, opts ...wasmkeeper.Option) (*app.MigalooApp, app.GenesisState) { +func setup(withGenesis bool, opts ...wasmkeeper.Option) (*MigalooApp, GenesisState) { db := dbm.NewMemDB() - migalooApp := app.NewMigalooApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, - app.DefaultNodeHome, 5, app.MakeEncodingConfig(), EmptyBaseAppOptions{}, opts) + migalooApp := NewMigalooApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, + DefaultNodeHome, 5, MakeEncodingConfig(), EmptyBaseAppOptions{}, opts) if withGenesis { - return migalooApp, app.NewDefaultGenesisState() + return migalooApp, NewDefaultGenesisState() } - return migalooApp, app.GenesisState{} + return migalooApp, GenesisState{} } func genesisStateWithValSet(t *testing.T, - app *app.MigalooApp, genesisState app.GenesisState, + app *MigalooApp, genesisState GenesisState, valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance, -) app.GenesisState { +) GenesisState { // set genesis accounts authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs) genesisState[authtypes.ModuleName] = app.AppCodec().MustMarshalJSON(authGenesis) diff --git a/x/feeburn/ante/utils_test.go b/x/feeburn/ante/utils_test.go index 43f8fa3e..ffae622e 100644 --- a/x/feeburn/ante/utils_test.go +++ b/x/feeburn/ante/utils_test.go @@ -1,8 +1,8 @@ package ante_test import ( + apptesting "github.com/White-Whale-Defi-Platform/migaloo-chain/v3/app" config "github.com/White-Whale-Defi-Platform/migaloo-chain/v3/app/params" - apptesting "github.com/White-Whale-Defi-Platform/migaloo-chain/v3/app/testing" "github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/types/tx/signing" diff --git a/x/feeburn/keeper/keeper_test.go b/x/feeburn/keeper/keeper_test.go index b4562005..b42cd5bb 100644 --- a/x/feeburn/keeper/keeper_test.go +++ b/x/feeburn/keeper/keeper_test.go @@ -1,8 +1,8 @@ package keeper_test import ( + apptesting "github.com/White-Whale-Defi-Platform/migaloo-chain/v3/app" config "github.com/White-Whale-Defi-Platform/migaloo-chain/v3/app/params" - apptesting "github.com/White-Whale-Defi-Platform/migaloo-chain/v3/app/testing" "github.com/cometbft/cometbft/crypto/ed25519" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/tx"