diff --git a/x/tax2gas/keeper/keeper_test.go b/x/tax2gas/keeper/keeper_test.go new file mode 100644 index 00000000..48ed07c4 --- /dev/null +++ b/x/tax2gas/keeper/keeper_test.go @@ -0,0 +1,120 @@ +package keeper_test + +import ( + "testing" + + "github.com/classic-terra/core/v3/x/tax2gas/keeper" + "github.com/classic-terra/core/v3/x/tax2gas/types" + "github.com/cosmos/cosmos-sdk/testutil" + sdk "github.com/cosmos/cosmos-sdk/types" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" + "github.com/stretchr/testify/suite" + + tmproto "github.com/cometbft/cometbft/proto/tendermint/types" + tmtime "github.com/cometbft/cometbft/types/time" + "github.com/cosmos/cosmos-sdk/baseapp" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" +) + +type KeeperTestSuite struct { + suite.Suite + + ctx sdk.Context + keeper keeper.Keeper + + queryClient types.QueryClient + msgServer types.MsgServer + + encCfg moduletestutil.TestEncodingConfig +} + +func TestKeeperTestSuite(t *testing.T) { + suite.Run(t, new(KeeperTestSuite)) +} + +func (suite *KeeperTestSuite) SetupTest() { + key := sdk.NewKVStoreKey(types.StoreKey) + testCtx := testutil.DefaultContextWithDB(suite.T(), key, sdk.NewTransientStoreKey("transient_test")) + ctx := testCtx.Ctx.WithBlockHeader(tmproto.Header{Time: tmtime.Now()}) + encCfg := moduletestutil.MakeTestEncodingConfig() + + // gomock initializations + + suite.ctx = ctx + suite.keeper = keeper.NewKeeper( + encCfg.Codec, + key, + authtypes.NewModuleAddress(govtypes.ModuleName).String(), + ) + + types.RegisterInterfaces(encCfg.InterfaceRegistry) + + queryHelper := baseapp.NewQueryServerTestHelper(ctx, encCfg.InterfaceRegistry) + querier := keeper.NewQuerier(suite.keeper) + types.RegisterQueryServer(queryHelper, querier) + queryClient := types.NewQueryClient(queryHelper) + + suite.queryClient = queryClient + suite.msgServer = keeper.NewMsgServerImpl(suite.keeper) + suite.encCfg = encCfg +} + +func (suite *KeeperTestSuite) TestGetAuthority() { + NewKeeperWithAuthority := func(authority string) keeper.Keeper { + return keeper.NewKeeper( + moduletestutil.MakeTestEncodingConfig().Codec, + sdk.NewKVStoreKey(types.StoreKey), + authority, + ) + } + + tests := map[string]string{ + "some random account": "cosmos139f7kncmglres2nf3h4hc4tade85ekfr8sulz5", + "gov module account": authtypes.NewModuleAddress(govtypes.ModuleName).String(), + "another module account": authtypes.NewModuleAddress(minttypes.ModuleName).String(), + } + + for name, expected := range tests { + suite.T().Run(name, func(t *testing.T) { + kpr := NewKeeperWithAuthority(expected) + actual := kpr.GetAuthority() + suite.Require().Equal(expected, actual) + }) + } +} + +func (suite *KeeperTestSuite) TestSetParams() { + ctx, tax2gasKeeper := suite.ctx, suite.keeper + require := suite.Require() + + tax2gasKeeper.SetParams(ctx, types.DefaultParams()) + tests := []struct { + name string + params types.Params + expFail bool + }{ + { + name: "empty params", + params: types.Params{}, + expFail: true, + }, + { + name: "default params", + params: types.DefaultParams(), + expFail: false, + }, + } + + for _, tc := range tests { + suite.T().Run(tc.name, func(t *testing.T) { + err := tax2gasKeeper.SetParams(ctx, tc.params) + if tc.expFail { + require.Error(err) + } else { + require.NoError(err) + } + }) + } +} diff --git a/x/tax2gas/keeper/msg_server_test.go b/x/tax2gas/keeper/msg_server_test.go new file mode 100644 index 00000000..3819954f --- /dev/null +++ b/x/tax2gas/keeper/msg_server_test.go @@ -0,0 +1,58 @@ +package keeper_test + +import ( + "github.com/classic-terra/core/v3/x/tax2gas/types" +) + +func (suite *KeeperTestSuite) TestMsgUpdateParams() { + // default params + params := types.DefaultParams() + + testCases := []struct { + name string + input *types.MsgUpdateParams + expErr bool + expErrMsg string + }{ + { + name: "invalid authority", + input: &types.MsgUpdateParams{ + Authority: "invalid", + Params: params, + }, + expErr: true, + expErrMsg: "invalid authority", + }, + { + name: "empty params", + input: &types.MsgUpdateParams{ + Authority: suite.keeper.GetAuthority(), + Params: types.Params{}, + }, + expErr: true, + expErrMsg: "must provide at least 1 gas prices", + }, + { + name: "all good", + input: &types.MsgUpdateParams{ + Authority: suite.keeper.GetAuthority(), + Params: params, + }, + expErr: false, + }, + } + + for _, tc := range testCases { + tc := tc + suite.Run(tc.name, func() { + _, err := suite.msgServer.UpdateParams(suite.ctx, tc.input) + + if tc.expErr { + suite.Require().Error(err) + suite.Require().Contains(err.Error(), tc.expErrMsg) + } else { + suite.Require().NoError(err) + } + }) + } +} diff --git a/x/tax2gas/keeper/params.go b/x/tax2gas/keeper/params.go index ac87194f..d36ab16e 100644 --- a/x/tax2gas/keeper/params.go +++ b/x/tax2gas/keeper/params.go @@ -6,8 +6,11 @@ import ( "github.com/classic-terra/core/v3/x/tax2gas/types" ) -// SetParams sets the gov module's parameters. +// SetParams sets the tax2gas module's parameters. func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error { + if err := params.Validate(); err != nil { + return err + } store := ctx.KVStore(k.storeKey) bz, err := k.cdc.Marshal(¶ms) if err != nil { @@ -18,7 +21,7 @@ func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error { return nil } -// GetParams gets the gov module's parameters. +// GetParams gets the tax2gas module's parameters. func (k Keeper) GetParams(clientCtx sdk.Context) (params types.Params) { store := clientCtx.KVStore(k.storeKey) bz := store.Get(types.ParamsKey) diff --git a/x/tax2gas/types/codec.go b/x/tax2gas/types/codec.go index 3c3402be..cffb8f98 100644 --- a/x/tax2gas/types/codec.go +++ b/x/tax2gas/types/codec.go @@ -11,13 +11,13 @@ import ( // RegisterLegacyAminoCodec registers the necessary x/tax2gas interfaces and concrete types // on the provided LegacyAmino codec. These types are used for Amino JSON serialization. func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { - cdc.RegisterConcrete(&MsgTest{}, "tax2gas/MsgTest", nil) + cdc.RegisterConcrete(&MsgUpdateParams{}, "terra/tax2gas/MsgUpdateParams", nil) } func RegisterInterfaces(registry types.InterfaceRegistry) { registry.RegisterImplementations( (*sdk.Msg)(nil), - &MsgTest{}, + &MsgUpdateParams{}, ) msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) diff --git a/x/tax2gas/types/params.go b/x/tax2gas/types/params.go index 812c5738..71404001 100644 --- a/x/tax2gas/types/params.go +++ b/x/tax2gas/types/params.go @@ -1,6 +1,8 @@ package types import ( + fmt "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" ) @@ -52,6 +54,9 @@ func DefaultParams() Params { // Validate validates params. func (p Params) Validate() error { + if len(p.GasPrices) == 0 { + return fmt.Errorf("must provide at least 1 gas prices") + } return nil }