github.com/Finschia/finschia-sdk@v0.49.1/x/auth/tx2/service_test.go (about) 1 package tx2_test 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/stretchr/testify/suite" 9 10 "github.com/Finschia/finschia-sdk/client/flags" 11 "github.com/Finschia/finschia-sdk/testutil/network" 12 "github.com/Finschia/finschia-sdk/testutil/rest" 13 sdk "github.com/Finschia/finschia-sdk/types" 14 "github.com/Finschia/finschia-sdk/types/query" 15 "github.com/Finschia/finschia-sdk/types/tx2" 16 bankcli "github.com/Finschia/finschia-sdk/x/bank/client/testutil" 17 ) 18 19 type IntegrationTestSuite struct { 20 suite.Suite 21 22 cfg network.Config 23 network *network.Network 24 25 txHeight int64 26 queryClient tx2.ServiceClient 27 txRes sdk.TxResponse 28 } 29 30 func (s *IntegrationTestSuite) SetupSuite() { 31 s.T().Log("setting up integration test suite") 32 33 cfg := network.DefaultConfig() 34 cfg.NumValidators = 1 35 36 s.cfg = cfg 37 s.network = network.New(s.T(), cfg) 38 s.Require().NotNil(s.network) 39 40 val := s.network.Validators[0] 41 42 _, err := s.network.WaitForHeight(1) 43 s.Require().NoError(err) 44 45 s.queryClient = tx2.NewServiceClient(val.ClientCtx) 46 47 // Create a new MsgSend tx from val to itself. 48 out, err := bankcli.MsgSendExec( 49 val.ClientCtx, 50 val.Address, 51 val.Address, 52 sdk.NewCoins( 53 sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10)), 54 ), 55 fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), 56 fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), 57 fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), 58 fmt.Sprintf("--gas=%d", flags.DefaultGasLimit), 59 fmt.Sprintf("--%s=foobar", flags.FlagNote), 60 ) 61 s.Require().NoError(err) 62 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &s.txRes)) 63 s.Require().Equal(uint32(0), s.txRes.Code) 64 65 out, err = bankcli.MsgSendExec( 66 val.ClientCtx, 67 val.Address, 68 val.Address, 69 sdk.NewCoins( 70 sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(1)), 71 ), 72 fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), 73 fmt.Sprintf("--%s=2", flags.FlagSequence), 74 fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), 75 fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), 76 fmt.Sprintf("--gas=%d", flags.DefaultGasLimit), 77 fmt.Sprintf("--%s=foobar", flags.FlagNote), 78 ) 79 s.Require().NoError(err) 80 var tr sdk.TxResponse 81 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &tr)) 82 s.Require().Equal(uint32(0), tr.Code) 83 84 s.Require().NoError(s.network.WaitForNextBlock()) 85 height, err := s.network.LatestHeight() 86 s.Require().NoError(err) 87 s.txHeight = height 88 fmt.Printf("s.txHeight: %d\n", height) 89 } 90 91 func (s *IntegrationTestSuite) TearDownSuite() { 92 s.T().Log("tearing down integration test suite") 93 s.network.Cleanup() 94 } 95 96 func (s *IntegrationTestSuite) TestGetBlockWithTxs_GRPC() { 97 testCases := []struct { 98 name string 99 req *tx2.GetBlockWithTxsRequest 100 expErr bool 101 expErrMsg string 102 expTxsLen int 103 }{ 104 {"nil request", nil, true, "request cannot be nil", 0}, 105 {"empty request", &tx2.GetBlockWithTxsRequest{}, true, "height must not be less than 1 or greater than the current height", 0}, 106 {"bad height", &tx2.GetBlockWithTxsRequest{Height: 99999999}, true, "height must not be less than 1 or greater than the current height", 0}, 107 {"bad pagination", &tx2.GetBlockWithTxsRequest{Height: s.txHeight, Pagination: &query.PageRequest{Offset: 1000, Limit: 100}}, true, "out of range", 0}, 108 {"good request", &tx2.GetBlockWithTxsRequest{Height: s.txHeight}, false, "", 1}, 109 {"with pagination request", &tx2.GetBlockWithTxsRequest{Height: s.txHeight, Pagination: &query.PageRequest{Offset: 0, Limit: 1}}, false, "", 1}, 110 {"page all request", &tx2.GetBlockWithTxsRequest{Height: s.txHeight, Pagination: &query.PageRequest{Offset: 0, Limit: 100}}, false, "", 1}, 111 {"block with 0 tx", &tx2.GetBlockWithTxsRequest{Height: s.txHeight - 1, Pagination: &query.PageRequest{Offset: 0, Limit: 100}}, false, "", 0}, 112 } 113 for _, tc := range testCases { 114 s.Run(tc.name, func() { 115 // Query the tx via gRPC. 116 grpcRes, err := s.queryClient.GetBlockWithTxs(context.Background(), tc.req) 117 if tc.expErr { 118 s.Require().Error(err) 119 s.Require().Contains(err.Error(), tc.expErrMsg) 120 } else { 121 s.Require().NoError(err) 122 if tc.expTxsLen > 0 { 123 s.Require().Equal("foobar", grpcRes.Txs[0].Body.Memo) 124 } 125 s.Require().Equal(grpcRes.Block.Header.Height, tc.req.Height) 126 if tc.req.Pagination != nil { 127 s.Require().LessOrEqual(len(grpcRes.Txs), int(tc.req.Pagination.Limit)) 128 } 129 } 130 }) 131 } 132 } 133 134 func (s *IntegrationTestSuite) TestGetBlockWithTxs_GRPCGateway() { 135 val := s.network.Validators[0] 136 testCases := []struct { 137 name string 138 url string 139 expErr bool 140 expErrMsg string 141 }{ 142 { 143 "empty params", 144 fmt.Sprintf("%s/lbm/tx/v1beta1/txs/block/0", val.APIAddress), 145 true, "height must not be less than 1 or greater than the current height", 146 }, 147 { 148 "bad height", 149 fmt.Sprintf("%s/lbm/tx/v1beta1/txs/block/%d", val.APIAddress, 9999999), 150 true, "height must not be less than 1 or greater than the current height", 151 }, 152 { 153 "good request", 154 fmt.Sprintf("%s/lbm/tx/v1beta1/txs/block/%d", val.APIAddress, s.txHeight), 155 false, "", 156 }, 157 } 158 for _, tc := range testCases { 159 s.Run(tc.name, func() { 160 res, err := rest.GetRequest(tc.url) 161 s.Require().NoError(err) 162 if tc.expErr { 163 s.Require().Contains(string(res), tc.expErrMsg) 164 } else { 165 var result tx2.GetBlockWithTxsResponse 166 err = val.ClientCtx.Codec.UnmarshalJSON(res, &result) 167 s.Require().NoError(err) 168 s.Require().Equal("foobar", result.Txs[0].Body.Memo) 169 s.Require().Equal(result.Block.Header.Height, s.txHeight) 170 } 171 }) 172 } 173 } 174 175 func TestIntegrationTestSuite(t *testing.T) { 176 suite.Run(t, new(IntegrationTestSuite)) 177 }