Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve unit test #2914

Merged
merged 3 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 36 additions & 57 deletions app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
tokentypes "github.com/irisnet/irismod/modules/token/types"
"github.com/stretchr/testify/require"

"github.com/irisnet/irishub/v3/app/params"
iristypes "github.com/irisnet/irishub/v3/types"

dbm "github.com/cometbft/cometbft-db"
Expand Down Expand Up @@ -98,15 +99,7 @@ func TestFullAppSimulation(t *testing.T) {

encfg := RegisterEncodingConfig()

app := NewIrisApp(
logger,
db,
nil,
true,
encfg,
EmptyAppOptions{},
fauxMerkleModeOpt,
)
app := createApp(logger, db, encfg, fauxMerkleModeOpt)
require.Equal(t, "IrisApp", app.Name())

// run randomized simulation
Expand Down Expand Up @@ -136,6 +129,8 @@ func TestAppImportExport(t *testing.T) {
config := simcli.NewConfigFromFlags()
config.ChainID = AppChainID

sdk.DefaultBondDenom = iristypes.NativeToken.Symbol

dreamer-zq marked this conversation as resolved.
Show resolved Hide resolved
db, dir, logger, skip, err := simtestutil.SetupSimulation(
config,
"goleveldb-app-sim",
Expand All @@ -155,15 +150,7 @@ func TestAppImportExport(t *testing.T) {
}()

encfg := RegisterEncodingConfig()
app := NewIrisApp(
logger,
db,
nil,
true,
encfg,
EmptyAppOptions{},
fauxMerkleModeOpt,
)
app := createApp(logger, db, encfg, fauxMerkleModeOpt)
require.Equal(t, "IrisApp", app.Name())

// Run randomized simulation
Expand Down Expand Up @@ -209,15 +196,7 @@ func TestAppImportExport(t *testing.T) {
require.NoError(t, os.RemoveAll(newDir))
}()

newApp := NewIrisApp(
log.NewNopLogger(),
newDB,
nil,
true,
encfg,
EmptyAppOptions{},
fauxMerkleModeOpt,
)
newApp := createApp(logger, db, encfg, fauxMerkleModeOpt)
require.Equal(t, "IrisApp", newApp.Name())

var genesisState iristypes.GenesisState
Expand All @@ -244,11 +223,13 @@ func TestAppImportExport(t *testing.T) {

storeKeysPrefixes := []StoreKeysPrefixes{
{app.AppKeepers.KvStoreKeys()[authtypes.StoreKey], newApp.AppKeepers.KvStoreKeys()[authtypes.StoreKey], [][]byte{}},
{app.AppKeepers.KvStoreKeys()[stakingtypes.StoreKey], newApp.AppKeepers.KvStoreKeys()[stakingtypes.StoreKey],
{
app.AppKeepers.KvStoreKeys()[stakingtypes.StoreKey], newApp.AppKeepers.KvStoreKeys()[stakingtypes.StoreKey],
[][]byte{
stakingtypes.UnbondingQueueKey, stakingtypes.RedelegationQueueKey, stakingtypes.ValidatorQueueKey,
stakingtypes.HistoricalInfoKey,
}}, // ordering may change but it doesn't matter
},
}, // ordering may change but it doesn't matter
{app.AppKeepers.KvStoreKeys()[slashingtypes.StoreKey], newApp.AppKeepers.KvStoreKeys()[slashingtypes.StoreKey], [][]byte{}},
{app.AppKeepers.KvStoreKeys()[minttypes.StoreKey], newApp.AppKeepers.KvStoreKeys()[minttypes.StoreKey], [][]byte{}},
{app.AppKeepers.KvStoreKeys()[distrtypes.StoreKey], newApp.AppKeepers.KvStoreKeys()[distrtypes.StoreKey], [][]byte{}},
Expand All @@ -267,7 +248,7 @@ func TestAppImportExport(t *testing.T) {
// check irismod module
{app.AppKeepers.KvStoreKeys()[tokentypes.StoreKey], newApp.AppKeepers.KvStoreKeys()[tokentypes.StoreKey], [][]byte{}},
{app.AppKeepers.KvStoreKeys()[oracletypes.StoreKey], newApp.AppKeepers.KvStoreKeys()[oracletypes.StoreKey], [][]byte{}},
//mt.Supply is InitSupply, can be not equal to TotalSupply
// mt.Supply is InitSupply, can be not equal to TotalSupply
{app.AppKeepers.KvStoreKeys()[mttypes.StoreKey], newApp.AppKeepers.KvStoreKeys()[mttypes.StoreKey], [][]byte{mttypes.PrefixMT}},
{app.AppKeepers.KvStoreKeys()[nfttypes.StoreKey], newApp.AppKeepers.KvStoreKeys()[nfttypes.StoreKey], [][]byte{{0x05}}},
{
Expand Down Expand Up @@ -334,15 +315,7 @@ func TestAppSimulationAfterImport(t *testing.T) {
require.NoError(t, os.RemoveAll(dir))
}()

app := NewIrisApp(
logger,
db,
nil,
true,
encfg,
EmptyAppOptions{},
fauxMerkleModeOpt,
)
app := createApp(logger, db, encfg, fauxMerkleModeOpt)
require.Equal(t, "IrisApp", app.Name())

// Run randomized simulation
Expand Down Expand Up @@ -393,15 +366,7 @@ func TestAppSimulationAfterImport(t *testing.T) {
require.NoError(t, os.RemoveAll(newDir))
}()

newApp := NewIrisApp(
log.NewNopLogger(),
newDB,
nil,
true,
encfg,
EmptyAppOptions{},
fauxMerkleModeOpt,
)
newApp := createApp(logger, db, encfg, fauxMerkleModeOpt)
require.Equal(t, "IrisApp", newApp.Name())

newApp.InitChain(abci.RequestInitChain{
Expand All @@ -428,6 +393,7 @@ func TestAppStateDeterminism(t *testing.T) {
if !simcli.FlagEnabledValue {
t.Skip("skipping application simulation")
}
sdk.DefaultBondDenom = iristypes.NativeToken.Symbol
dreamer-zq marked this conversation as resolved.
Show resolved Hide resolved

config := simcli.NewConfigFromFlags()
config.InitialBlockHeight = 1
Expand All @@ -453,15 +419,7 @@ func TestAppStateDeterminism(t *testing.T) {
}

db := dbm.NewMemDB()
app := NewIrisApp(
logger,
db,
nil,
true,
encfg,
EmptyAppOptions{},
interBlockCacheOpt(),
)
app := createApp(logger, db, encfg, interBlockCacheOpt())

fmt.Printf(
"running non-determinism simulation; seed %d: %d/%d, attempt: %d/%d\n",
Expand Down Expand Up @@ -516,3 +474,24 @@ type EmptyAppOptions struct{}
func (ao EmptyAppOptions) Get(o string) interface{} {
return nil
}

func createApp(
logger log.Logger,
db dbm.DB,
encodingConfig params.EncodingConfig,
baseAppOptions ...func(*baseapp.BaseApp),
) *IrisApp {
if baseAppOptions == nil {
baseAppOptions = []func(*baseapp.BaseApp){}
}
baseAppOptions = append(baseAppOptions, baseapp.SetChainID(AppChainID))
return NewIrisApp(
logger,
db,
nil,
true,
encodingConfig,
EmptyAppOptions{},
baseAppOptions...,
)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ require (
)

require (
cosmossdk.io/simapp v0.0.0-20230608160436-666c345ad23d
github.com/gogo/protobuf v1.3.2
github.com/olebedev/config v0.0.0-20190528211619-364964f3a8e4
github.com/pkg/errors v0.9.1
Expand All @@ -52,6 +51,7 @@ require (
require (
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cosmossdk.io/log v1.3.1 // indirect
cosmossdk.io/simapp v0.0.0-20230608160436-666c345ad23d // indirect
cosmossdk.io/tools/rosetta v0.2.1 // indirect
github.com/btcsuite/btcd/btcutil v1.1.3 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect
Expand Down
6 changes: 3 additions & 3 deletions modules/guardian/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
guardiancli "github.com/irisnet/irishub/v3/modules/guardian/client/cli"
guardiantestutil "github.com/irisnet/irishub/v3/modules/guardian/client/testutil"
guardiantypes "github.com/irisnet/irishub/v3/modules/guardian/types"
"github.com/irisnet/irishub/v3/simapp"
apptestutil "github.com/irisnet/irishub/v3/testutil"
)

var privKey cryptotypes.PrivKey
Expand All @@ -35,7 +35,7 @@ type IntegrationTestSuite struct {
func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")

cfg := simapp.NewConfig()
cfg := apptestutil.NewConfig()
cfg.NumValidators = 1

privKey, pubKey, addr = testdata.KeyTestPubAddr()
Expand Down Expand Up @@ -90,7 +90,7 @@ func (s *IntegrationTestSuite) TestGuardian() {
),
}

result := simapp.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
10 changes: 6 additions & 4 deletions modules/guardian/client/testutil/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
"github.com/cosmos/cosmos-sdk/testutil/network"

guardiancli "github.com/irisnet/irishub/v3/modules/guardian/client/cli"
"github.com/irisnet/irishub/v3/simapp"
apptestutil "github.com/irisnet/irishub/v3/testutil"
)

// MsgRedelegateExec creates a redelegate message.
// CreateSuperExec creates a new super
func CreateSuperExec(
t *testing.T,
network *network.Network,
Expand All @@ -30,9 +30,10 @@ func CreateSuperExec(
}
args = append(args, extraArgs...)

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

// DeleteSuperExec deletes a super
func DeleteSuperExec(
t *testing.T,
network *network.Network,
Expand All @@ -45,9 +46,10 @@ func DeleteSuperExec(
}
args = append(args, extraArgs...)

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

// QuerySupersExec queries supers
func QuerySupersExec(clientCtx client.Context, extraArgs ...string) (testutil.BufferWriter, error) {
args := []string{
fmt.Sprintf("--%s=json", cli.OutputFlag),
Expand Down
4 changes: 2 additions & 2 deletions modules/guardian/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/irisnet/irishub/v3/modules/guardian"
"github.com/irisnet/irishub/v3/modules/guardian/keeper"
"github.com/irisnet/irishub/v3/modules/guardian/types"
"github.com/irisnet/irishub/v3/simapp"
"github.com/irisnet/irishub/v3/testutil"
)

type TestSuite struct {
Expand All @@ -25,7 +25,7 @@ type TestSuite struct {
}

func (suite *TestSuite) SetupTest() {
app := simapp.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
9 changes: 5 additions & 4 deletions modules/guardian/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ 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"

"github.com/irisnet/irishub/v3/modules/guardian/keeper"
"github.com/irisnet/irishub/v3/modules/guardian/types"
"github.com/irisnet/irishub/v3/simapp"
"github.com/irisnet/irishub/v3/testutil"
)

var (
Expand All @@ -35,16 +36,16 @@ type KeeperTestSuite struct {
suite.Suite

cdc *codec.LegacyAmino
ifr codectypes.InterfaceRegistry
ctx sdk.Context
keeper keeper.Keeper
app *simapp.SimApp
}

func (suite *KeeperTestSuite) SetupTest() {
app := simapp.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
6 changes: 3 additions & 3 deletions modules/mint/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"github.com/irisnet/irishub/v3/modules/mint"
"github.com/irisnet/irishub/v3/modules/mint/types"
"github.com/irisnet/irishub/v3/simapp"
apptestutil "github.com/irisnet/irishub/v3/testutil"
)

func TestBeginBlocker(t *testing.T) {
Expand All @@ -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) (*simapp.SimApp, sdk.Context) {
app := simapp.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
4 changes: 2 additions & 2 deletions modules/mint/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

minttestutil "github.com/irisnet/irishub/v3/modules/mint/client/testutil"
minttypes "github.com/irisnet/irishub/v3/modules/mint/types"
"github.com/irisnet/irishub/v3/simapp"
"github.com/irisnet/irishub/v3/testutil"
)

type IntegrationTestSuite struct {
Expand All @@ -22,7 +22,7 @@ type IntegrationTestSuite struct {
func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")

cfg := simapp.NewConfig()
cfg := testutil.NewConfig()
cfg.NumValidators = 1

var err error
Expand Down
2 changes: 1 addition & 1 deletion modules/mint/client/rest/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ package rest_test
// "github.com/cosmos/cosmos-sdk/types/rest"

// minttypes "github.com/irisnet/irishub/v3/modules/mint/types"
// "github.com/irisnet/irishub/v3/simapp"
// "github.com/irisnet/irishub/v3/testutil"
// )

// type IntegrationTestSuite struct {
Expand Down
Loading
Loading