From 9262319caf37472d47e2d314edaeada90d716d5c Mon Sep 17 00:00:00 2001 From: dauTT Date: Sat, 18 Jul 2020 18:53:39 +0200 Subject: [PATCH] Merge PR #6772: x/bank/simulation/genesis.go: add unit tests --- x/bank/simulation/genesis_test.go | 72 +++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 x/bank/simulation/genesis_test.go diff --git a/x/bank/simulation/genesis_test.go b/x/bank/simulation/genesis_test.go new file mode 100644 index 0000000000..8bc2d8d442 --- /dev/null +++ b/x/bank/simulation/genesis_test.go @@ -0,0 +1,72 @@ +package simulation_test + +import ( + "encoding/json" + "math/rand" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/types/module" + simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + "github.com/cosmos/cosmos-sdk/x/bank/simulation" + "github.com/cosmos/cosmos-sdk/x/bank/types" +) + +// TestRandomizedGenState tests the normal scenario of applying RandomizedGenState. +// Abonormal scenarios are not tested here. +func TestRandomizedGenState(t *testing.T) { + cdc := codec.New() + s := rand.NewSource(1) + r := rand.New(s) + + simState := module.SimulationState{ + AppParams: make(simtypes.AppParams), + Cdc: cdc, + Rand: r, + NumBonded: 3, + Accounts: simtypes.RandomAccounts(r, 3), + InitialStake: 1000, + GenState: make(map[string]json.RawMessage), + } + + simulation.RandomizedGenState(&simState) + + var bankGenesis types.GenesisState + simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &bankGenesis) + + require.Equal(t, true, bankGenesis.Params.GetDefaultSendEnabled()) + require.Len(t, bankGenesis.Params.GetSendEnabled(), 1) + require.Len(t, bankGenesis.Balances, 3) + require.Equal(t, "cosmos1ghekyjucln7y67ntx7cf27m9dpuxxemn4c8g4r", bankGenesis.Balances[2].GetAddress().String()) + require.Equal(t, "1000stake", bankGenesis.Balances[2].GetCoins().String()) + require.Equal(t, "6000stake", bankGenesis.Supply.String()) +} + +// TestRandomizedGenState tests abnormal scenarios of applying RandomizedGenState. +func TestRandomizedGenState1(t *testing.T) { + cdc := codec.New() + + s := rand.NewSource(1) + r := rand.New(s) + + // all these tests will panic + tests := []struct { + simState module.SimulationState + panicMsg string + }{ + { // panic => reason: incomplete initialization of the simState + module.SimulationState{}, "invalid memory address or nil pointer dereference"}, + { // panic => reason: incomplete initialization of the simState + module.SimulationState{ + AppParams: make(simtypes.AppParams), + Cdc: cdc, + Rand: r, + }, "assignment to entry in nil map"}, + } + + for _, tt := range tests { + require.Panicsf(t, func() { simulation.RandomizedGenState(&tt.simState) }, tt.panicMsg) + } +}