Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate x/evidence to mocks #12749

Merged
merged 20 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
44afaed
refactor: use mocks for x/evidence keeper tests
JeancarloBarrios Jul 18, 2022
78626e8
generate code
JeancarloBarrios Jul 18, 2022
4384f8e
testing
JeancarloBarrios Jul 20, 2022
9036f93
Merge branch 'main' into JeancarloBarrios/migrate-evidence-to-mocks
JeancarloBarrios Jul 21, 2022
58106f6
Merge branch 'main' into JeancarloBarrios/migrate-evidence-to-mocks
JeancarloBarrios Jul 25, 2022
3dd30d9
keeper_test
JeancarloBarrios Jul 27, 2022
b60b791
Merge branch 'main' into JeancarloBarrios/migrate-evidence-to-mocks
JeancarloBarrios Jul 27, 2022
93fff10
pre merge
JeancarloBarrios Jul 28, 2022
852e721
Merge branch 'main' into JeancarloBarrios/migrate-evidence-to-mocks
JeancarloBarrios Jul 28, 2022
49eaf40
kept integration test on infraction
JeancarloBarrios Jul 28, 2022
70d9b49
Merge branch 'main' into JeancarloBarrios/migrate-evidence-to-mocks
JeancarloBarrios Jul 28, 2022
2163400
Merge branch 'main' into JeancarloBarrios/migrate-evidence-to-mocks
JeancarloBarrios Aug 1, 2022
c43abed
minor interface fixes
JeancarloBarrios Aug 1, 2022
497db0b
m
JeancarloBarrios Aug 1, 2022
4bf778e
reset accidental modification
JeancarloBarrios Aug 1, 2022
bd56758
remove of unsessesary setup
JeancarloBarrios Aug 1, 2022
ac8ab71
Merge branch 'main' into JeancarloBarrios/migrate-evidence-to-mocks
JeancarloBarrios Aug 1, 2022
035402d
Merge branch 'main' into JeancarloBarrios/migrate-evidence-to-mocks
JeancarloBarrios Aug 1, 2022
e2d5c64
remove unesesarry code
JeancarloBarrios Aug 1, 2022
4d67983
Merge branch 'main' into JeancarloBarrios/migrate-evidence-to-mocks
JeancarloBarrios Aug 1, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion scripts/mockgen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ $mockgen_cmd -source=x/params/proposal_handler_test.go -package testutil -destin
$mockgen_cmd -source=x/crisis/types/expected_keepers.go -package testutil -destination x/crisis/testutil/expected_keepers_mocks.go
$mockgen_cmd -source=x/auth/types/expected_keepers.go -package testutil -destination x/auth/testutil/expected_keepers_mocks.go
$mockgen_cmd -source=x/auth/ante/expected_keepers.go -package testutil -destination x/auth/ante/testutil/expected_keepers_mocks.go
$mockgen_cmd -source=x/bank/types/expected_keepers.go -package testutil -destination x/bank/testutil/expected_keepers_mocks.go
$mockgen_cmd -source=x/bank/types/expected_keepers.go -package testutil -destination x/bank/testutil/expected_keepers_mocks.go
$mockgen_cmd -source=x/evidence/types/expected_keepers.go -package testutil -destination x/evidence/testutil/expected_keepers_mocks.go
2 changes: 1 addition & 1 deletion x/evidence/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (suite *KeeperTestSuite) TestQueryEvidence() {
true,
func(res *types.QueryEvidenceResponse) {
var evi exported.Evidence
err := suite.interfaceRegistry.UnpackAny(res.Evidence, &evi)
err := suite.encCfg.InterfaceRegistry.UnpackAny(res.Evidence, &evi)
suite.Require().NoError(err)
suite.Require().NotNil(evi)
suite.Require().Equal(evi, evidence[0])
Expand Down
72 changes: 68 additions & 4 deletions x/evidence/keeper/infraction_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,68 @@
package keeper_test

import (
"time"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/evidence/keeper"
"github.com/cosmos/cosmos-sdk/x/evidence/testutil"
"github.com/cosmos/cosmos-sdk/x/evidence/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"time"
)

func (suite *KeeperTestSuite) TestHandleDoubleSign() {
type InfractionTestSuite struct {
suite.Suite
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not exactly sure, why use the mixing of mock test and integration test?
For example, why are you using simtestuitl instead of KeeperTestSuite?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this seems to be more like an integration test, and logic is a bit critical to just mock, it felt more like I was testing mock expect than anything else. We discuss it a bit and for now we left it like that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, we don't use

func (suite *KeeperTestSuite) populateValidators(ctx sdk.Context) {
anywhere

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true sorry I missed that thanks!!!


ctx sdk.Context
app *runtime.App

evidenceKeeper keeper.Keeper
bankKeeper bankkeeper.Keeper
accountKeeper authkeeper.AccountKeeper
slashingKeeper slashingkeeper.Keeper
stakingKeeper *stakingkeeper.Keeper
interfaceRegistry codectypes.InterfaceRegistry

queryClient types.QueryClient
}

func (suite *InfractionTestSuite) SetupTest() {
var (
evidenceKeeper keeper.Keeper
)

app, err := simtestutil.Setup(testutil.AppConfig,
&evidenceKeeper,
&suite.interfaceRegistry,
&suite.accountKeeper,
&suite.bankKeeper,
&suite.slashingKeeper,
&suite.stakingKeeper,
)
require.NoError(suite.T(), err)

router := types.NewRouter()
router = router.AddRoute(types.RouteEquivocation, testEquivocationHandler(evidenceKeeper))
evidenceKeeper.SetRouter(router)

suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{Height: 1})
suite.app = app

suite.evidenceKeeper = evidenceKeeper
}

func (suite *InfractionTestSuite) TestHandleDoubleSign() {
ctx := suite.ctx.WithIsCheckTx(false).WithBlockHeight(1)
suite.populateValidators(ctx)

Expand Down Expand Up @@ -75,7 +128,7 @@ func (suite *KeeperTestSuite) TestHandleDoubleSign() {
suite.Len(evidences, 1)
}

func (suite *KeeperTestSuite) TestHandleDoubleSign_TooOld() {
func (suite *InfractionTestSuite) TestHandleDoubleSign_TooOld() {
ctx := suite.ctx.WithIsCheckTx(false).WithBlockHeight(1).WithBlockTime(time.Now())
suite.populateValidators(ctx)

Expand Down Expand Up @@ -111,3 +164,14 @@ func (suite *KeeperTestSuite) TestHandleDoubleSign_TooOld() {
suite.False(suite.stakingKeeper.Validator(ctx, operatorAddr).IsJailed())
suite.False(suite.slashingKeeper.IsTombstoned(ctx, sdk.ConsAddress(val.Address())))
}

func (suite *InfractionTestSuite) populateValidators(ctx sdk.Context) {
// add accounts and set total supply
totalSupplyAmt := initAmt.MulRaw(int64(len(valAddresses)))
totalSupply := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, totalSupplyAmt))
suite.NoError(suite.bankKeeper.MintCoins(ctx, minttypes.ModuleName, totalSupply))

for _, addr := range valAddresses {
suite.NoError(suite.bankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, (sdk.AccAddress)(addr), initCoins))
}
}
82 changes: 40 additions & 42 deletions x/evidence/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,23 @@ package keeper_test
import (
"encoding/hex"
"fmt"
"time"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/cosmos/cosmos-sdk/testutil"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/evidence"
evidencetestutil "github.com/cosmos/cosmos-sdk/x/evidence/testutil"
"github.com/golang/mock/gomock"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"time"

"github.com/cosmos/cosmos-sdk/baseapp"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/runtime"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/evidence/exported"
"github.com/cosmos/cosmos-sdk/x/evidence/keeper"
"github.com/cosmos/cosmos-sdk/x/evidence/testutil"
"github.com/cosmos/cosmos-sdk/x/evidence/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/stretchr/testify/suite"
)

var (
Expand Down Expand Up @@ -80,49 +74,53 @@ type KeeperTestSuite struct {

ctx sdk.Context
querier sdk.Querier
app *runtime.App

evidenceKeeper keeper.Keeper
bankKeeper bankkeeper.Keeper
accountKeeper authkeeper.AccountKeeper
slashingKeeper slashingkeeper.Keeper
stakingKeeper *stakingkeeper.Keeper
interfaceRegistry codectypes.InterfaceRegistry

queryClient types.QueryClient
evidenceKeeper keeper.Keeper
bankKeeper *evidencetestutil.MockBankKeeper
accountKeeper *evidencetestutil.MockAccountKeeper
slashingKeeper *evidencetestutil.MockSlashingKeeper
stakingKeeper *evidencetestutil.MockStakingKeeper
queryClient types.QueryClient
encCfg moduletestutil.TestEncodingConfig
}

func (suite *KeeperTestSuite) SetupTest() {
var (
evidenceKeeper keeper.Keeper
encCfg := moduletestutil.MakeTestEncodingConfig(evidence.AppModuleBasic{})
key := sdk.NewKVStoreKey(types.StoreKey)
tkey := sdk.NewTransientStoreKey("evidence_transient_store")
testCtx := testutil.DefaultContext(key, tkey)
suite.ctx = testCtx

ctrl := gomock.NewController(suite.T())

stakingKeeper := evidencetestutil.NewMockStakingKeeper(ctrl)
slashingKeeper := evidencetestutil.NewMockSlashingKeeper(ctrl)
accountKeeper := evidencetestutil.NewMockAccountKeeper(ctrl)
bankKeeper := evidencetestutil.NewMockBankKeeper(ctrl)

evidenceKeeper := keeper.NewKeeper(
encCfg.Codec,
key,
stakingKeeper,
slashingKeeper,
)

app, err := simtestutil.Setup(testutil.AppConfig,
&evidenceKeeper,
&suite.interfaceRegistry,
&suite.accountKeeper,
&suite.bankKeeper,
&suite.slashingKeeper,
&suite.stakingKeeper,
)
require.NoError(suite.T(), err)
suite.stakingKeeper = stakingKeeper
suite.slashingKeeper = slashingKeeper
suite.bankKeeper = bankKeeper

router := types.NewRouter()
router = router.AddRoute(types.RouteEquivocation, testEquivocationHandler(evidenceKeeper))
evidenceKeeper.SetRouter(router)
suite.ctx = testCtx.WithBlockHeader(tmproto.Header{Height: 1})
suite.encCfg = moduletestutil.MakeTestEncodingConfig(evidence.AppModuleBasic{})

suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{Height: 1})
suite.app = app

for i, addr := range valAddresses {
addr := sdk.AccAddress(addr)
suite.accountKeeper.SetAccount(suite.ctx, authtypes.NewBaseAccount(addr, pubkeys[i], uint64(i), 0))
}
suite.accountKeeper = accountKeeper

queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.interfaceRegistry)
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.encCfg.InterfaceRegistry)
types.RegisterQueryServer(queryHelper, evidenceKeeper)
suite.queryClient = types.NewQueryClient(queryHelper)
suite.evidenceKeeper = evidenceKeeper
suite.evidenceKeeper = *evidenceKeeper
}

func (suite *KeeperTestSuite) populateEvidence(ctx sdk.Context, numEvidence int) []exported.Evidence {
Expand Down
Loading