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: Use mocks for x/nft testing #12407

Merged
merged 18 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
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
11 changes: 1 addition & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,9 @@ cosmovisor:

.PHONY: build build-linux cosmovisor

mockgen_cmd=go run github.com/golang/mock/mockgen

mocks: $(MOCKS_DIR)
$(mockgen_cmd) -source=client/account_retriever.go -package mocks -destination tests/mocks/account_retriever.go
$(mockgen_cmd) -package mocks -destination tests/mocks/tendermint_tm_db_DB.go github.com/tendermint/tm-db DB
$(mockgen_cmd) -source db/types.go -package mocks -destination tests/mocks/db/types.go
$(mockgen_cmd) -source=types/module/module.go -package mocks -destination tests/mocks/types_module_module.go
$(mockgen_cmd) -source=types/invariant.go -package mocks -destination tests/mocks/types_invariant.go
$(mockgen_cmd) -source=types/router.go -package mocks -destination tests/mocks/types_router.go
$(mockgen_cmd) -package mocks -destination tests/mocks/grpc_server.go github.com/gogo/protobuf/grpc Server
$(mockgen_cmd) -package mocks -destination tests/mocks/tendermint_tendermint_libs_log_DB.go github.com/tendermint/tendermint/libs/log Logger
$(mockgen_cmd) -source=orm/model/ormtable/hooks.go -package ormmocks -destination orm/testing/ormmocks/hooks.go
sh ./scripts/mockgen.sh
.PHONY: mocks

$(MOCKS_DIR):
Expand Down
13 changes: 13 additions & 0 deletions scripts/mockgen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

mockgen_cmd="go run github.com/golang/mock/mockgen"
$mockgen_cmd -source=client/account_retriever.go -package mocks -destination tests/mocks/account_retriever.go
$mockgen_cmd -package mocks -destination tests/mocks/tendermint_tm_db_DB.go github.com/tendermint/tm-db DB
$mockgen_cmd -source db/types.go -package mocks -destination tests/mocks/db/types.go
$mockgen_cmd -source=types/module/module.go -package mocks -destination tests/mocks/types_module_module.go
$mockgen_cmd -source=types/invariant.go -package mocks -destination tests/mocks/types_invariant.go
$mockgen_cmd -source=types/router.go -package mocks -destination tests/mocks/types_router.go
$mockgen_cmd -package mocks -destination tests/mocks/grpc_server.go github.com/gogo/protobuf/grpc Server
$mockgen_cmd -package mocks -destination tests/mocks/tendermint_tendermint_libs_log_DB.go github.com/tendermint/tendermint/libs/log Logger
$mockgen_cmd -source=orm/model/ormtable/hooks.go -package ormmocks -destination orm/testing/ormmocks/hooks.go
$mockgen_cmd -source=x/nft/expected_keepers.go -package testutil -destination x/nft/testutil/expected_keepers_mocks.go
22 changes: 22 additions & 0 deletions testutil/context.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package testutil

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"
Expand All @@ -24,3 +27,22 @@ func DefaultContext(key storetypes.StoreKey, tkey storetypes.StoreKey) sdk.Conte

return ctx
}

type TestContext struct {
Ctx sdk.Context
DB *dbm.MemDB
CMS store.CommitMultiStore
}

func DefaultContextWithDB(t *testing.T, key storetypes.StoreKey, tkey storetypes.StoreKey) TestContext {
db := dbm.NewMemDB()
cms := store.NewCommitMultiStore(db)
cms.MountStoreWithDB(key, storetypes.StoreTypeIAVL, db)
cms.MountStoreWithDB(tkey, storetypes.StoreTypeTransient, db)
err := cms.LoadLatestVersion()
assert.NoError(t, err)

ctx := sdk.NewContext(cms, tmproto.Header{}, false, log.NewNopLogger())

return TestContext{ctx, db, cms}
}
51 changes: 25 additions & 26 deletions x/nft/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package keeper_test
import (
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"
tmtime "github.com/tendermint/tendermint/libs/time"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/cosmos/cosmos-sdk/baseapp"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/testutil"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/nft"
"github.com/cosmos/cosmos-sdk/x/nft/keeper"
"github.com/cosmos/cosmos-sdk/x/nft/testutil"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/nft/module"
nfttestutil "github.com/cosmos/cosmos-sdk/x/nft/testutil"
)

