-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add unit tests for genesis state checks, init, export and validate
- Loading branch information
Showing
2 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |