Skip to content

Commit

Permalink
register codec and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
expertdicer committed Jul 11, 2024
1 parent 92ef82c commit c016cc6
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 4 deletions.
120 changes: 120 additions & 0 deletions x/tax2gas/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
58 changes: 58 additions & 0 deletions x/tax2gas/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
7 changes: 5 additions & 2 deletions x/tax2gas/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(&params)
if err != nil {
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions x/tax2gas/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions x/tax2gas/types/params.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit c016cc6

Please sign in to comment.