x/gov/simulation/genesis.go: add unit tests (#6739)
Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
parent
de08dc4288
commit
ffff6a21cd
80
x/gov/simulation/genesis_test.go
Normal file
80
x/gov/simulation/genesis_test.go
Normal file
@ -0,0 +1,80 @@
|
||||
package simulation_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math/rand"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/types/module"
|
||||
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
||||
"github.com/cosmos/cosmos-sdk/x/gov/simulation"
|
||||
"github.com/cosmos/cosmos-sdk/x/gov/types"
|
||||
)
|
||||
|
||||
// TestRandomizedGenState tests the normal scenario of applying RandomizedGenState.
|
||||
// Abonormal scenarios are not tested here.
|
||||
func TestRandomizedGenState(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),
|
||||
}
|
||||
|
||||
simulation.RandomizedGenState(&simState)
|
||||
|
||||
var govGenesis types.GenesisState
|
||||
simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &govGenesis)
|
||||
|
||||
dec1, _ := sdk.NewDecFromStr("0.361000000000000000")
|
||||
dec2, _ := sdk.NewDecFromStr("0.512000000000000000")
|
||||
dec3, _ := sdk.NewDecFromStr("0.267000000000000000")
|
||||
|
||||
require.Equal(t, "905stake", govGenesis.DepositParams.MinDeposit.String())
|
||||
require.Equal(t, "77h26m10s", govGenesis.DepositParams.MaxDepositPeriod.String())
|
||||
require.Equal(t, float64(148296), govGenesis.VotingParams.VotingPeriod.Seconds())
|
||||
require.Equal(t, dec1, govGenesis.TallyParams.Quorum)
|
||||
require.Equal(t, dec2, govGenesis.TallyParams.Threshold)
|
||||
require.Equal(t, dec3, govGenesis.TallyParams.Veto)
|
||||
require.Equal(t, uint64(0x28), govGenesis.StartingProposalID)
|
||||
require.Equal(t, types.Deposits(nil), govGenesis.Deposits)
|
||||
require.Equal(t, types.Votes(nil), govGenesis.Votes)
|
||||
require.Equal(t, types.Proposals(nil), govGenesis.Proposals)
|
||||
}
|
||||
|
||||
// TestRandomizedGenState tests abnormal scenarios of applying RandomizedGenState.
|
||||
func TestRandomizedGenState1(t *testing.T) {
|
||||
cdc := codec.New()
|
||||
|
||||
s := rand.NewSource(1)
|
||||
r := rand.New(s)
|
||||
// all these tests will panic
|
||||
tests := []struct {
|
||||
simState module.SimulationState
|
||||
panicMsg string
|
||||
}{
|
||||
{ // panic => reason: incomplete initialization of the simState
|
||||
module.SimulationState{}, "invalid memory address or nil pointer dereference"},
|
||||
{ // panic => reason: incomplete initialization of the simState
|
||||
module.SimulationState{
|
||||
AppParams: make(simtypes.AppParams),
|
||||
Cdc: cdc,
|
||||
Rand: r,
|
||||
}, "assignment to entry in nil map"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
require.Panicsf(t, func() { simulation.RandomizedGenState(&tt.simState) }, tt.panicMsg)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user