Skip to content

Commit

Permalink
x/ibc/simulation/genesis_test.go: add unit tests (#6712)
Browse files Browse the repository at this point in the history
Co-authored-by: Alessio Treglia <alessio@tendermint.com>
  • Loading branch information
dauTT and Alessio Treglia committed Jul 14, 2020
1 parent 8a62e1a commit 56ba2d8
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions x/ibc/simulation/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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/ibc/02-client/types"
host "github.com/cosmos/cosmos-sdk/x/ibc/24-host"
"github.com/cosmos/cosmos-sdk/x/ibc/simulation"
)

// 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)

// Make sure to register cdc.
// Otherwise RandomizedGenState will panic!
types.RegisterCodec(cdc)

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),
}

// Remark: the current RandomizedGenState function
// is actually not random as it does not utilize concretely the random value r.
// This tests will pass for any value of r.
simulation.RandomizedGenState(&simState)

var ibcGenesis types.GenesisState
simState.Cdc.MustUnmarshalJSON(simState.GenState[host.ModuleName], &ibcGenesis)

require.Len(t, ibcGenesis.Clients, 0)
require.Len(t, ibcGenesis.ClientsConsensus, 0)
require.False(t, ibcGenesis.CreateLocalhost)

// Note: ibcGenesis.ChannelGenesis is missing because the ChannelGenesis
// interface is not register in RegisterCodec. (Not sure if is a feature
// or a bug.)

}

// TestRandomizedGenState tests the execution of RandomizedGenState
// without registering the IBC client interfaces and types.
// We expect the test to panic.
func TestRandomizedGenState1(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),
}

require.Panicsf(t, func() { simulation.RandomizedGenState(&simState) }, "failed to marshal JSON: Unregistered interface exported.ClientState")

}

0 comments on commit 56ba2d8

Please sign in to comment.