From ebc3ad6843134a18cb5c1ef92a78eb0c7fb663b5 Mon Sep 17 00:00:00 2001 From: dauTT Date: Thu, 16 Jul 2020 11:42:06 +0200 Subject: [PATCH] x/evidence/simulation/genesis.go: add unit tests (#6740) Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- x/evidence/simulation/genesis_test.go | 66 +++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 x/evidence/simulation/genesis_test.go diff --git a/x/evidence/simulation/genesis_test.go b/x/evidence/simulation/genesis_test.go new file mode 100644 index 0000000000..aa1259b5d0 --- /dev/null +++ b/x/evidence/simulation/genesis_test.go @@ -0,0 +1,66 @@ +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/evidence/simulation" + "github.com/cosmos/cosmos-sdk/x/evidence/types" +) + +// TestRandomizedGenState tests the normal scenario of applying RandomizedGenState. +// Abonormal scenarios are not tested here. +func TestRandomizedGenState(t *testing.T) { + cdc := codec.New() + // Make sure to register cdc. + // Otherwise RandomizedGenState will panic! + types.RegisterCodec(cdc) + + 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 evidenceGenesis types.GenesisState + simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &evidenceGenesis) + + require.Len(t, evidenceGenesis.Evidence, 0) +} + +// TestRandomizedGenState tests the execution of RandomizedGenState +// without registering the evidence interfaces. +// We expect the test to panic. +func TestRandomizedGenState1(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), + } + + require.Panicsf(t, func() { simulation.RandomizedGenState(&simState) }, "failed to marshal JSON: Unregistered interface exported.Evidence") +}