github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/bc/bcinfo_test.go (about) 1 // Copyright (c) 2022 IoTeX 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/ioctl/config" 19 "github.com/iotexproject/iotex-core/ioctl/util" 20 "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" 21 ) 22 23 // test for bc info command 24 func TestNewBCInfoCmd(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 30 client.EXPECT().SelectTranslation(gomock.Any()).Return("", config.English).AnyTimes() 31 client.EXPECT().Config().Return(config.Config{}).AnyTimes() 32 client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).Times(2) 33 34 t.Run("get blockchain info", func(t *testing.T) { 35 chainMetaResponse := &iotexapi.GetChainMetaResponse{ChainMeta: &iotextypes.ChainMeta{}} 36 apiServiceClient.EXPECT().GetChainMeta(gomock.Any(), gomock.Any()).Return(chainMetaResponse, nil).Times(1) 37 38 cmd := NewBCInfoCmd(client) 39 _, err := util.ExecuteCmd(cmd) 40 require.NoError(err) 41 }) 42 43 t.Run("failed to get chain meta", func(t *testing.T) { 44 expectedErr := errors.New("failed to get chain meta") 45 apiServiceClient.EXPECT().GetChainMeta(gomock.Any(), gomock.Any()).Return(nil, expectedErr).Times(1) 46 47 cmd := NewBCInfoCmd(client) 48 _, err := util.ExecuteCmd(cmd) 49 require.Error(err) 50 require.Contains(err.Error(), "failed to get chain meta") 51 }) 52 53 t.Run("failed to dial grpc connection", func(t *testing.T) { 54 expectedErr := errors.New("failed to dial grpc connection") 55 client.EXPECT().APIServiceClient().Return(nil, expectedErr).Times(1) 56 57 cmd := NewBCInfoCmd(client) 58 _, err := util.ExecuteCmd(cmd) 59 require.Error(err) 60 require.Equal(expectedErr, err) 61 }) 62 }