Skip to content

Commit

Permalink
remove classKeeper mock from keeper test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
tkxkd0159 committed Feb 8, 2024
1 parent b0dddfd commit 8e3d439
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 76 deletions.
122 changes: 54 additions & 68 deletions x/collection/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package keeper_test
import (
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"

"cosmossdk.io/log"
"cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
Expand All @@ -23,17 +21,15 @@ import (
"github.com/Finschia/finschia-sdk/x/collection"
"github.com/Finschia/finschia-sdk/x/collection/keeper"
"github.com/Finschia/finschia-sdk/x/collection/module"
ctutil "github.com/Finschia/finschia-sdk/x/collection/testutil"
)

type KeeperTestSuite struct {
suite.Suite

ctx sdk.Context
keeper keeper.Keeper
queryServer collection.QueryServer
msgServer collection.MsgServer
mockClassKeeper *ctutil.MockClassKeeper
ctx sdk.Context
keeper keeper.Keeper
queryServer collection.QueryServer
msgServer collection.MsgServer

vendor sdk.AccAddress
operator sdk.AccAddress
Expand All @@ -50,7 +46,6 @@ type KeeperTestSuite struct {

numNFTs int
numRoots int
mockCtrl *gomock.Controller
}

func (s *KeeperTestSuite) createRandomAccounts(accNum int) []sdk.AccAddress {
Expand All @@ -72,61 +67,9 @@ func (s *KeeperTestSuite) createRandomAccounts(accNum int) []sdk.AccAddress {
}

func (s *KeeperTestSuite) SetupTest() {
// Create Store for test
key := storetypes.NewKVStoreKey(collection.StoreKey)
tkey := storetypes.NewTransientStoreKey("transient_test")
testCtx := testutil.DefaultContextWithDB(s.T(), key, tkey)
kvStoreService := runtime.NewKVStoreService(key)
s.ctx = testCtx.Ctx

// Create EncodingConfig
encCfg := moduletestutil.MakeTestEncodingConfig(module.AppModuleBasic{})
encCfg.InterfaceRegistry = codectestutil.CodecOptions{
AccAddressPrefix: "link",
ValAddressPrefix: "linkvaloper",
}.NewInterfaceRegistry()
encCfg.Codec = codec.NewProtoCodec(encCfg.InterfaceRegistry)

collection.RegisterInterfaces(encCfg.InterfaceRegistry)
testdata.RegisterInterfaces(encCfg.InterfaceRegistry)

// Create BaseApp
bapp := baseapp.NewBaseApp(
"collection",
log.NewNopLogger(),
testCtx.DB,
encCfg.TxConfig.TxDecoder(),
)
bapp.SetInterfaceRegistry(encCfg.InterfaceRegistry)

ctrl := gomock.NewController(s.T())
s.mockCtrl = ctrl
s.mockClassKeeper = ctutil.NewMockClassKeeper(ctrl)
s.keeper = keeper.NewKeeper(encCfg.Codec, kvStoreService, s.mockClassKeeper)

s.queryServer = keeper.NewQueryServer(s.keeper)
s.msgServer = keeper.NewMsgServer(s.keeper)

s.depthLimit = 4
s.keeper.SetParams(s.ctx, collection.Params{
DepthLimit: uint32(s.depthLimit),
WidthLimit: 4,
})

addresses := []*sdk.AccAddress{
&s.vendor,
&s.operator,
&s.customer,
&s.stranger,
}
for i, address := range s.createRandomAccounts(len(addresses)) {
*addresses[i] = address
}
s.prepareInitialSetup()

s.balance = math.NewInt(1000000)

s.mockClassKeeper.EXPECT().NewID(gomock.Any()).Return("9be17165")
s.mockClassKeeper.EXPECT().HasID(gomock.Any(), "9be17165").Return(true).AnyTimes()
s.contractID = s.keeper.CreateContract(s.ctx, s.vendor, collection.Contract{
Name: "fox",
})
Expand Down Expand Up @@ -211,16 +154,59 @@ func (s *KeeperTestSuite) SetupTest() {
s.Require().NoError(err)

// not token contract
s.mockClassKeeper.EXPECT().NewID(gomock.Any()).Return("678c146a")
s.mockClassKeeper.EXPECT().HasID(gomock.Any(), "678c146a").Return(true).AnyTimes()
notTokenContractID := s.mockClassKeeper.NewID(s.ctx)
notTokenContractID := s.keeper.NewID(s.ctx)
err = keeper.ValidateLegacyContract(s.keeper, s.ctx, notTokenContractID)
s.Require().ErrorIs(err, collection.ErrCollectionNotExist)
s.mockClassKeeper.EXPECT().HasID(gomock.Any(), "deadbeef").Return(false).AnyTimes()
}

func (s *KeeperTestSuite) TearDownTest() {
s.mockCtrl.Finish()
func (s *KeeperTestSuite) prepareInitialSetup() {
// Create Store for test
key := storetypes.NewKVStoreKey(collection.StoreKey)
tkey := storetypes.NewTransientStoreKey("transient_test")
testCtx := testutil.DefaultContextWithDB(s.T(), key, tkey)
kvStoreService := runtime.NewKVStoreService(key)
s.ctx = testCtx.Ctx

// Create EncodingConfig
encCfg := moduletestutil.MakeTestEncodingConfig(module.AppModuleBasic{})
encCfg.InterfaceRegistry = codectestutil.CodecOptions{
AccAddressPrefix: "link",
ValAddressPrefix: "linkvaloper",
}.NewInterfaceRegistry()
encCfg.Codec = codec.NewProtoCodec(encCfg.InterfaceRegistry)

collection.RegisterInterfaces(encCfg.InterfaceRegistry)
testdata.RegisterInterfaces(encCfg.InterfaceRegistry)

// Create BaseApp
bapp := baseapp.NewBaseApp(
"collection",
log.NewNopLogger(),
testCtx.DB,
encCfg.TxConfig.TxDecoder(),
)
bapp.SetInterfaceRegistry(encCfg.InterfaceRegistry)

s.keeper = keeper.NewKeeper(encCfg.Codec, kvStoreService)
s.keeper.InitGenesis(s.ctx, collection.DefaultGenesisState())
s.depthLimit = 4
s.keeper.SetParams(s.ctx, collection.Params{
DepthLimit: uint32(s.depthLimit),
WidthLimit: 4,
})

s.queryServer = keeper.NewQueryServer(s.keeper)
s.msgServer = keeper.NewMsgServer(s.keeper)

addresses := []*sdk.AccAddress{
&s.vendor,
&s.operator,
&s.customer,
&s.stranger,
}
for i, address := range s.createRandomAccounts(len(addresses)) {
*addresses[i] = address
}
}

func TestKeeperTestSuite(t *testing.T) {
Expand Down
6 changes: 1 addition & 5 deletions x/collection/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ import (
"encoding/json"
"fmt"

abci "github.com/cometbft/cometbft/abci/types"
"github.com/golang/mock/gomock"

"cosmossdk.io/math"

abci "github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"

Expand Down Expand Up @@ -581,7 +578,6 @@ func (s *KeeperTestSuite) TestMsgRevokeOperator() {

func (s *KeeperTestSuite) TestMsgCreateContract() {
expectedNewContractID := "3336b76f"
s.mockClassKeeper.EXPECT().NewID(gomock.Any()).Return(expectedNewContractID).Times(1)
testCases := map[string]struct {
owner sdk.AccAddress
err error
Expand Down
3 changes: 0 additions & 3 deletions x/collection/keeper/supply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ import (
"fmt"
"math/rand"

"github.com/golang/mock/gomock"

"cosmossdk.io/math"

"github.com/Finschia/finschia-sdk/x/collection"
)

func (s *KeeperTestSuite) TestCreateContract() {
ctx, _ := s.ctx.CacheContext()
s.mockClassKeeper.EXPECT().NewID(gomock.Any()).Return("3336b76f")

input := collection.Contract{
Name: "tibetian fox",
Expand Down

0 comments on commit 8e3d439

Please sign in to comment.