github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/bc/bcbucketlist_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 bc 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/pkg/errors" 16 "github.com/stretchr/testify/require" 17 "google.golang.org/protobuf/proto" 18 "google.golang.org/protobuf/types/known/timestamppb" 19 20 "github.com/iotexproject/iotex-core/ioctl/config" 21 "github.com/iotexproject/iotex-core/ioctl/util" 22 "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" 23 "github.com/iotexproject/iotex-core/testutil" 24 ) 25 26 func TestNewBCBucketListCmd(t *testing.T) { 27 require := require.New(t) 28 ctrl := gomock.NewController(t) 29 client := mock_ioctlclient.NewMockClient(ctrl) 30 apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl) 31 32 client.EXPECT().SelectTranslation(gomock.Any()).Return("", config.English).AnyTimes() 33 client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).Times(2) 34 client.EXPECT().Config().Return(config.Config{}).AnyTimes() 35 36 t.Run("get bucket list by voter", func(t *testing.T) { 37 client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return("io1uwnr55vqmhf3xeg5phgurlyl702af6eju542sx", nil) 38 vblist, err := proto.Marshal(&iotextypes.VoteBucketList{ 39 Buckets: []*iotextypes.VoteBucket{ 40 { 41 Index: 1, 42 StakedAmount: "10", 43 UnstakeStartTime: timestamppb.New(testutil.TimestampNow()), 44 }, 45 { 46 Index: 2, 47 StakedAmount: "20", 48 UnstakeStartTime: timestamppb.New(testutil.TimestampNow()), 49 }, 50 }, 51 }) 52 require.NoError(err) 53 apiServiceClient.EXPECT().ReadState(gomock.Any(), gomock.All()).Return(&iotexapi.ReadStateResponse{ 54 Data: vblist, 55 }, nil) 56 57 cmd := NewBCBucketListCmd(client) 58 result, err := util.ExecuteCmd(cmd, "voter", "io1uwnr55vqmhf3xeg5phgurlyl702af6eju542sx") 59 require.NoError(err) 60 require.Contains(result, 61 "index: 1", 62 "stakedAmount: 0.00000000000000001 IOTX", 63 "index: 2", 64 "stakedAmount: 0.00000000000000002 IOTX") 65 }) 66 67 t.Run("get bucket list by candidate", func(t *testing.T) { 68 apiServiceClient.EXPECT().ReadState(gomock.Any(), gomock.All()).Return(&iotexapi.ReadStateResponse{}, nil) 69 cmd := NewBCBucketListCmd(client) 70 result, err := util.ExecuteCmd(cmd, "cand", "io1uwnr55vqmhf3xeg5phgurlyl702af6eju542sx") 71 require.NoError(err) 72 require.Equal("Empty bucketlist with given address\n", result) 73 }) 74 75 t.Run("invalid voter address", func(t *testing.T) { 76 expectedErr := errors.New("cannot find address for alias test") 77 client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return("", expectedErr) 78 cmd := NewBCBucketListCmd(client) 79 _, err := util.ExecuteCmd(cmd, "voter", "test") 80 require.Contains(err.Error(), expectedErr.Error()) 81 }) 82 83 t.Run("unknown method", func(t *testing.T) { 84 expectedErr := errors.New("unknown <method>") 85 cmd := NewBCBucketListCmd(client) 86 _, err := util.ExecuteCmd(cmd, "unknown", "io1uwnr55vqmhf3xeg5phgurlyl702af6eju542sx") 87 require.Equal(expectedErr.Error(), err.Error()) 88 }) 89 90 t.Run("invalid offset", func(t *testing.T) { 91 expectedErr := errors.New("invalid offset") 92 cmd := NewBCBucketListCmd(client) 93 _, err := util.ExecuteCmd(cmd, "voter", "io1uwnr55vqmhf3xeg5phgurlyl702af6eju542sx", "test") 94 require.Contains(err.Error(), expectedErr.Error()) 95 }) 96 97 t.Run("invalid limit", func(t *testing.T) { 98 expectedErr := errors.New("invalid limit") 99 cmd := NewBCBucketListCmd(client) 100 _, err := util.ExecuteCmd(cmd, "voter", "io1uwnr55vqmhf3xeg5phgurlyl702af6eju542sx", "0", "test") 101 require.Contains(err.Error(), expectedErr.Error()) 102 }) 103 }