From 1e00907ce62f93d19caed9db70ef5b703aabb66c Mon Sep 17 00:00:00 2001 From: DauTT Date: Sat, 18 Jul 2020 12:59:10 +0200 Subject: [PATCH 1/3] x/bank/simulation/operations.go: add unit tests --- x/bank/simulation/operations_test.go | 145 +++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 x/bank/simulation/operations_test.go diff --git a/x/bank/simulation/operations_test.go b/x/bank/simulation/operations_test.go new file mode 100644 index 000000000000..e1b0b1fc32aa --- /dev/null +++ b/x/bank/simulation/operations_test.go @@ -0,0 +1,145 @@ +package simulation_test + +import ( + "math/rand" + "testing" + + "github.com/stretchr/testify/suite" + + abci "github.com/tendermint/tendermint/abci/types" + + "github.com/cosmos/cosmos-sdk/simapp" + simappparams "github.com/cosmos/cosmos-sdk/simapp/params" + sdk "github.com/cosmos/cosmos-sdk/types" + simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + "github.com/cosmos/cosmos-sdk/x/bank/simulation" + "github.com/cosmos/cosmos-sdk/x/bank/types" +) + +// TestWeightedOperations tests the weights of the operations. +func (suite *SimTestSuite) TestWeightedOperations() { + cdc := suite.app.Codec() + appParams := make(simtypes.AppParams) + + weightesOps := simulation.WeightedOperations(appParams, cdc, suite.app.AccountKeeper, suite.app.BankKeeper) + + // setup 3 accounts + s := rand.NewSource(1) + r := rand.New(s) + accs := suite.getTestingAccounts(r, 3) + + expected := []struct { + weight int + opMsgRoute string + opMsgName string + }{ + {simappparams.DefaultWeightMsgSend, types.ModuleName, types.TypeMsgSend}, + {simappparams.DefaultWeightMsgMultiSend, types.ModuleName, types.TypeMsgMultiSend}, + } + + for i, w := range weightesOps { + operationMsg, _, _ := w.Op()(r, suite.app.BaseApp, suite.ctx, accs, "") + // the following checks are very much dependent from the ordering of the output given + // by WeightedOperations. if the ordering in WeightedOperations changes some tests + // will fail + suite.Require().Equal(expected[i].weight, w.Weight(), "weight should be the same") + suite.Require().Equal(expected[i].opMsgRoute, operationMsg.Route, "route should be the same") + suite.Require().Equal(expected[i].opMsgName, operationMsg.Name, "operation Msg name should be the same") + } +} + +// TestSimulateMsgSend tests the normal scenario of a valid message of type TypeMsgSend. +// Abonormal scenarios, where the message is created by an errors, are not tested here. +func (suite *SimTestSuite) TestSimulateMsgSend() { + + // setup 3 accounts + s := rand.NewSource(1) + r := rand.New(s) + accounts := suite.getTestingAccounts(r, 3) + + // begin a new block + suite.app.BeginBlock(abci.RequestBeginBlock{Header: abci.Header{Height: suite.app.LastBlockHeight() + 1, AppHash: suite.app.LastCommitID().Hash}}) + + // execute operation + op := simulation.SimulateMsgSend(suite.app.AccountKeeper, suite.app.BankKeeper) + operationMsg, futureOperations, err := op(r, suite.app.BaseApp, suite.ctx, accounts, "") + suite.Require().NoError(err) + + var msg types.MsgSend + types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + + suite.Require().True(operationMsg.OK) + suite.Require().Equal("65337742stake", msg.Amount.String()) + suite.Require().Equal("cosmos1ghekyjucln7y67ntx7cf27m9dpuxxemn4c8g4r", msg.FromAddress.String()) + suite.Require().Equal("cosmos1p8wcgrjr4pjju90xg6u9cgq55dxwq8j7u4x9a0", msg.ToAddress.String()) + suite.Require().Equal(types.TypeMsgSend, msg.Type()) + suite.Require().Equal(types.ModuleName, msg.Route()) + suite.Require().Len(futureOperations, 0) +} + +// TestSimulateMsgSend tests the normal scenario of a valid message of type TypeMsgMultiSend. +// Abonormal scenarios, where the message is created by an errors, are not tested here. +func (suite *SimTestSuite) TestSimulateMsgMultiSend() { + + // setup 3 accounts + s := rand.NewSource(1) + r := rand.New(s) + accounts := suite.getTestingAccounts(r, 3) + + // begin a new block + suite.app.BeginBlock(abci.RequestBeginBlock{Header: abci.Header{Height: suite.app.LastBlockHeight() + 1, AppHash: suite.app.LastCommitID().Hash}}) + + // execute operation + op := simulation.SimulateMsgMultiSend(suite.app.AccountKeeper, suite.app.BankKeeper) + operationMsg, futureOperations, err := op(r, suite.app.BaseApp, suite.ctx, accounts, "") + suite.Require().NoError(err) + + var msg types.MsgMultiSend + types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + + suite.Require().True(operationMsg.OK) + suite.Require().Len(msg.Inputs, 3) + suite.Require().Equal("cosmos1p8wcgrjr4pjju90xg6u9cgq55dxwq8j7u4x9a0", msg.Inputs[1].GetAddress().String()) + suite.Require().Equal("185121068stake", msg.Inputs[1].GetCoins().String()) + suite.Require().Len(msg.Outputs, 2) + suite.Require().Equal("cosmos1ghekyjucln7y67ntx7cf27m9dpuxxemn4c8g4r", msg.Outputs[1].GetAddress().String()) + suite.Require().Equal("260469617stake", msg.Outputs[1].GetCoins().String()) + suite.Require().Equal(types.TypeMsgMultiSend, msg.Type()) + suite.Require().Equal(types.ModuleName, msg.Route()) + suite.Require().Len(futureOperations, 0) +} + +type SimTestSuite struct { + suite.Suite + + ctx sdk.Context + app *simapp.SimApp +} + +func (suite *SimTestSuite) SetupTest() { + checkTx := false + app := simapp.Setup(checkTx) + suite.app = app + suite.ctx = app.BaseApp.NewContext(checkTx, abci.Header{}) +} + +func (suite *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Account { + accounts := simtypes.RandomAccounts(r, n) + + initAmt := sdk.TokensFromConsensusPower(200) + initCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt)) + + // add coins to the accounts + for _, account := range accounts { + acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, account.Address) + suite.app.AccountKeeper.SetAccount(suite.ctx, acc) + err := suite.app.BankKeeper.SetBalances(suite.ctx, account.Address, initCoins) + suite.Require().NoError(err) + } + + return accounts +} + +func TestSimTestSuite(t *testing.T) { + suite.Run(t, new(SimTestSuite)) +} From c4a92c4a0365aa0198866d61aa9de605d5ad1321 Mon Sep 17 00:00:00 2001 From: DauTT Date: Sat, 18 Jul 2020 20:19:57 +0200 Subject: [PATCH 2/3] Implement requested changes --- x/bank/simulation/operations_test.go | 30 +++++++++++++--------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/x/bank/simulation/operations_test.go b/x/bank/simulation/operations_test.go index e1b0b1fc32aa..e4c1eddfc36a 100644 --- a/x/bank/simulation/operations_test.go +++ b/x/bank/simulation/operations_test.go @@ -16,6 +16,20 @@ import ( "github.com/cosmos/cosmos-sdk/x/bank/types" ) +type SimTestSuite struct { + suite.Suite + + ctx sdk.Context + app *simapp.SimApp +} + +func (suite *SimTestSuite) SetupTest() { + checkTx := false + app := simapp.Setup(checkTx) + suite.app = app + suite.ctx = app.BaseApp.NewContext(checkTx, abci.Header{}) +} + // TestWeightedOperations tests the weights of the operations. func (suite *SimTestSuite) TestWeightedOperations() { cdc := suite.app.Codec() @@ -51,7 +65,6 @@ func (suite *SimTestSuite) TestWeightedOperations() { // TestSimulateMsgSend tests the normal scenario of a valid message of type TypeMsgSend. // Abonormal scenarios, where the message is created by an errors, are not tested here. func (suite *SimTestSuite) TestSimulateMsgSend() { - // setup 3 accounts s := rand.NewSource(1) r := rand.New(s) @@ -80,7 +93,6 @@ func (suite *SimTestSuite) TestSimulateMsgSend() { // TestSimulateMsgSend tests the normal scenario of a valid message of type TypeMsgMultiSend. // Abonormal scenarios, where the message is created by an errors, are not tested here. func (suite *SimTestSuite) TestSimulateMsgMultiSend() { - // setup 3 accounts s := rand.NewSource(1) r := rand.New(s) @@ -109,20 +121,6 @@ func (suite *SimTestSuite) TestSimulateMsgMultiSend() { suite.Require().Len(futureOperations, 0) } -type SimTestSuite struct { - suite.Suite - - ctx sdk.Context - app *simapp.SimApp -} - -func (suite *SimTestSuite) SetupTest() { - checkTx := false - app := simapp.Setup(checkTx) - suite.app = app - suite.ctx = app.BaseApp.NewContext(checkTx, abci.Header{}) -} - func (suite *SimTestSuite) getTestingAccounts(r *rand.Rand, n int) []simtypes.Account { accounts := simtypes.RandomAccounts(r, n) From fcebe7df031e254225d0e5fc865e51a76126482a Mon Sep 17 00:00:00 2001 From: Alexander Bezobchuk Date: Sat, 18 Jul 2020 19:13:25 -0400 Subject: [PATCH 3/3] Update x/bank/simulation/operations_test.go --- x/bank/simulation/operations_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/x/bank/simulation/operations_test.go b/x/bank/simulation/operations_test.go index e4c1eddfc36a..2718dde50092 100644 --- a/x/bank/simulation/operations_test.go +++ b/x/bank/simulation/operations_test.go @@ -5,7 +5,6 @@ import ( "testing" "github.com/stretchr/testify/suite" - abci "github.com/tendermint/tendermint/abci/types" "github.com/cosmos/cosmos-sdk/simapp"