github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/action/actionread_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 action
     7  
     8  import (
     9  	"encoding/hex"
    10  	"testing"
    11  
    12  	"github.com/golang/mock/gomock"
    13  	"github.com/iotexproject/iotex-proto/golang/iotexapi"
    14  	"github.com/iotexproject/iotex-proto/golang/iotexapi/mock_iotexapi"
    15  	"github.com/pkg/errors"
    16  	"github.com/stretchr/testify/require"
    17  
    18  	"github.com/iotexproject/iotex-core/ioctl/config"
    19  	"github.com/iotexproject/iotex-core/ioctl/util"
    20  	"github.com/iotexproject/iotex-core/test/identityset"
    21  	"github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient"
    22  )
    23  
    24  func TestNewActionReadCmd(t *testing.T) {
    25  	require := require.New(t)
    26  	ctrl := gomock.NewController(t)
    27  	client := mock_ioctlclient.NewMockClient(ctrl)
    28  	apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl)
    29  	addr := identityset.Address(0).String()
    30  
    31  	client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).AnyTimes()
    32  	client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).Times(2)
    33  	client.EXPECT().Address(gomock.Any()).Return(addr, nil).Times(3)
    34  	client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return(addr, nil).Times(2)
    35  
    36  	t.Run("read action", func(t *testing.T) {
    37  		expectedVal := "36306665343762313030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030\n"
    38  		apiServiceClient.EXPECT().ReadContract(gomock.Any(), gomock.Any()).Return(&iotexapi.ReadContractResponse{
    39  			Data: hex.EncodeToString([]byte("60fe47b100000000000000000000000000000000000000000000000000000000")),
    40  		}, nil)
    41  		cmd := NewActionReadCmd(client)
    42  		result, err := util.ExecuteCmd(cmd, addr, "--bytecode", "0x60fe47b100000000000000000000000000000000000000000000000000000000")
    43  		require.NoError(err)
    44  		require.Equal(expectedVal, result)
    45  	})
    46  
    47  	t.Run("failed to Read contract", func(t *testing.T) {
    48  		expectedErr := errors.New("failed to Read contract")
    49  		apiServiceClient.EXPECT().ReadContract(gomock.Any(), gomock.Any()).Return(nil, expectedErr)
    50  		cmd := NewActionReadCmd(client)
    51  		_, err := util.ExecuteCmd(cmd, addr, "--bytecode", "0x60fe47b100000000000000000000000000000000000000000000000000000000")
    52  		require.Contains(err.Error(), expectedErr.Error())
    53  	})
    54  
    55  	t.Run("invalid bytecode", func(t *testing.T) {
    56  		expectedErr := errors.New("invalid bytecode")
    57  		cmd := NewActionReadCmd(client)
    58  		_, err := util.ExecuteCmd(cmd, addr, "--bytecode", "test")
    59  		require.Contains(err.Error(), expectedErr.Error())
    60  	})
    61  
    62  	t.Run("failed to get contract address", func(t *testing.T) {
    63  		expectedErr := errors.New("failed to get contract address")
    64  		client.EXPECT().Address(gomock.Any()).Return("", expectedErr)
    65  		cmd := NewActionReadCmd(client)
    66  		_, err := util.ExecuteCmd(cmd, "test")
    67  		require.Contains(err.Error(), expectedErr.Error())
    68  	})
    69  }