github.com/cosmos/cosmos-sdk@v0.50.10/x/distribution/simulation/genesis_test.go (about)

     1  package simulation_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"math/rand"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	sdkmath "cosmossdk.io/math"
    11  
    12  	"github.com/cosmos/cosmos-sdk/codec"
    13  	codectypes "github.com/cosmos/cosmos-sdk/codec/types"
    14  	"github.com/cosmos/cosmos-sdk/types/module"
    15  	simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
    16  	"github.com/cosmos/cosmos-sdk/x/distribution/simulation"
    17  	"github.com/cosmos/cosmos-sdk/x/distribution/types"
    18  )
    19  
    20  // TestRandomizedGenState tests the normal scenario of applying RandomizedGenState.
    21  // Abonormal scenarios are not tested here.
    22  func TestRandomizedGenState(t *testing.T) {
    23  	interfaceRegistry := codectypes.NewInterfaceRegistry()
    24  	cdc := codec.NewProtoCodec(interfaceRegistry)
    25  	s := rand.NewSource(1)
    26  	r := rand.New(s)
    27  
    28  	simState := module.SimulationState{
    29  		AppParams:    make(simtypes.AppParams),
    30  		Cdc:          cdc,
    31  		Rand:         r,
    32  		NumBonded:    3,
    33  		Accounts:     simtypes.RandomAccounts(r, 3),
    34  		InitialStake: sdkmath.NewInt(1000),
    35  		GenState:     make(map[string]json.RawMessage),
    36  	}
    37  
    38  	simulation.RandomizedGenState(&simState)
    39  
    40  	var distrGenesis types.GenesisState
    41  	simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &distrGenesis)
    42  
    43  	dec1, _ := sdkmath.LegacyNewDecFromStr("0.210000000000000000")
    44  
    45  	require.Equal(t, dec1, distrGenesis.Params.CommunityTax)
    46  	require.Equal(t, true, distrGenesis.Params.WithdrawAddrEnabled)
    47  	require.Len(t, distrGenesis.DelegatorStartingInfos, 0)
    48  	require.Len(t, distrGenesis.DelegatorWithdrawInfos, 0)
    49  	require.Len(t, distrGenesis.ValidatorSlashEvents, 0)
    50  }
    51  
    52  // TestRandomizedGenState tests abnormal scenarios of applying RandomizedGenState.
    53  func TestRandomizedGenState1(t *testing.T) {
    54  	interfaceRegistry := codectypes.NewInterfaceRegistry()
    55  	cdc := codec.NewProtoCodec(interfaceRegistry)
    56  
    57  	s := rand.NewSource(1)
    58  	r := rand.New(s)
    59  
    60  	// all these tests will panic
    61  	tests := []struct {
    62  		simState module.SimulationState
    63  		panicMsg string
    64  	}{
    65  		{ // panic => reason: incomplete initialization of the simState
    66  			module.SimulationState{}, "invalid memory address or nil pointer dereference"},
    67  		{ // panic => reason: incomplete initialization of the simState
    68  			module.SimulationState{
    69  				AppParams: make(simtypes.AppParams),
    70  				Cdc:       cdc,
    71  				Rand:      r,
    72  			}, "assignment to entry in nil map"},
    73  	}
    74  
    75  	for _, tt := range tests {
    76  		require.Panicsf(t, func() { simulation.RandomizedGenState(&tt.simState) }, tt.panicMsg)
    77  	}
    78  }