github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/distribution/types/msg_test.go (about)

     1  package types
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/fibonacci-chain/fbc/libs/tendermint/crypto/ed25519"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    10  )
    11  
    12  var (
    13  	delPk1       = ed25519.GenPrivKey().PubKey()
    14  	delPk2       = ed25519.GenPrivKey().PubKey()
    15  	delAddr1     = sdk.AccAddress(delPk1.Address())
    16  	delAddr2     = sdk.AccAddress(delPk2.Address())
    17  	emptyDelAddr sdk.AccAddress
    18  
    19  	valPk1       = ed25519.GenPrivKey().PubKey()
    20  	valAddr1     = sdk.ValAddress(valPk1.Address())
    21  	emptyValAddr sdk.ValAddress
    22  )
    23  
    24  // TestNewMsgSetWithdrawAddress test ValidateBasic for NewMsgSetWithdrawAddress
    25  func TestNewMsgSetWithdrawAddress(t *testing.T) {
    26  	msg := NewMsgSetWithdrawAddress(delAddr1, delAddr2)
    27  	bz := ModuleCdc.MustMarshalJSON(msg)
    28  	require.Equal(t, ModuleName, msg.Route())
    29  	require.Equal(t, "set_withdraw_address", msg.Type())
    30  	require.Equal(t, []sdk.AccAddress{delAddr1}, msg.GetSigners())
    31  	require.Equal(t, sdk.MustSortJSON(bz), msg.GetSignBytes())
    32  	require.NoError(t, msg.ValidateBasic())
    33  }
    34  
    35  // TestNewMsgWithdrawValidatorCommission test ValidateBasic for MsgWithdrawValidatorCommission
    36  func TestNewMsgWithdrawValidatorCommission(t *testing.T) {
    37  	msg := NewMsgWithdrawValidatorCommission(valAddr1)
    38  	bz := ModuleCdc.MustMarshalJSON(msg)
    39  	require.Equal(t, ModuleName, msg.Route())
    40  	require.Equal(t, "withdraw_validator_commission", msg.Type())
    41  	require.Equal(t, []sdk.AccAddress{valAddr1.Bytes()}, msg.GetSigners())
    42  	require.Equal(t, sdk.MustSortJSON(bz), msg.GetSignBytes())
    43  	require.NoError(t, msg.ValidateBasic())
    44  }
    45  
    46  // TestMsgSetWithdrawAddress test ValidateBasic for MsgSetWithdrawAddress
    47  func TestMsgSetWithdrawAddress(t *testing.T) {
    48  	tests := []struct {
    49  		delegatorAddr sdk.AccAddress
    50  		withdrawAddr  sdk.AccAddress
    51  		expectPass    bool
    52  	}{
    53  		{delAddr1, delAddr2, true},
    54  		{delAddr1, delAddr1, true},
    55  		{emptyDelAddr, delAddr1, false},
    56  		{delAddr1, emptyDelAddr, false},
    57  		{emptyDelAddr, emptyDelAddr, false},
    58  	}
    59  
    60  	for i, tc := range tests {
    61  		msg := NewMsgSetWithdrawAddress(tc.delegatorAddr, tc.withdrawAddr)
    62  		if tc.expectPass {
    63  			require.Nil(t, msg.ValidateBasic(), "test index: %v", i)
    64  		} else {
    65  			require.NotNil(t, msg.ValidateBasic(), "test index: %v", i)
    66  		}
    67  	}
    68  }
    69  
    70  // TestMsgWithdrawValidatorCommission test ValidateBasic for MsgWithdrawValidatorCommission
    71  func TestMsgWithdrawValidatorCommission(t *testing.T) {
    72  	tests := []struct {
    73  		validatorAddr sdk.ValAddress
    74  		expectPass    bool
    75  	}{
    76  		{valAddr1, true},
    77  		{emptyValAddr, false},
    78  	}
    79  	for i, tc := range tests {
    80  		msg := NewMsgWithdrawValidatorCommission(tc.validatorAddr)
    81  		if tc.expectPass {
    82  			require.Nil(t, msg.ValidateBasic(), "test index: %v", i)
    83  		} else {
    84  			require.NotNil(t, msg.ValidateBasic(), "test index: %v", i)
    85  		}
    86  	}
    87  }