Skip to content

Commit

Permalink
Provider Unit Test Cleanup (#267)
Browse files Browse the repository at this point in the history
* dummies and keeper construction

* cleans

* dun works

* comment and progress save

* progess save

* finished keeper

* couple unit tests for prop order

* pr suggestions

* testify -> gomock

* small change

* operation order - small change

* remove unneeded assignments

* delete proposals after execution

* adjust tests

Co-authored-by: Jehan <[email protected]>
  • Loading branch information
shaspitz and jtremback authored Aug 16, 2022
1 parent 2554378 commit 1e4d887
Show file tree
Hide file tree
Showing 9 changed files with 1,279 additions and 156 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ require (
gopkg.in/yaml.v2 v2.4.0
)

require github.com/regen-network/cosmos-proto v0.3.1
require (
github.com/golang/mock v1.6.0
github.com/regen-network/cosmos-proto v0.3.1
)

require (
filippo.io/edwards25519 v1.0.0-beta.2 // indirect
Expand Down
1 change: 0 additions & 1 deletion testutil/keeper/ccv.go

This file was deleted.

765 changes: 765 additions & 0 deletions testutil/keeper/mocks.go

Large diffs are not rendered by default.

100 changes: 100 additions & 0 deletions testutil/keeper/unit_test_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package keeper

import (
"testing"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
providerkeeper "github.com/cosmos/interchain-security/x/ccv/provider/keeper"
"github.com/cosmos/interchain-security/x/ccv/types"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmdb "github.com/tendermint/tm-db"
)

// Constructs a keeper and context object for unit tests, backed by an in-memory db.
func GetProviderKeeperAndCtx(t testing.TB) (providerkeeper.Keeper, sdk.Context) {

cdc, storeKey, paramsSubspace, ctx := SetupInMemKeeper(t)

k := providerkeeper.NewKeeper(
cdc,
storeKey,
paramsSubspace,
capabilitykeeper.ScopedKeeper{},
&MockChannelKeeper{},
&MockPortKeeper{},
&MockConnectionKeeper{},
&MockClientKeeper{},
&MockStakingKeeper{},
&MockSlashingKeeper{},
&MockAccountKeeper{},
"",
)
return k, ctx
}

// Constructs a keeper for unit tests, backed by an in-memory db,
// with ability to pass mocked or otherwise manipulated parameters.
// Note: Use the dummy types defined in this file for keepers you don't wish to mock,
// and SetupInMemKeeper() for other parameters you don't wish to manipulate.
func GetProviderKeeperWithMocks(t testing.TB,
cdc *codec.ProtoCodec,
storeKey *storetypes.KVStoreKey,
paramsSubspace paramstypes.Subspace,
ctx sdk.Context,
capabilityKeeper capabilitykeeper.ScopedKeeper,
channelKeeper types.ChannelKeeper,
portKeeper types.PortKeeper,
connectionKeeper types.ConnectionKeeper,
clientKeeper types.ClientKeeper,
stakingKeeper types.StakingKeeper,
slashingKeeper types.SlashingKeeper,
accountKeeper types.AccountKeeper,
) providerkeeper.Keeper {

k := providerkeeper.NewKeeper(
cdc,
storeKey,
paramsSubspace,
capabilityKeeper,
channelKeeper,
portKeeper,
connectionKeeper,
clientKeeper,
stakingKeeper,
slashingKeeper,
accountKeeper,
"",
)
return k
}

func SetupInMemKeeper(t testing.TB) (*codec.ProtoCodec, *storetypes.KVStoreKey, paramstypes.Subspace, sdk.Context) {
storeKey := sdk.NewKVStoreKey(types.StoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)

db := tmdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db)
stateStore.MountStoreWithDB(storeKey, sdk.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(memStoreKey, sdk.StoreTypeMemory, nil)
require.NoError(t, stateStore.LoadLatestVersion())

registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)

paramsSubspace := paramstypes.NewSubspace(cdc,
codec.NewLegacyAmino(),
storeKey,
memStoreKey,
paramstypes.ModuleName,
)
ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
return cdc, storeKey, paramsSubspace, ctx
}
Loading

0 comments on commit 1e4d887

Please sign in to comment.