From 3f25325e0f93806ad943ea5f6f4965093ad55117 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Wed, 28 Feb 2024 15:40:23 +0530 Subject: [PATCH] Setup integration tests for auction module keeper --- Makefile | 7 + app/app.go | 2 - go.mod | 4 +- tests/Makefile | 2 + .../integration/auction/keeper/common_test.go | 120 ++++++++++++++++++ .../auction/keeper/query_server_test.go | 31 +++++ x/auction/keys.go | 3 + 7 files changed, 165 insertions(+), 4 deletions(-) create mode 100644 tests/Makefile create mode 100644 tests/integration/auction/keeper/common_test.go create mode 100644 tests/integration/auction/keeper/query_server_test.go diff --git a/Makefile b/Makefile index 34a7d9ca..6ce6a9da 100644 --- a/Makefile +++ b/Makefile @@ -81,3 +81,10 @@ lint-fix: @$(golangci_lint_cmd) run ./... --fix --timeout 15m .PHONY: lint lint-fix + +################# +### Tests ### +################# + +test-integration: + $(MAKE) -C tests test-integration diff --git a/app/app.go b/app/app.go index 13bcb96b..aea38d1a 100644 --- a/app/app.go +++ b/app/app.go @@ -79,7 +79,6 @@ type LaconicApp struct { AuctionKeeper *auctionkeeper.Keeper // (Use * as per ProvideModule implementation) BondKeeper *bondkeeper.Keeper RegistryKeeper registrykeeper.Keeper - // RegistryRecordKeeper registrykeeper.RecordKeeper // simulation manager sm *module.SimulationManager @@ -141,7 +140,6 @@ func NewLaconicApp( &app.ConsensusParamsKeeper, &app.AuctionKeeper, &app.BondKeeper, - // &app.RegistryRecordKeeper, &app.RegistryKeeper, ); err != nil { return nil, err diff --git a/go.mod b/go.mod index e4386920..21ec8029 100644 --- a/go.mod +++ b/go.mod @@ -37,11 +37,13 @@ require ( github.com/ipld/go-ipld-prime v0.21.0 github.com/spf13/cobra v1.8.0 github.com/spf13/viper v1.17.0 + github.com/stretchr/testify v1.8.4 golang.org/x/exp v0.0.0-20231006140011-7918f672742d google.golang.org/genproto/googleapis/api v0.0.0-20231120223509-83a465c0220f google.golang.org/grpc v1.60.1 google.golang.org/protobuf v1.32.0 gopkg.in/yaml.v3 v3.0.1 + gotest.tools/v3 v3.5.1 ) require ( @@ -160,7 +162,6 @@ require ( github.com/spf13/afero v1.10.0 // indirect github.com/spf13/cast v1.5.1 // indirect github.com/spf13/pflag v1.0.5 // indirect - github.com/stretchr/testify v1.8.4 // indirect github.com/subosito/gotenv v1.6.0 // indirect github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect github.com/tendermint/go-amino v0.16.0 // indirect @@ -179,7 +180,6 @@ require ( google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect - gotest.tools/v3 v3.5.1 // indirect lukechampine.com/blake3 v1.1.6 // indirect nhooyr.io/websocket v1.8.6 // indirect pgregory.net/rapid v1.1.0 // indirect diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 00000000..d325c7ef --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,2 @@ +test-integration: + go test -mod=readonly ./integration/... -test.v -timeout 30m diff --git a/tests/integration/auction/keeper/common_test.go b/tests/integration/auction/keeper/common_test.go new file mode 100644 index 00000000..0014a844 --- /dev/null +++ b/tests/integration/auction/keeper/common_test.go @@ -0,0 +1,120 @@ +package keeper_test + +import ( + "testing" + + "cosmossdk.io/core/appmodule" + "cosmossdk.io/log" + storetypes "cosmossdk.io/store/types" + cmtprototypes "github.com/cometbft/cometbft/proto/tendermint/types" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" + + "github.com/cosmos/cosmos-sdk/codec" + addresscodec "github.com/cosmos/cosmos-sdk/codec/address" + "github.com/cosmos/cosmos-sdk/runtime" + "github.com/cosmos/cosmos-sdk/testutil/integration" + sdk "github.com/cosmos/cosmos-sdk/types" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" + "github.com/cosmos/cosmos-sdk/x/auth" + authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" + authsims "github.com/cosmos/cosmos-sdk/x/auth/simulation" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + "github.com/cosmos/cosmos-sdk/x/bank" + bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + + auctionTypes "git.vdb.to/cerc-io/laconic2d/x/auction" + auctionkeeper "git.vdb.to/cerc-io/laconic2d/x/auction/keeper" + auctionmodule "git.vdb.to/cerc-io/laconic2d/x/auction/module" + bondTypes "git.vdb.to/cerc-io/laconic2d/x/bond" + registryTypes "git.vdb.to/cerc-io/laconic2d/x/registry" +) + +type KeeperTestSuite struct { + suite.Suite + + app *integration.App + + sdkCtx sdk.Context + cdc codec.Codec + keys map[string]*storetypes.KVStoreKey + + accountKeeper authkeeper.AccountKeeper + bankKeeper bankkeeper.Keeper + auctionKeeper *auctionkeeper.Keeper +} + +func (kts *KeeperTestSuite) SetupTest() { + keys := storetypes.NewKVStoreKeys( + authtypes.StoreKey, banktypes.StoreKey, auctionTypes.StoreKey, + ) + cdc := moduletestutil.MakeTestEncodingConfig(auth.AppModuleBasic{}, auctionmodule.AppModule{}).Codec + + logger := log.NewNopLogger() // Use log.NewTestLogger(kts.T()) for help with debugging + cms := integration.CreateMultiStore(keys, logger) + + newCtx := sdk.NewContext(cms, cmtprototypes.Header{}, true, logger) + + authority := authtypes.NewModuleAddress("gov") + + maccPerms := map[string][]string{ + auctionTypes.ModuleName: {}, + auctionTypes.AuctionBurnModuleAccountName: {}, + bondTypes.ModuleName: {}, + registryTypes.ModuleName: {}, + registryTypes.RecordRentModuleAccountName: {}, + registryTypes.AuthorityRentModuleAccountName: {}, + } + + accountKeeper := authkeeper.NewAccountKeeper( + cdc, + runtime.NewKVStoreService(keys[authtypes.StoreKey]), + authtypes.ProtoBaseAccount, + maccPerms, + addresscodec.NewBech32Codec(sdk.Bech32MainPrefix), + sdk.Bech32MainPrefix, + authority.String(), + ) + + blockedAddresses := map[string]bool{ + accountKeeper.GetAuthority(): false, + } + bankKeeper := bankkeeper.NewBaseKeeper( + cdc, + runtime.NewKVStoreService(keys[banktypes.StoreKey]), + accountKeeper, + blockedAddresses, + authority.String(), + log.NewNopLogger(), + ) + + auctionKeeper := auctionkeeper.NewKeeper(cdc, runtime.NewKVStoreService(keys[auctionTypes.StoreKey]), accountKeeper, bankKeeper) + + authModule := auth.NewAppModule(cdc, accountKeeper, authsims.RandomGenesisAccounts, nil) + bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper, nil) + auctionModule := auctionmodule.NewAppModule(cdc, auctionKeeper) + + integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc, map[string]appmodule.AppModule{ + authtypes.ModuleName: authModule, + banktypes.ModuleName: bankModule, + auctionTypes.ModuleName: auctionModule, + }) + + sdkCtx := sdk.UnwrapSDKContext(integrationApp.Context()) + + // Register MsgServer and QueryServer + auctionTypes.RegisterMsgServer(integrationApp.MsgServiceRouter(), auctionkeeper.NewMsgServerImpl(auctionKeeper)) + auctionTypes.RegisterQueryServer(integrationApp.QueryHelper(), auctionkeeper.NewQueryServerImpl(auctionKeeper)) + + // set default staking params + assert.Nil(kts.T(), auctionKeeper.Params.Set(sdkCtx, auctionTypes.DefaultParams())) + + kts.app = integrationApp + kts.sdkCtx, kts.cdc, kts.keys = sdkCtx, cdc, keys + kts.accountKeeper, kts.bankKeeper, kts.auctionKeeper = accountKeeper, bankKeeper, auctionKeeper +} + +func TestKeeperTestSuite(t *testing.T) { + suite.Run(t, new(KeeperTestSuite)) +} diff --git a/tests/integration/auction/keeper/query_server_test.go b/tests/integration/auction/keeper/query_server_test.go new file mode 100644 index 00000000..2bd9f2d5 --- /dev/null +++ b/tests/integration/auction/keeper/query_server_test.go @@ -0,0 +1,31 @@ +package keeper_test + +import ( + "context" + "fmt" + + types "git.vdb.to/cerc-io/laconic2d/x/auction" +) + +func (kts *KeeperTestSuite) TestGrpcQueryParams() { + qr := kts.app.QueryHelper() + queryClient := types.NewQueryClient(qr) + + testCases := []struct { + msg string + req *types.QueryParamsRequest + }{ + { + "fetch params", + &types.QueryParamsRequest{}, + }, + } + for _, test := range testCases { + kts.Run(fmt.Sprintf("Case %s", test.msg), func() { + resp, err := queryClient.Params(context.Background(), test.req) + kts.Require().Nil(err) + kts.Require().Equal(*(resp.Params), types.DefaultParams()) + }) + } +} + diff --git a/x/auction/keys.go b/x/auction/keys.go index fe626433..81c24cf3 100644 --- a/x/auction/keys.go +++ b/x/auction/keys.go @@ -5,6 +5,9 @@ import "cosmossdk.io/collections" const ( ModuleName = "auction" + // StoreKey defines the primary module store key + StoreKey = ModuleName + // AuctionBurnModuleAccountName is the name of the auction burn module account. AuctionBurnModuleAccountName = "auction_burn" )