github.com/Finschia/finschia-sdk@v0.48.1/x/bank/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  	"github.com/Finschia/finschia-sdk/types/module"
    13  	simtypes "github.com/Finschia/finschia-sdk/types/simulation"
    14  	"github.com/Finschia/finschia-sdk/x/bank/simulation"
    15  	"github.com/Finschia/finschia-sdk/x/bank/types"
    16  )
    17  
    18  // TestRandomizedGenState tests the normal scenario of applying RandomizedGenState.
    19  // Abonormal scenarios are not tested here.
    20  func TestRandomizedGenState(t *testing.T) {
    21  	interfaceRegistry := codectypes.NewInterfaceRegistry()
    22  	cdc := codec.NewProtoCodec(interfaceRegistry)
    23  	s := rand.NewSource(1)
    24  	r := rand.New(s)
    25  
    26  	simState := module.SimulationState{
    27  		AppParams:    make(simtypes.AppParams),
    28  		Cdc:          cdc,
    29  		Rand:         r,
    30  		NumBonded:    3,
    31  		Accounts:     simtypes.RandomAccounts(r, 3),
    32  		InitialStake: 1000,
    33  		GenState:     make(map[string]json.RawMessage),
    34  	}
    35  
    36  	simulation.RandomizedGenState(&simState)
    37  
    38  	var bankGenesis types.GenesisState
    39  	simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &bankGenesis)
    40  
    41  	require.Equal(t, true, bankGenesis.Params.GetDefaultSendEnabled())
    42  	require.Len(t, bankGenesis.Params.GetSendEnabled(), 1)
    43  	require.Len(t, bankGenesis.Balances, 3)
    44  	require.Equal(t, "link1ghekyjucln7y67ntx7cf27m9dpuxxemnqk82wt", bankGenesis.Balances[2].GetAddress().String())
    45  	require.Equal(t, "1000stake", bankGenesis.Balances[2].GetCoins().String())
    46  	require.Equal(t, "6000stake", bankGenesis.Supply.String())
    47  }
    48  
    49  // TestRandomizedGenState tests abnormal scenarios of applying RandomizedGenState.
    50  func TestRandomizedGenState1(t *testing.T) {
    51  	interfaceRegistry := codectypes.NewInterfaceRegistry()
    52  	cdc := codec.NewProtoCodec(interfaceRegistry)
    53  
    54  	s := rand.NewSource(1)
    55  	r := rand.New(s)
    56  
    57  	// all these tests will panic
    58  	tests := []struct {
    59  		simState module.SimulationState
    60  		panicMsg string
    61  	}{
    62  		{ // panic => reason: incomplete initialization of the simState
    63  			module.SimulationState{}, "invalid memory address or nil pointer dereference"},
    64  		{ // panic => reason: incomplete initialization of the simState
    65  			module.SimulationState{
    66  				AppParams: make(simtypes.AppParams),
    67  				Cdc:       cdc,
    68  				Rand:      r,
    69  			}, "assignment to entry in nil map"},
    70  	}
    71  
    72  	for _, tt := range tests {
    73  		require.Panicsf(t, func() { simulation.RandomizedGenState(&tt.simState) }, tt.panicMsg)
    74  	}
    75  }