Merge PR #6769: x/distribution/simulation/genesis.go: add unit tests

This commit is contained in:
dauTT 2020-07-18 18:34:15 +02:00 committed by GitHub
parent 6b5733918b
commit 07a6bd4321
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 78 additions and 0 deletions

View File

@ -0,0 +1,78 @@
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/distribution/simulation"
"github.com/cosmos/cosmos-sdk/x/distribution/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 distrGenesis types.GenesisState
simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &distrGenesis)
dec1, _ := sdk.NewDecFromStr("0.170000000000000000")
dec2, _ := sdk.NewDecFromStr("0.010000000000000000")
dec3, _ := sdk.NewDecFromStr("0.210000000000000000")
require.Equal(t, dec1, distrGenesis.Params.BaseProposerReward)
require.Equal(t, dec2, distrGenesis.Params.BonusProposerReward)
require.Equal(t, dec3, distrGenesis.Params.CommunityTax)
require.Equal(t, true, distrGenesis.Params.WithdrawAddrEnabled)
require.Len(t, distrGenesis.DelegatorStartingInfos, 0)
require.Len(t, distrGenesis.DelegatorWithdrawInfos, 0)
require.Len(t, distrGenesis.ValidatorSlashEvents, 0)
}
// 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)
}
}