Skip to content

Commit

Permalink
implement mock option for observer
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Feb 20, 2024
1 parent e5930be commit 926e12e
Show file tree
Hide file tree
Showing 14 changed files with 242 additions and 16 deletions.
2 changes: 1 addition & 1 deletion testutil/keeper/crosschain.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (
CrosschainNoMocks = CrosschainMockOptions{}
)

// CrosschainKeeper initializes a crosschain keeper for testing purposes with option to mock specific keepers
// CrosschainKeeperWithMocks initializes a crosschain keeper for testing purposes with option to mock specific keepers
func CrosschainKeeperWithMocks(
t testing.TB,
mockOptions CrosschainMockOptions,
Expand Down
2 changes: 1 addition & 1 deletion testutil/keeper/mocks/crosschain/account.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/crosschain/bank.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/crosschain/fungible.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/crosschain/observer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/crosschain/staking.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/fungible/account.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/fungible/bank.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/fungible/evm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/fungible/observer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions testutil/keeper/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mocks
import (
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"
fungibletypes "github.com/zeta-chain/zetacore/x/fungible/types"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
)

/**
Expand Down Expand Up @@ -57,3 +58,17 @@ type FungibleObserverKeeper interface {
type FungibleEVMKeeper interface {
fungibletypes.EVMKeeper
}

/**
* Observer Mocks
*/

//go:generate mockery --name ObserverStakingKeeper --filename staking.go --case underscore --output ./observer
type ObserverStakingKeeper interface {
observertypes.StakingKeeper
}

//go:generate mockery --name ObserverSlashingKeeper --filename slashing.go --case underscore --output ./observer
type ObserverSlashingKeeper interface {
observertypes.SlashingKeeper
}
53 changes: 53 additions & 0 deletions testutil/keeper/mocks/observer/slashing.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 91 additions & 0 deletions testutil/keeper/mocks/observer/staking.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 73 additions & 6 deletions testutil/keeper/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,36 @@ package keeper
import (
"testing"

slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
tmdb "github.com/tendermint/tm-db"
observermocks "github.com/zeta-chain/zetacore/testutil/keeper/mocks/observer"
"github.com/zeta-chain/zetacore/x/observer/keeper"
"github.com/zeta-chain/zetacore/x/observer/types"
)

// ObserverMockOptions represents options for instantiating an observer keeper with mocks
type ObserverMockOptions struct {
UseStakingMock bool
UseSlashingMock bool
}

var (
ObserverMocksAll = ObserverMockOptions{
UseStakingMock: true,
UseSlashingMock: true,
}
ObserverNoMocks = ObserverMockOptions{}
)

func initObserverKeeper(
cdc codec.Codec,
db *tmdb.MemDB,
Expand All @@ -40,8 +56,8 @@ func initObserverKeeper(
)
}

// ObserverKeeper instantiates an observer keeper for testing purposes
func ObserverKeeper(t testing.TB) (*keeper.Keeper, sdk.Context, SDKKeepers) {
// ObserverKeeperWithMocks instantiates an observer keeper for testing purposes with the option to mock specific keepers
func ObserverKeeperWithMocks(t testing.TB, mockOptions ObserverMockOptions) (*keeper.Keeper, sdk.Context, SDKKeepers) {
storeKey := sdk.NewKVStoreKey(types.StoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)

Expand All @@ -66,16 +82,67 @@ func ObserverKeeper(t testing.TB) (*keeper.Keeper, sdk.Context, SDKKeepers) {
// Add a proposer to the context
ctx = sdkKeepers.InitBlockProposer(t, ctx)

// Initialize mocks for mocked keepers
var stakingKeeper types.StakingKeeper = sdkKeepers.StakingKeeper
var slashingKeeper types.SlashingKeeper = sdkKeepers.SlashingKeeper
if mockOptions.UseStakingMock {
stakingKeeper = observermocks.NewObserverStakingKeeper(t)
}
if mockOptions.UseSlashingMock {
slashingKeeper = observermocks.NewObserverSlashingKeeper(t)
}

k := keeper.NewKeeper(
cdc,
storeKey,
memStoreKey,
sdkKeepers.ParamsKeeper.Subspace(types.ModuleName),
sdkKeepers.StakingKeeper,
sdkKeepers.SlashingKeeper,
stakingKeeper,
slashingKeeper,
)

k.SetParams(ctx, types.DefaultParams())

return k, ctx, sdkKeepers
}

// ObserverKeeper instantiates an observer keeper for testing purposes
func ObserverKeeper(t testing.TB) (*keeper.Keeper, sdk.Context, SDKKeepers) {
return ObserverKeeperWithMocks(t, ObserverNoMocks)
}

// GetObserverStakingMock returns a new observer staking keeper mock
func GetObserverStakingMock(t testing.TB, keeper *keeper.Keeper) *ObserverMockStakingKeeper {
k, ok := keeper.GetStakingKeeper().(*observermocks.ObserverStakingKeeper)
require.True(t, ok)
return &ObserverMockStakingKeeper{
ObserverStakingKeeper: k,
}
}

// GetObserverSlashingMock returns a new observer slashing keeper mock
func GetObserverSlashingMock(t testing.TB, keeper *keeper.Keeper) *ObserverMockSlashingKeeper {
k, ok := keeper.GetSlashingKeeper().(*observermocks.ObserverSlashingKeeper)
require.True(t, ok)
return &ObserverMockSlashingKeeper{
ObserverSlashingKeeper: k,
}
}

// ObserverMockStakingKeeper is a wrapper of the observer staking keeper mock that add methods to mock the GetValidator method
type ObserverMockStakingKeeper struct {
*observermocks.ObserverStakingKeeper
}

func (m *ObserverMockStakingKeeper) MockGetValidator(validator stakingtypes.Validator) {
m.On("GetValidator", mock.Anything, mock.Anything).Return(validator, true)
}

// ObserverMockSlashingKeeper is a wrapper of the observer slashing keeper mock that add methods to mock the IsTombstoned method
type ObserverMockSlashingKeeper struct {
*observermocks.ObserverSlashingKeeper
}

func (m *ObserverMockSlashingKeeper) MockIsTombstoned(isTombstoned bool) {
m.On("IsTombstoned", mock.Anything, mock.Anything).Return(isTombstoned)
}

0 comments on commit 926e12e

Please sign in to comment.