github.com/Finschia/finschia-sdk@v0.48.1/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  
    10  	"github.com/Finschia/finschia-sdk/codec"
    11  	codectypes "github.com/Finschia/finschia-sdk/codec/types"
    12  	sdk "github.com/Finschia/finschia-sdk/types"
    13  	"github.com/Finschia/finschia-sdk/types/module"
    14  	simtypes "github.com/Finschia/finschia-sdk/types/simulation"
    15  	"github.com/Finschia/finschia-sdk/x/gov/simulation"
    16  	"github.com/Finschia/finschia-sdk/x/gov/types"
    17  )
    18  
    19  // TestRandomizedGenState tests the normal scenario of applying RandomizedGenState.
    20  // Abonormal scenarios are not tested here.
    21  func TestRandomizedGenState(t *testing.T) {
    22  	interfaceRegistry := codectypes.NewInterfaceRegistry()
    23  	cdc := codec.NewProtoCodec(interfaceRegistry)
    24  
    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: 1000,
    35  		GenState:     make(map[string]json.RawMessage),
    36  	}
    37  
    38  	simulation.RandomizedGenState(&simState)
    39  
    40  	var govGenesis types.GenesisState
    41  	simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &govGenesis)
    42  
    43  	dec1, _ := sdk.NewDecFromStr("0.361000000000000000")
    44  	dec2, _ := sdk.NewDecFromStr("0.512000000000000000")
    45  	dec3, _ := sdk.NewDecFromStr("0.267000000000000000")
    46  
    47  	require.Equal(t, "905stake", govGenesis.DepositParams.MinDeposit.String())
    48  	require.Equal(t, "77h26m10s", govGenesis.DepositParams.MaxDepositPeriod.String())
    49  	require.Equal(t, float64(148296), govGenesis.VotingParams.VotingPeriod.Seconds())
    50  	require.Equal(t, dec1, govGenesis.TallyParams.Quorum)
    51  	require.Equal(t, dec2, govGenesis.TallyParams.Threshold)
    52  	require.Equal(t, dec3, govGenesis.TallyParams.VetoThreshold)
    53  	require.Equal(t, uint64(0x28), govGenesis.StartingProposalId)
    54  	require.Equal(t, types.Deposits{}, govGenesis.Deposits)
    55  	require.Equal(t, types.Votes{}, govGenesis.Votes)
    56  	require.Equal(t, types.Proposals{}, govGenesis.Proposals)
    57  }
    58  
    59  // TestRandomizedGenState tests abnormal scenarios of applying RandomizedGenState.
    60  func TestRandomizedGenState1(t *testing.T) {
    61  	interfaceRegistry := codectypes.NewInterfaceRegistry()
    62  	cdc := codec.NewProtoCodec(interfaceRegistry)
    63  
    64  	s := rand.NewSource(1)
    65  	r := rand.New(s)
    66  	// all these tests will panic
    67  	tests := []struct {
    68  		simState module.SimulationState
    69  		panicMsg string
    70  	}{
    71  		{ // panic => reason: incomplete initialization of the simState
    72  			module.SimulationState{}, "invalid memory address or nil pointer dereference"},
    73  		{ // panic => reason: incomplete initialization of the simState
    74  			module.SimulationState{
    75  				AppParams: make(simtypes.AppParams),
    76  				Cdc:       cdc,
    77  				Rand:      r,
    78  			}, "assignment to entry in nil map"},
    79  	}
    80  
    81  	for _, tt := range tests {
    82  		require.Panicsf(t, func() { simulation.RandomizedGenState(&tt.simState) }, tt.panicMsg)
    83  	}
    84  }