github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/bc/bc_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  
    18  	"github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient"
    19  )
    20  
    21  func TestGetBucketList(t *testing.T) {
    22  	require := require.New(t)
    23  	ctrl := gomock.NewController(t)
    24  	client := mock_ioctlclient.NewMockClient(ctrl)
    25  	apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl)
    26  	request := &iotexapi.ReadStakingDataRequest{}
    27  	response := &iotexapi.ReadStateResponse{}
    28  	client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).Times(2)
    29  
    30  	t.Run("get bucket list", func(t *testing.T) {
    31  		expectedValue := &iotextypes.VoteBucketList{
    32  			Buckets: []*iotextypes.VoteBucket{},
    33  		}
    34  		apiServiceClient.EXPECT().ReadState(gomock.Any(), gomock.Any()).Return(response, nil)
    35  
    36  		result, err := GetBucketList(client, iotexapi.ReadStakingDataMethod_BUCKETS_BY_CANDIDATE, request)
    37  		require.NoError(err)
    38  		require.Contains(result.String(), expectedValue.String())
    39  	})
    40  
    41  	t.Run("failed to invoke ReadState api", func(t *testing.T) {
    42  		expectedErr := errors.New("failed to invoke ReadState api")
    43  		apiServiceClient.EXPECT().ReadState(gomock.Any(), gomock.Any()).Return(nil, expectedErr)
    44  
    45  		_, err := GetBucketList(client, iotexapi.ReadStakingDataMethod_BUCKETS_BY_VOTER, request)
    46  		require.Contains(err.Error(), expectedErr.Error())
    47  	})
    48  }