Skip to content

Commit

Permalink
Merge PR cosmos#6692: x/staking/simulation/genesis.go: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dauTT authored Jul 12, 2020
1 parent 3ab823c commit d6e0888
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions x/staking/simulation/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package simulation_test

import (
"encoding/json"
"math/rand"
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/staking/simulation"
"github.com/cosmos/cosmos-sdk/x/staking/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 stakingGenesis types.GenesisState
simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &stakingGenesis)

require.Equal(t, uint32(207), stakingGenesis.Params.MaxValidators)
require.Equal(t, uint32(7), stakingGenesis.Params.MaxEntries)
require.Equal(t, uint32(48), stakingGenesis.Params.HistoricalEntries)
require.Equal(t, "stake", stakingGenesis.Params.BondDenom)
require.Equal(t, float64(238280), stakingGenesis.Params.UnbondingTime.Seconds())
// check numbers of Delegations and Validators
require.Len(t, stakingGenesis.Delegations, 3)
require.Len(t, stakingGenesis.Validators, 3)
// check Delegations
require.Equal(t, "cosmos1tnh2q55v8wyygtt9srz5safamzdengsnqeycj3", stakingGenesis.Delegations[0].DelegatorAddress.String())
require.Equal(t, "cosmosvaloper1tnh2q55v8wyygtt9srz5safamzdengsn9dsd7z", stakingGenesis.Delegations[0].ValidatorAddress.String())
require.Equal(t, "1000.000000000000000000", stakingGenesis.Delegations[0].Shares.String())
// check validators
require.Equal(t, "cosmosvaloper1ghekyjucln7y67ntx7cf27m9dpuxxemnsvnaes", stakingGenesis.Validators[2].OperatorAddress.String())
require.Equal(t, "cosmosvalconspub1addwnpepqwr8k5g44urevkvz5ys2qjag0nnp6xkd2f8lejn5pw2rehkjt6ftv5d9nrp", stakingGenesis.Validators[2].ConsensusPubkey)
require.Equal(t, false, stakingGenesis.Validators[2].Jailed)
require.Equal(t, "Unbonded", stakingGenesis.Validators[2].Status.String())
require.Equal(t, "1000", stakingGenesis.Validators[2].Tokens.String())
require.Equal(t, "1000.000000000000000000", stakingGenesis.Validators[2].DelegatorShares.String())
require.Equal(t, "0.292059246265731326", stakingGenesis.Validators[2].Commission.CommissionRates.Rate.String())
require.Equal(t, "0.330000000000000000", stakingGenesis.Validators[2].Commission.CommissionRates.MaxRate.String())
require.Equal(t, "0.038337453731274481", stakingGenesis.Validators[2].Commission.CommissionRates.MaxChangeRate.String())
require.Equal(t, "1", stakingGenesis.Validators[2].MinSelfDelegation.String())
}

// 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,
}, "invalid memory address or nil pointer dereference"},
{
// panic => reason: numBonded != len(Accnounts)
module.SimulationState{
AppParams: make(simtypes.AppParams),
Cdc: cdc,
Rand: r,
NumBonded: 4,
Accounts: simtypes.RandomAccounts(r, 3),
InitialStake: 1000,
GenState: make(map[string]json.RawMessage),
}, "invalid memory address or nil pointer dereference"},
}

for _, tt := range tests {
require.Panicsf(t, func() { simulation.RandomizedGenState(&tt.simState) }, tt.panicMsg)
}
}

0 comments on commit d6e0888

Please sign in to comment.