const (
Expand All @@ -37,34 +38,32 @@ type TestSuite struct {
addrs []sdk.AccAddress
queryClient nft.QueryClient
nftKeeper keeper.Keeper

encCfg moduletestutil.TestEncodingConfig
}

func (s *TestSuite) SetupTest() {
var (
interfaceRegistry codectypes.InterfaceRegistry
bankKeeper bankkeeper.Keeper
stakingKeeper *stakingkeeper.Keeper
nftKeeper keeper.Keeper
)

app, err := simtestutil.Setup(
testutil.AppConfig,
&interfaceRegistry,
&nftKeeper,
&bankKeeper,
&stakingKeeper,
)
s.Require().NoError(err)

ctx := app.BaseApp.NewContext(false, tmproto.Header{})
ctx = ctx.WithBlockHeader(tmproto.Header{Time: tmtime.Now()})
queryHelper := baseapp.NewQueryServerTestHelper(ctx, interfaceRegistry)
// suite setup
s.addrs = simtestutil.CreateIncrementalAccounts(3)
s.encCfg = moduletestutil.MakeTestEncodingConfig(module.AppModuleBasic{})

key := sdk.NewKVStoreKey(nft.StoreKey)
testCtx := testutil.DefaultContextWithDB(s.T(), key, sdk.NewTransientStoreKey("transient_test"))
ctx := testCtx.Ctx.WithBlockHeader(tmproto.Header{Time: tmtime.Now()})

// gomock initializations
ctrl := gomock.NewController(s.T())
accountKeeper := nfttestutil.NewMockAccountKeeper(ctrl)
bankKeeper := nfttestutil.NewMockBankKeeper(ctrl)
accountKeeper.EXPECT().GetModuleAddress("nft").Return(s.addrs[0]).AnyTimes()

nftKeeper := keeper.NewKeeper(key, s.encCfg.Codec, accountKeeper, bankKeeper)
queryHelper := baseapp.NewQueryServerTestHelper(ctx, s.encCfg.InterfaceRegistry)
nft.RegisterQueryServer(queryHelper, nftKeeper)

s.ctx = ctx
s.queryClient = nft.NewQueryClient(queryHelper)
s.addrs = simtestutil.AddTestAddrsIncremental(bankKeeper, stakingKeeper, ctx, 3, sdk.NewInt(30000000))
s.nftKeeper = nftKeeper
s.queryClient = nft.NewQueryClient(queryHelper)
s.ctx = ctx
}

func TestTestSuite(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion x/nft/module/module.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nft
package module

import (
"context"
Expand Down
14 changes: 6 additions & 8 deletions x/nft/simulation/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import (

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/depinject"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/kv"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/nft"
"github.com/cosmos/cosmos-sdk/x/nft/keeper"
"github.com/cosmos/cosmos-sdk/x/nft/module"
"github.com/cosmos/cosmos-sdk/x/nft/simulation"
"github.com/cosmos/cosmos-sdk/x/nft/testutil"
)

var (
Expand All @@ -23,9 +22,8 @@ var (
)

func TestDecodeStore(t *testing.T) {
var cdc codec.Codec
depinject.Inject(testutil.AppConfig, &cdc)
dec := simulation.NewDecodeStore(cdc)
encCfg := moduletestutil.MakeTestEncodingConfig(module.AppModuleBasic{})
dec := simulation.NewDecodeStore(encCfg.Codec)

class := nft.Class{
Id: "ClassID",
Expand All @@ -34,15 +32,15 @@ func TestDecodeStore(t *testing.T) {
Description: "ClassDescription",
Uri: "ClassURI",
}
classBz, err := cdc.Marshal(&class)
classBz, err := encCfg.Codec.Marshal(&class)
require.NoError(t, err)

nft := nft.NFT{
ClassId: "ClassID",
Id: "NFTID",
Uri: "NFTURI",
}
nftBz, err := cdc.Marshal(&nft)
nftBz, err := encCfg.Codec.Marshal(&nft)
require.NoError(t, err)

nftOfClassByOwnerValue := []byte{0x01}
Expand Down
10 changes: 4 additions & 6 deletions x/nft/simulation/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,23 @@ import (
"github.com/stretchr/testify/require"

sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/depinject"
"github.com/cosmos/cosmos-sdk/types/module"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/nft"
nftmodule "github.com/cosmos/cosmos-sdk/x/nft/module"
"github.com/cosmos/cosmos-sdk/x/nft/simulation"
"github.com/cosmos/cosmos-sdk/x/nft/testutil"
)

func TestRandomizedGenState(t *testing.T) {
var cdc codec.Codec
depinject.Inject(testutil.AppConfig, &cdc)
encCfg := moduletestutil.MakeTestEncodingConfig(nftmodule.AppModuleBasic{})

s := rand.NewSource(1)
r := rand.New(s)

simState := module.SimulationState{
AppParams: make(simtypes.AppParams),
Cdc: cdc,
Cdc: encCfg.Codec,
Rand: r,
NumBonded: 3,
Accounts: simtypes.RandomAccounts(r, 3),
Expand Down
Loading