github.com/cosmos/cosmos-sdk@v0.50.10/x/params/proposal_handler_test.go (about)

     1  package params_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/golang/mock/gomock"
     8  	"github.com/stretchr/testify/suite"
     9  
    10  	storetypes "cosmossdk.io/store/types"
    11  
    12  	"github.com/cosmos/cosmos-sdk/testutil"
    13  	sdk "github.com/cosmos/cosmos-sdk/types"
    14  	moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
    15  	govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
    16  	"github.com/cosmos/cosmos-sdk/x/params"
    17  	"github.com/cosmos/cosmos-sdk/x/params/keeper"
    18  	paramstestutil "github.com/cosmos/cosmos-sdk/x/params/testutil"
    19  	paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
    20  	"github.com/cosmos/cosmos-sdk/x/params/types/proposal"
    21  	stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
    22  )
    23  
    24  // StakingKeeper defines the expected staking keeper
    25  type StakingKeeper interface {
    26  	MaxValidators(ctx context.Context) (res uint32, err error)
    27  }
    28  
    29  type HandlerTestSuite struct {
    30  	suite.Suite
    31  
    32  	ctx           sdk.Context
    33  	govHandler    govv1beta1.Handler
    34  	stakingKeeper StakingKeeper
    35  }
    36  
    37  func (suite *HandlerTestSuite) SetupTest() {
    38  	encodingCfg := moduletestutil.MakeTestEncodingConfig(params.AppModuleBasic{})
    39  	key := storetypes.NewKVStoreKey(paramtypes.StoreKey)
    40  	tkey := storetypes.NewTransientStoreKey("params_transient_test")
    41  
    42  	ctx := testutil.DefaultContext(key, tkey)
    43  	paramsKeeper := keeper.NewKeeper(encodingCfg.Codec, encodingCfg.Amino, key, tkey)
    44  	paramsKeeper.Subspace("staking").WithKeyTable(stakingtypes.ParamKeyTable()) //nolint:staticcheck // TODO: depreacte this test case
    45  	ctrl := gomock.NewController(suite.T())
    46  	stakingKeeper := paramstestutil.NewMockStakingKeeper(ctrl)
    47  	stakingKeeper.EXPECT().MaxValidators(ctx).Return(uint32(1), nil)
    48  
    49  	suite.govHandler = params.NewParamChangeProposalHandler(paramsKeeper)
    50  	suite.stakingKeeper = stakingKeeper
    51  	suite.ctx = ctx
    52  }
    53  
    54  func TestHandlerTestSuite(t *testing.T) {
    55  	suite.Run(t, new(HandlerTestSuite))
    56  }
    57  
    58  func testProposal(changes ...proposal.ParamChange) *proposal.ParameterChangeProposal {
    59  	return proposal.NewParameterChangeProposal("title", "description", changes)
    60  }
    61  
    62  func (suite *HandlerTestSuite) TestProposalHandler() {
    63  	testCases := []struct {
    64  		name     string
    65  		proposal *proposal.ParameterChangeProposal
    66  		onHandle func()
    67  		expErr   bool
    68  	}{
    69  		{
    70  			"all fields",
    71  			testProposal(proposal.NewParamChange(stakingtypes.ModuleName, string(stakingtypes.KeyMaxValidators), "1")),
    72  			func() {
    73  				maxVals, err := suite.stakingKeeper.MaxValidators(suite.ctx)
    74  				suite.Require().NoError(err)
    75  				suite.Require().Equal(uint32(1), maxVals)
    76  			},
    77  			false,
    78  		},
    79  		{
    80  			"invalid type",
    81  			testProposal(proposal.NewParamChange(stakingtypes.ModuleName, string(stakingtypes.KeyMaxValidators), "-")),
    82  			func() {},
    83  			true,
    84  		},
    85  		//{
    86  		//	"omit empty fields",
    87  		//	testProposal(proposal.ParamChange{
    88  		//		Subspace: govtypes.ModuleName,
    89  		//		Key:      string(govv1.ParamStoreKeyDepositParams),
    90  		//		Value:    `{"min_deposit": [{"denom": "uatom","amount": "64000000"}], "max_deposit_period": "172800000000000"}`,
    91  		//	}),
    92  		//	func() {
    93  		//		depositParams := suite.app.GovKeeper.GetDepositParams(suite.ctx)
    94  		//		defaultPeriod := govv1.DefaultPeriod
    95  		//		suite.Require().Equal(govv1.DepositParams{
    96  		//			MinDeposit:       sdk.NewCoins(sdk.NewCoin("uatom", sdkmath.NewInt(64000000))),
    97  		//			MaxDepositPeriod: &defaultPeriod,
    98  		//		}, depositParams)
    99  		//	},
   100  		//	false,
   101  		// },
   102  	}
   103  
   104  	for _, tc := range testCases {
   105  		tc := tc
   106  		suite.Run(tc.name, func() {
   107  			err := suite.govHandler(suite.ctx, tc.proposal)
   108  			if tc.expErr {
   109  				suite.Require().Error(err)
   110  			} else {
   111  				suite.Require().NoError(err)
   112  				tc.onHandle()
   113  			}
   114  		})
   115  	}
   116  }