github.com/cosmos/cosmos-sdk@v0.50.10/x/gov/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  	"gotest.tools/v3/assert"
    10  
    11  	sdkmath "cosmossdk.io/math"
    12  
    13  	"github.com/cosmos/cosmos-sdk/codec"
    14  	codectypes "github.com/cosmos/cosmos-sdk/codec/types"
    15  	sdk "github.com/cosmos/cosmos-sdk/types"
    16  	"github.com/cosmos/cosmos-sdk/types/module"
    17  	simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
    18  	"github.com/cosmos/cosmos-sdk/x/gov/simulation"
    19  	"github.com/cosmos/cosmos-sdk/x/gov/types"
    20  	v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
    21  )
    22  
    23  // TestRandomizedGenState tests the normal scenario of applying RandomizedGenState.
    24  // Abnormal scenarios are not tested here.
    25  func TestRandomizedGenState(t *testing.T) {
    26  	interfaceRegistry := codectypes.NewInterfaceRegistry()
    27  	cdc := codec.NewProtoCodec(interfaceRegistry)
    28  
    29  	s := rand.NewSource(1)
    30  	r := rand.New(s)
    31  
    32  	simState := module.SimulationState{
    33  		AppParams:    make(simtypes.AppParams),
    34  		Cdc:          cdc,
    35  		Rand:         r,
    36  		NumBonded:    3,
    37  		BondDenom:    sdk.DefaultBondDenom,
    38  		Accounts:     simtypes.RandomAccounts(r, 3),
    39  		InitialStake: sdkmath.NewInt(1000),
    40  		GenState:     make(map[string]json.RawMessage),
    41  	}
    42  
    43  	simulation.RandomizedGenState(&simState)
    44  
    45  	var govGenesis v1.GenesisState
    46  	simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &govGenesis)
    47  
    48  	const (
    49  		tallyQuorum             = "0.350000000000000000"
    50  		tallyThreshold          = "0.495000000000000000"
    51  		tallyExpeditedThreshold = "0.545000000000000000"
    52  		tallyVetoThreshold      = "0.327000000000000000"
    53  		minInitialDepositDec    = "0.880000000000000000"
    54  	)
    55  
    56  	assert.Equal(t, "272stake", govGenesis.Params.MinDeposit[0].String())
    57  	assert.Equal(t, "800stake", govGenesis.Params.ExpeditedMinDeposit[0].String())
    58  	assert.Equal(t, "41h11m36s", govGenesis.Params.MaxDepositPeriod.String())
    59  	assert.Equal(t, float64(283889), govGenesis.Params.VotingPeriod.Seconds())
    60  	assert.Equal(t, float64(123081), govGenesis.Params.ExpeditedVotingPeriod.Seconds())
    61  	assert.Equal(t, tallyQuorum, govGenesis.Params.Quorum)
    62  	assert.Equal(t, tallyThreshold, govGenesis.Params.Threshold)
    63  	assert.Equal(t, tallyExpeditedThreshold, govGenesis.Params.ExpeditedThreshold)
    64  	assert.Equal(t, tallyVetoThreshold, govGenesis.Params.VetoThreshold)
    65  	assert.Equal(t, uint64(0x28), govGenesis.StartingProposalId)
    66  	assert.DeepEqual(t, []*v1.Deposit{}, govGenesis.Deposits)
    67  	assert.DeepEqual(t, []*v1.Vote{}, govGenesis.Votes)
    68  	assert.DeepEqual(t, []*v1.Proposal{}, govGenesis.Proposals)
    69  }
    70  
    71  // TestRandomizedGenState tests abnormal scenarios of applying RandomizedGenState.
    72  func TestRandomizedGenState1(t *testing.T) {
    73  	interfaceRegistry := codectypes.NewInterfaceRegistry()
    74  	cdc := codec.NewProtoCodec(interfaceRegistry)
    75  
    76  	s := rand.NewSource(1)
    77  	r := rand.New(s)
    78  	// all these tests will panic
    79  	tests := []struct {
    80  		simState module.SimulationState
    81  		panicMsg string
    82  	}{
    83  		{ // panic => reason: incomplete initialization of the simState
    84  			module.SimulationState{}, "invalid memory address or nil pointer dereference"},
    85  		{ // panic => reason: incomplete initialization of the simState
    86  			module.SimulationState{
    87  				AppParams: make(simtypes.AppParams),
    88  				Cdc:       cdc,
    89  				Rand:      r,
    90  			}, "assignment to entry in nil map"},
    91  	}
    92  
    93  	for _, tt := range tests {
    94  		require.Panicsf(t, func() { simulation.RandomizedGenState(&tt.simState) }, tt.panicMsg)
    95  	}
    96  }