From 8e3d43950da12c931ec00928c9cb34f04727058f Mon Sep 17 00:00:00 2001 From: Jayden Lee <41176085+tkxkd0159@users.noreply.github.com> Date: Fri, 9 Feb 2024 01:10:43 +0900 Subject: [PATCH] remove classKeeper mock from keeper test suite --- x/collection/keeper/keeper_test.go | 122 +++++++++++-------------- x/collection/keeper/msg_server_test.go | 6 +- x/collection/keeper/supply_test.go | 3 - 3 files changed, 55 insertions(+), 76 deletions(-) diff --git a/x/collection/keeper/keeper_test.go b/x/collection/keeper/keeper_test.go index 8a22dcf263..c474d0991c 100644 --- a/x/collection/keeper/keeper_test.go +++ b/x/collection/keeper/keeper_test.go @@ -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" @@ -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 @@ -50,7 +46,6 @@ type KeeperTestSuite struct { numNFTs int numRoots int - mockCtrl *gomock.Controller } func (s *KeeperTestSuite) createRandomAccounts(accNum int) []sdk.AccAddress { @@ -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", }) @@ -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) { diff --git a/x/collection/keeper/msg_server_test.go b/x/collection/keeper/msg_server_test.go index f08c45fba8..524d607692 100644 --- a/x/collection/keeper/msg_server_test.go +++ b/x/collection/keeper/msg_server_test.go @@ -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" @@ -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 diff --git a/x/collection/keeper/supply_test.go b/x/collection/keeper/supply_test.go index f994421bf2..ec87bb13c8 100644 --- a/x/collection/keeper/supply_test.go +++ b/x/collection/keeper/supply_test.go @@ -4,8 +4,6 @@ import ( "fmt" "math/rand" - "github.com/golang/mock/gomock" - "cosmossdk.io/math" "github.com/Finschia/finschia-sdk/x/collection" @@ -13,7 +11,6 @@ import ( func (s *KeeperTestSuite) TestCreateContract() { ctx, _ := s.ctx.CacheContext() - s.mockClassKeeper.EXPECT().NewID(gomock.Any()).Return("3336b76f") input := collection.Contract{ Name: "tibetian fox",