Skip to content

Commit

Permalink
refactor: Use mocks for x/nft testing (#12407)
Browse files Browse the repository at this point in the history
## Description

Ref: #12398



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
facundomedica authored Jul 5, 2022
1 parent c3314ea commit 76bbcd2
Show file tree
Hide file tree
Showing 11 changed files with 352 additions and 169 deletions.
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

0 comments on commit 76bbcd2

Please sign in to comment.