Skip to content

Commit

Permalink
[ioctl] Build Read unittest (#3635)
Browse files Browse the repository at this point in the history
* build Read unittest

* fix code smells

* remove redundant argument

* fix error

* remove redundant code

* remove redundant space

* delete useless code

Co-authored-by: huof6890 <[email protected]>
  • Loading branch information
LuckyPigeon and huof6829 authored Sep 30, 2022
1 parent 49ce613 commit b94422c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
1 change: 0 additions & 1 deletion ioctl/newcmd/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,6 @@ func Execute(client ioctl.Client,

// Read reads smart contract on IoTeX blockchain
func Read(client ioctl.Client,
cmd *cobra.Command,
contract address.Address,
amount string,
bytecode []byte,
Expand Down
68 changes: 39 additions & 29 deletions ioctl/newcmd/action/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/util"
"github.com/iotexproject/iotex-core/test/identityset"
"github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient"
)

Expand All @@ -28,8 +29,8 @@ func TestSigner(t *testing.T) {
ctrl := gomock.NewController(t)
client := mock_ioctlclient.NewMockClient(ctrl)
client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).Times(2)
client.EXPECT().SetEndpointWithFlag(gomock.Any()).Do(func(_ func(*string, string, string, string)) {})
client.EXPECT().SetInsecureWithFlag(gomock.Any()).Do(func(_ func(*bool, string, bool, string)) {})
client.EXPECT().SetEndpointWithFlag(gomock.Any())
client.EXPECT().SetInsecureWithFlag(gomock.Any())

t.Run("returns signer's address", func(t *testing.T) {
client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return("test", nil).AnyTimes()
Expand Down Expand Up @@ -119,7 +120,6 @@ func TestSendRaw(t *testing.T) {

t.Run("failed to invoke SendAction api", func(t *testing.T) {
expectedErr := errors.New("failed to invoke SendAction api")

apiServiceClient.EXPECT().SendAction(gomock.Any(), gomock.Any()).Return(nil, expectedErr)

cmd := NewActionCmd(client)
Expand Down Expand Up @@ -155,8 +155,8 @@ func TestSendAction(t *testing.T) {
}}

client.EXPECT().SelectTranslation(gomock.Any()).Return("action", config.English).Times(64)
client.EXPECT().SetEndpointWithFlag(gomock.Any()).Do(func(_ func(*string, string, string, string)) {}).AnyTimes()
client.EXPECT().SetInsecureWithFlag(gomock.Any()).Do(func(_ func(*bool, string, bool, string)) {}).AnyTimes()
client.EXPECT().SetEndpointWithFlag(gomock.Any()).AnyTimes()
client.EXPECT().SetInsecureWithFlag(gomock.Any()).AnyTimes()
client.EXPECT().IsCryptoSm2().Return(false).Times(15)
client.EXPECT().NewKeyStore().Return(ks).Times(15)

Expand All @@ -165,9 +165,6 @@ func TestSendAction(t *testing.T) {
client.EXPECT().ReadSecret().Return("", expectedErr).Times(1)

cmd := NewActionCmd(client)
RegisterWriteCommand(client, cmd)
_, err := util.ExecuteCmd(cmd, "--password", "")
require.NoError(err)
err = SendAction(client, cmd, elp, accAddr.String(), "", 0, false)
require.Contains(err.Error(), expectedErr.Error())
})
Expand All @@ -189,9 +186,6 @@ func TestSendAction(t *testing.T) {

t.Run("sends signed action to blockchain", func(t *testing.T) {
cmd := NewActionCmd(client)
RegisterWriteCommand(client, cmd)
_, err := util.ExecuteCmd(cmd, "--password", passwd)
require.NoError(err)
err = SendAction(client, cmd, elp, accAddr.String(), passwd, 1, false)
require.NoError(err)
})
Expand All @@ -201,9 +195,6 @@ func TestSendAction(t *testing.T) {
client.EXPECT().HdwalletMnemonic(gomock.Any()).Return(mnemonic, nil)

cmd := NewActionCmd(client)
RegisterWriteCommand(client, cmd)
_, err := util.ExecuteCmd(cmd, "--password", passwd)
require.NoError(err)
err = SendAction(client, cmd, elp, "hdw::1/2", passwd, 1, false)
require.NoError(err)
})
Expand All @@ -212,9 +203,6 @@ func TestSendAction(t *testing.T) {
client.EXPECT().AskToConfirm(gomock.Any()).Return(false, nil)

cmd := NewActionCmd(client)
RegisterWriteCommand(client, cmd)
_, err := util.ExecuteCmd(cmd, "--password", passwd)
require.NoError(err)
err = SendAction(client, cmd, elp, accAddr.String(), passwd, 1, false)
require.NoError(err)
})
Expand All @@ -224,9 +212,6 @@ func TestSendAction(t *testing.T) {
client.EXPECT().AskToConfirm(gomock.Any()).Return(false, expectedErr)

cmd := NewActionCmd(client)
RegisterWriteCommand(client, cmd)
_, err := util.ExecuteCmd(cmd, "--password", passwd)
require.NoError(err)
err = SendAction(client, cmd, elp, accAddr.String(), passwd, 1, false)
require.Contains(err.Error(), expectedErr.Error())
})
Expand All @@ -236,9 +221,6 @@ func TestSendAction(t *testing.T) {
apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(nil, expectedErr)

cmd := NewActionCmd(client)
RegisterWriteCommand(client, cmd)
_, err := util.ExecuteCmd(cmd, "--password", passwd)
require.NoError(err)
err = SendAction(client, cmd, elp, accAddr.String(), passwd, 1, false)
require.Contains(err.Error(), expectedErr.Error())
})
Expand All @@ -250,9 +232,6 @@ func TestSendAction(t *testing.T) {
apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(nil, expectedErr)

cmd := NewActionCmd(client)
RegisterWriteCommand(client, cmd)
_, err := util.ExecuteCmd(cmd, "--password", passwd)
require.NoError(err)
err = SendAction(client, cmd, elp, "hdw::1/2", passwd, 1, false)
require.Contains(err.Error(), expectedErr.Error())
})
Expand All @@ -263,10 +242,41 @@ func TestSendAction(t *testing.T) {
apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(accountResponse, nil)

cmd := NewActionCmd(client)
RegisterWriteCommand(client, cmd)
_, err := util.ExecuteCmd(cmd, "--password", passwd)
require.NoError(err)
err = SendAction(client, cmd, elp, accAddr.String(), passwd, 1, false)
require.Contains(err.Error(), expectedErr.Error())
})
}

