github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/account/accountnonce_test.go (about) 1 // Copyright (c) 2019 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 "fmt" 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/iotexproject/iotex-proto/golang/iotextypes" 16 "github.com/pkg/errors" 17 "github.com/stretchr/testify/require" 18 19 "github.com/iotexproject/iotex-core/ioctl/config" 20 "github.com/iotexproject/iotex-core/ioctl/util" 21 "github.com/iotexproject/iotex-core/test/identityset" 22 "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" 23 ) 24 25 func TestNewAccountNonce(t *testing.T) { 26 accountNoneTests := []struct { 27 // input 28 inAddr string 29 // output 30 outPendingNonce int 31 }{ 32 { 33 inAddr: "", 34 outPendingNonce: 0, 35 }, 36 { 37 inAddr: "io1cjh35tq9k8fu0gqcsat4px7yr8trh75c95hc5r", 38 outPendingNonce: 1, 39 }, 40 { 41 inAddr: "io187evpmjdankjh0g5dfz83w2z3p23ljhn4s9jw7", 42 outPendingNonce: 3, 43 }, 44 } 45 46 require := require.New(t) 47 ctrl := gomock.NewController(t) 48 defer ctrl.Finish() 49 client := mock_ioctlclient.NewMockClient(ctrl) 50 client.EXPECT().SelectTranslation(gomock.Any()).Return("", config.English).AnyTimes() 51 52 accAddr := identityset.Address(28).String() 53 client.EXPECT().Config().Return(config.Config{}).AnyTimes() 54 apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl) 55 56 // success 57 for i := 0; i < len(accountNoneTests); i++ { 58 client.EXPECT().APIServiceClient().Return(apiServiceClient, nil) 59 client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return(accAddr, nil) 60 accountResponse := &iotexapi.GetAccountResponse{AccountMeta: &iotextypes.AccountMeta{ 61 Address: accAddr, 62 PendingNonce: uint64(accountNoneTests[i].outPendingNonce), 63 }} 64 apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(accountResponse, nil) 65 66 cmd := NewAccountNonce(client) 67 result, err := util.ExecuteCmd(cmd, accountNoneTests[i].inAddr) 68 require.NoError(err) 69 require.Contains(result, fmt.Sprintf("Pending Nonce: %d", accountNoneTests[i].outPendingNonce)) 70 } 71 72 // fail to get account addr 73 expectedErr := errors.New("failed to get address") 74 client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return("", expectedErr) 75 cmd := NewAccountNonce(client) 76 _, err := util.ExecuteCmd(cmd) 77 require.Contains(err.Error(), expectedErr.Error()) 78 79 // fail to dial grpc 80 expectedErr = errors.New("failed to dial grpc connection") 81 client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return(accAddr, nil) 82 client.EXPECT().APIServiceClient().Return(nil, expectedErr) 83 cmd = NewAccountNonce(client) 84 _, err = util.ExecuteCmd(cmd) 85 require.Contains(err.Error(), expectedErr.Error()) 86 87 // fail to invoke grpc api 88 expectedErr = errors.New("failed to invoke GetAccount api") 89 client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return(accAddr, nil) 90 client.EXPECT().APIServiceClient().Return(apiServiceClient, nil) 91 apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(nil, expectedErr) 92 cmd = NewAccountNonce(client) 93 _, err = util.ExecuteCmd(cmd) 94 require.Contains(err.Error(), expectedErr.Error()) 95 }