-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR #4918: refactor crisis tests
- Loading branch information
1 parent
e7b378d
commit 5aacf45
Showing
4 changed files
with
138 additions
and
99 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
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
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,33 @@ | ||
package keeper_test | ||
|
||
import ( | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
"github.com/tendermint/tendermint/libs/log" | ||
dbm "github.com/tendermint/tm-db" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec" | ||
"github.com/cosmos/cosmos-sdk/simapp" | ||
) | ||
|
||
func createTestApp() *simapp.SimApp { | ||
db := dbm.NewMemDB() | ||
app := simapp.NewSimApp(log.NewNopLogger(), db, nil, true, 5) | ||
// init chain must be called to stop deliverState from being nil | ||
genesisState := simapp.NewDefaultGenesisState() | ||
stateBytes, err := codec.MarshalJSONIndent(app.Codec(), genesisState) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Initialize the chain | ||
app.InitChain( | ||
abci.RequestInitChain{ | ||
Validators: []abci.ValidatorUpdate{}, | ||
AppStateBytes: stateBytes, | ||
}, | ||
) | ||
app.Commit() | ||
app.BeginBlock(abci.RequestBeginBlock{Header: abci.Header{Height: app.LastBlockHeight() + 1}}) | ||
|
||
return app | ||
} |
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 |
---|---|---|
@@ -1,56 +1,38 @@ | ||
package keeper | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/tendermint/tendermint/libs/log" | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/crisis/internal/types" | ||
"github.com/cosmos/cosmos-sdk/x/params" | ||
) | ||
|
||
func testPassingInvariant(_ sdk.Context) (string, bool) { | ||
return "", false | ||
} | ||
|
||
func testFailingInvariant(_ sdk.Context) (string, bool) { | ||
return "", true | ||
} | ||
|
||
func testKeeper(checkPeriod uint) Keeper { | ||
cdc := codec.New() | ||
paramsKeeper := params.NewKeeper( | ||
cdc, sdk.NewKVStoreKey(params.StoreKey), sdk.NewTransientStoreKey(params.TStoreKey), params.DefaultCodespace, | ||
) | ||
|
||
return NewKeeper(paramsKeeper.Subspace(types.DefaultParamspace), checkPeriod, nil, "test") | ||
} | ||
|
||
func TestLogger(t *testing.T) { | ||
k := testKeeper(5) | ||
app := createTestApp() | ||
|
||
ctx := sdk.Context{}.WithLogger(log.NewNopLogger()) | ||
require.Equal(t, ctx.Logger(), k.Logger(ctx)) | ||
ctx := app.NewContext(true, abci.Header{}) | ||
require.Equal(t, ctx.Logger(), app.CrisisKeeper.Logger(ctx)) | ||
} | ||
|
||
func TestInvariants(t *testing.T) { | ||
k := testKeeper(5) | ||
require.Equal(t, k.InvCheckPeriod(), uint(5)) | ||
app := createTestApp() | ||
require.Equal(t, app.CrisisKeeper.InvCheckPeriod(), uint(5)) | ||
|
||
k.RegisterRoute("testModule", "testRoute", testPassingInvariant) | ||
require.Len(t, k.Routes(), 1) | ||
// SimApp has 11 registered invariants | ||
orgInvRoutes := app.CrisisKeeper.Routes() | ||
app.CrisisKeeper.RegisterRoute("testModule", "testRoute", func(sdk.Context) (string, bool) { return "", false }) | ||
require.Equal(t, len(app.CrisisKeeper.Routes()), len(orgInvRoutes)+1) | ||
} | ||
|
||
func TestAssertInvariants(t *testing.T) { | ||
k := testKeeper(5) | ||
ctx := sdk.Context{}.WithLogger(log.NewNopLogger()) | ||
app := createTestApp() | ||
ctx := app.NewContext(true, abci.Header{}) | ||
|
||
k.RegisterRoute("testModule", "testRoute1", testPassingInvariant) | ||
require.NotPanics(t, func() { k.AssertInvariants(ctx) }) | ||
app.CrisisKeeper.RegisterRoute("testModule", "testRoute1", func(sdk.Context) (string, bool) { return "", false }) | ||
require.NotPanics(t, func() { app.CrisisKeeper.AssertInvariants(ctx) }) | ||
|
||
k.RegisterRoute("testModule", "testRoute2", testFailingInvariant) | ||
require.Panics(t, func() { k.AssertInvariants(ctx) }) | ||
app.CrisisKeeper.RegisterRoute("testModule", "testRoute2", func(sdk.Context) (string, bool) { return "", true }) | ||
require.Panics(t, func() { app.CrisisKeeper.AssertInvariants(ctx) }) | ||
} |