github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/account/accountbalance_test.go (about)

     1  // Copyright (c) 2022 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package account
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/golang/mock/gomock"
    12  	"github.com/iotexproject/iotex-proto/golang/iotexapi"
    13  	"github.com/iotexproject/iotex-proto/golang/iotexapi/mock_iotexapi"
    14  	"github.com/iotexproject/iotex-proto/golang/iotextypes"
    15  	"github.com/stretchr/testify/require"
    16  
    17  	"github.com/iotexproject/iotex-core/ioctl/config"
    18  	"github.com/iotexproject/iotex-core/ioctl/util"
    19  	"github.com/iotexproject/iotex-core/test/identityset"
    20  	"github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient"
    21  )
    22  
    23  func TestNewAccountBalance(t *testing.T) {
    24  	require := require.New(t)
    25  	ctrl := gomock.NewController(t)
    26  	defer ctrl.Finish()
    27  	client := mock_ioctlclient.NewMockClient(ctrl)
    28  	client.EXPECT().SelectTranslation(gomock.Any()).Return("", config.English).AnyTimes()
    29  
    30  	accAddr := identityset.Address(28)
    31  	client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return(accAddr.String(), nil).AnyTimes()
    32  	client.EXPECT().Config().Return(config.Config{}).AnyTimes()
    33  
    34  	apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl)
    35  	accountResponse := &iotexapi.GetAccountResponse{AccountMeta: &iotextypes.AccountMeta{
    36  		Address:          accAddr.String(),
    37  		Balance:          "20000000132432000",
    38  		PendingNonce:     uint64(1),
    39  		NumActions:       uint64(2),
    40  		IsContract:       true,
    41  		ContractByteCode: []byte("60806040526101f4600055603260015534801561001b57600080fd5b506102558061002b6000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c806358931c461461003b5780637f353d5514610045575b600080fd5b61004361004f565b005b61004d610097565b005b60006001905060005b6000548110156100935760028261006f9190610114565b915060028261007e91906100e3565b9150808061008b90610178565b915050610058565b5050565b60005b6001548110156100e057600281908060018154018082558091505060019003906000526020600020016000909190919091505580806100d890610178565b91505061009a565b50565b60006100ee8261016e565b91506100f98361016e565b925082610109576101086101f0565b5b828204905092915050565b600061011f8261016e565b915061012a8361016e565b9250817fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0483118215151615610163576101626101c1565b5b828202905092915050565b6000819050919050565b60006101838261016e565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8214156101b6576101b56101c1565b5b600182019050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601260045260246000fdfea2646970667358221220cb9cada3f1d447c978af17aa3529d6fe4f25f9c5a174085443e371b6940ae99b64736f6c63430008070033"),
    42  	}}
    43  	client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).AnyTimes()
    44  	apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(accountResponse, nil).AnyTimes()
    45  
    46  	t.Run("get account balance", func(t *testing.T) {
    47  		cmd := NewAccountBalance(client)
    48  		result, err := util.ExecuteCmd(cmd, accAddr.String())
    49  		require.NoError(err)
    50  		require.Contains(result, "0.020000000132432")
    51  	})
    52  
    53  	t.Run("get balance with empty address", func(t *testing.T) {
    54  		accAddr := ""
    55  
    56  		cmd := NewAccountBalance(client)
    57  		result, err := util.ExecuteCmd(cmd, accAddr)
    58  		require.NoError(err)
    59  		require.Contains(result, "0.020000000132432")
    60  	})
    61  
    62  	t.Run("get balance with StakingBucketPoolAddr", func(t *testing.T) {
    63  		accAddr := "io000000000000000000000000stakingprotocol"
    64  
    65  		cmd := NewAccountBalance(client)
    66  		result, err := util.ExecuteCmd(cmd, accAddr)
    67  		require.NoError(err)
    68  		require.Contains(result, "0.020000000132432")
    69  	})
    70  
    71  	t.Run("get balance with RewardingPoolAddr", func(t *testing.T) {
    72  		accAddr := "io0000000000000000000000rewardingprotocol"
    73  
    74  		cmd := NewAccountBalance(client)
    75  		result, err := util.ExecuteCmd(cmd, accAddr)
    76  		require.NoError(err)
    77  		require.Contains(result, "0.020000000132432")
    78  	})
    79  }