Skip to content

Commit

Permalink
add unit tests for genesis state checks, init, export and validate
Browse files Browse the repository at this point in the history
  • Loading branch information
Anmol1696 committed Aug 28, 2023
1 parent 7954ec1 commit 0ccd348
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 0 deletions.
66 changes: 66 additions & 0 deletions x/meshsecurity/keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package keeper

import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"

"github.com/osmosis-labs/mesh-security-sdk/x/meshsecurity/types"
)

func TestInitGenesis(t *testing.T) {
specs := map[string]struct {
state types.GenesisState
expErr bool
}{
"custom param, should pass": {
state: types.GenesisState{
Params: types.Params{
TotalContractsMaxCap: sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(15_000_000_000)),
EpochLength: 2_000,
MaxGasEndBlocker: 600_000,
},
},
expErr: false,
},
"custom small value param, should pass": {
state: types.GenesisState{
Params: types.Params{
TotalContractsMaxCap: sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(1_000_000)),
EpochLength: 20,
MaxGasEndBlocker: 10_000,
},
},
expErr: false,
},
}

for name, spec := range specs {
t.Run(name, func(t *testing.T) {
pCtx, keepers := CreateDefaultTestInput(t)
k := keepers.MeshKeeper

k.InitGenesis(pCtx, spec.state)

p := k.GetParams(pCtx)
assert.Equal(t, spec.state.Params.MaxGasEndBlocker, p.MaxGasEndBlocker)
assert.Equal(t, spec.state.Params.EpochLength, p.EpochLength)
assert.Equal(t, spec.state.Params.TotalContractsMaxCap, p.TotalContractsMaxCap)
})
}
}

func TestExportGenesis(t *testing.T) {
pCtx, keepers := CreateDefaultTestInput(t)
k := keepers.MeshKeeper
params := types.DefaultParams(sdk.DefaultBondDenom)

err := k.SetParams(pCtx, params)
require.NoError(t, err)

exported := k.ExportGenesis(pCtx)
assert.Equal(t, params.MaxGasEndBlocker, exported.Params.MaxGasEndBlocker)
assert.Equal(t, params.EpochLength, exported.Params.EpochLength)
assert.Equal(t, params.TotalContractsMaxCap, exported.Params.TotalContractsMaxCap)cd
90 changes: 90 additions & 0 deletions x/meshsecurity/types/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package types

import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"
"testing"
)

func TestValidateGenesis(t *testing.T) {
specs := map[string]struct {
state GenesisState
expErr bool
}{
"default params": {
state: *DefaultGenesisState(sdk.DefaultBondDenom),
expErr: false,
},
"custom param, should pass": {
state: GenesisState{
Params: Params{
TotalContractsMaxCap: sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(15_000_000_000)),
EpochLength: 2_000,
MaxGasEndBlocker: 600_000,
},
},
expErr: false,
},
"custom small value param, should pass": {
state: GenesisState{
Params: Params{
TotalContractsMaxCap: sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(1_000_000)),
EpochLength: 20,
MaxGasEndBlocker: 10_000,
},
},
expErr: false,
},
"invalid epoch length, should fail": {
state: GenesisState{
Params: Params{
TotalContractsMaxCap: sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(15_000_000_000)),
EpochLength: 0,
MaxGasEndBlocker: 600_000,
},
},
expErr: true,
},
"invalid max gas length, should fail": {
state: GenesisState{
Params: Params{
TotalContractsMaxCap: sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(15_000_000_000)),
EpochLength: 10,
MaxGasEndBlocker: 0,
},
},
expErr: true,
},
"invalid max cap coin denom, should fail": {
state: GenesisState{
Params: Params{
TotalContractsMaxCap: sdk.Coin{Denom: "invalid denom test", Amount: math.Int{}},
EpochLength: 10,
MaxGasEndBlocker: 0,
},
},
expErr: true,
},
"invalid max cap coin amount, should fail": {
state: GenesisState{
Params: Params{
TotalContractsMaxCap: sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: math.NewInt(-100)},
EpochLength: 10,
MaxGasEndBlocker: 0,
},
},
expErr: true,
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
err := ValidateGenesis(&spec.state)
if spec.expErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}

0 comments on commit 0ccd348

Please sign in to comment.