func TestRead(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
client := mock_ioctlclient.NewMockClient(ctrl)
apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl)
accAddr := identityset.Address(0)
bytecode := "608060405234801561001057600080fd5b506040516040806108018339810180604052810190808051906020019092919080519060200190929190505050816004819055508060058190555050506107a58061005c6000396000f300608060405260043610610078576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680631249c58b1461007d57806327e235e31461009457806353277879146100eb5780636941b84414610142578063810ad50514610199578063a9059cbb14610223575b600080fd5b34801561008957600080fd5b50610092610270565b005b3480156100a057600080fd5b506100d5600480360381019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610475565b6040518082815260200191505060405180910390f35b3480156100f757600080fd5b5061012c600480360381019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061048d565b6040518082815260200191505060405180910390f35b34801561014e57600080fd5b50610183600480360381019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506104a5565b6040518082815260200191505060405180910390f35b3480156101a557600080fd5b506101da600480360381019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506104bd565b604051808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018281526020019250505060405180910390f35b34801561022f57600080fd5b5061026e600480360381019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610501565b005b436004546000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054011115151561032a576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260108152602001807f746f6f20736f6f6e20746f206d696e740000000000000000000000000000000081525060200191505060405180910390fd5b436000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550600554600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540192505081905550600260003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600081548092919060010191905055503373ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167fec61728879a33aa50b55e1f4789dcfc1c680f30a24d7b8694a9f874e242a97b46005546040518082815260200191505060405180910390a3565b60016020528060005260406000206000915090505481565b60026020528060005260406000206000915090505481565b60006020528060005260406000206000915090505481565b60036020528060005260406000206000915090508060000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16908060010154905082565b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101515156105b8576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260148152602001807f696e73756666696369656e742062616c616e636500000000000000000000000081525060200191505060405180910390fd5b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555060408051908101604052803373ffffffffffffffffffffffffffffffffffffffff16815260200182815250600360008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008201518160000160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550602082015181600101559050508173ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fec61728879a33aa50b55e1f4789dcfc1c680f30a24d7b8694a9f874e242a97b4836040518082815260200191505060405180910390a350505600a165627a7a7230582047e5e1380e66d6b109548617ae59ff7baf70ee2d4a6734559b8fc5cabca0870b0029000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000186a0"
chainMetaResponse := &iotexapi.GetChainMetaResponse{ChainMeta: &iotextypes.ChainMeta{}}

client.EXPECT().SelectTranslation(gomock.Any()).Return("action", config.English).Times(4)
client.EXPECT().SetEndpointWithFlag(gomock.Any()).Times(2)
client.EXPECT().SetInsecureWithFlag(gomock.Any()).Times(2)
client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).Times(2)

t.Run("reads smart contract on IoTeX blockchain", func(t *testing.T) {
client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return("test", nil)
apiServiceClient.EXPECT().GetChainMeta(gomock.Any(), gomock.Any()).Return(chainMetaResponse, nil)
apiServiceClient.EXPECT().ReadContract(gomock.Any(), gomock.Any()).Return(&iotexapi.ReadContractResponse{
Data: "test",
}, nil)

result, err := Read(client, accAddr, "100", []byte(bytecode), "test", 100)
require.NoError(err)
require.Equal("test", result)
})

t.Run("failed to get signer address", func(t *testing.T) {
expectErr := errors.New("failed to get signer address")
client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return("", expectErr)
_, err := Read(client, accAddr, "100", []byte(bytecode), "test", 100)
require.Contains(err.Error(), expectErr.Error())
})
}
2 changes: 1 addition & 1 deletion ioctl/newcmd/action/xrc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func parseAmount(client ioctl.Client, cmd *cobra.Command, contract address.Addre
if err != nil {
return nil, errors.Wrap(err, "failed to get flag gas-limit")
}
result, err := Read(client, cmd, contract, "0", decimalBytecode, signer, gasLimit)
result, err := Read(client, contract, "0", decimalBytecode, signer, gasLimit)
if err != nil {
return nil, errors.New("failed to read contract")
}
Expand Down

0 comments on commit b94422c

Please sign in to comment.