github.com/Finschia/finschia-sdk@v0.49.1/x/fswap/client/testutil/query.go (about)

     1  package testutil
     2  
     3  import (
     4  	"github.com/gogo/protobuf/proto"
     5  
     6  	clitestutil "github.com/Finschia/finschia-sdk/testutil/cli"
     7  	sdk "github.com/Finschia/finschia-sdk/types"
     8  	"github.com/Finschia/finschia-sdk/types/query"
     9  	"github.com/Finschia/finschia-sdk/x/fswap/client/cli"
    10  	fswaptypes "github.com/Finschia/finschia-sdk/x/fswap/types"
    11  )
    12  
    13  func (s *IntegrationTestSuite) TestCmdQuerySwapped() {
    14  	val := s.network.Validators[0]
    15  	clientCtx := val.ClientCtx
    16  	// avoid printing as yaml from CLI command
    17  	clientCtx.OutputFormat = jsonOutputFormat
    18  
    19  	fromDenom := s.cfg.BondDenom
    20  	toDenom := s.toDenom.Base
    21  
    22  	testCases := []struct {
    23  		name      string
    24  		args      []string
    25  		expectErr bool
    26  		expected  proto.Message
    27  	}{
    28  		{
    29  			"valid query",
    30  			[]string{fromDenom, toDenom},
    31  			false,
    32  			&fswaptypes.QuerySwappedResponse{
    33  				FromCoinAmount: sdk.NewCoin(fromDenom, sdk.ZeroInt()),
    34  				ToCoinAmount:   sdk.NewCoin(toDenom, sdk.ZeroInt()),
    35  			},
    36  		},
    37  		{
    38  			"wrong number of args",
    39  			[]string{fromDenom, toDenom, "extra"},
    40  			true,
    41  			nil,
    42  		},
    43  		{
    44  			"invalid fromDenom",
    45  			[]string{"", toDenom},
    46  			true,
    47  			nil,
    48  		},
    49  		{
    50  			"invalid toDenom",
    51  			[]string{fromDenom, ""},
    52  			true,
    53  			nil,
    54  		},
    55  	}
    56  
    57  	for _, tc := range testCases {
    58  		s.Run(tc.name, func() {
    59  			cmd := cli.CmdQuerySwapped()
    60  			out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
    61  
    62  			if tc.expectErr {
    63  				s.Require().Error(err)
    64  			} else {
    65  				s.Require().NoError(err)
    66  				var actual fswaptypes.QuerySwappedResponse
    67  				s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual))
    68  				s.Require().Equal(tc.expected, &actual)
    69  			}
    70  		})
    71  	}
    72  }
    73  
    74  func (s *IntegrationTestSuite) TestCmdQueryTotalSwappableAmount() {
    75  	val := s.network.Validators[0]
    76  	clientCtx := val.ClientCtx
    77  	// avoid printing as yaml from CLI command
    78  	clientCtx.OutputFormat = jsonOutputFormat
    79  
    80  	fromDenom := s.cfg.BondDenom
    81  	toDenom := s.toDenom.Base
    82  
    83  	testCases := []struct {
    84  		name      string
    85  		args      []string
    86  		expectErr bool
    87  		expected  proto.Message
    88  	}{
    89  		{
    90  			"valid query",
    91  			[]string{fromDenom, toDenom},
    92  			false,
    93  			&fswaptypes.QueryTotalSwappableToCoinAmountResponse{
    94  				SwappableAmount: sdk.NewCoin(toDenom, s.dummySwap.AmountCapForToDenom),
    95  			},
    96  		},
    97  		{
    98  			"wrong number of args",
    99  			[]string{fromDenom, toDenom, "extra"},
   100  			true,
   101  			nil,
   102  		},
   103  		{
   104  			"invalid fromDenom",
   105  			[]string{"", toDenom},
   106  			true,
   107  			nil,
   108  		},
   109  		{
   110  			"invalid toDenom",
   111  			[]string{fromDenom, ""},
   112  			true,
   113  			nil,
   114  		},
   115  	}
   116  
   117  	for _, tc := range testCases {
   118  		s.Run(tc.name, func() {
   119  			cmd := cli.CmdQueryTotalSwappableAmount()
   120  			out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
   121  
   122  			if tc.expectErr {
   123  				s.Require().Error(err)
   124  			} else {
   125  				s.Require().NoError(err)
   126  				var actual fswaptypes.QueryTotalSwappableToCoinAmountResponse
   127  				s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual))
   128  				s.Require().Equal(tc.expected, &actual)
   129  			}
   130  		})
   131  	}
   132  }
   133  
   134  func (s *IntegrationTestSuite) TestCmdQuerySwap() {
   135  	val := s.network.Validators[0]
   136  	clientCtx := val.ClientCtx
   137  	// avoid printing as yaml from CLI command
   138  	clientCtx.OutputFormat = jsonOutputFormat
   139  
   140  	fromDenom := s.cfg.BondDenom
   141  	toDenom := s.toDenom.Base
   142  
   143  	testCases := []struct {
   144  		name      string
   145  		args      []string
   146  		expectErr bool
   147  		expected  proto.Message
   148  	}{
   149  		{
   150  			"valid query",
   151  			[]string{fromDenom, toDenom},
   152  			false,
   153  			&fswaptypes.QuerySwapResponse{
   154  				Swap: s.dummySwap,
   155  			},
   156  		},
   157  		{
   158  			"wrong number of args",
   159  			[]string{fromDenom, toDenom, "extra"},
   160  			true,
   161  			nil,
   162  		},
   163  		{
   164  			"invalid fromDenom",
   165  			[]string{"", toDenom},
   166  			true,
   167  			nil,
   168  		},
   169  		{
   170  			"invalid toDenom",
   171  			[]string{fromDenom, ""},
   172  			true,
   173  			nil,
   174  		},
   175  	}
   176  
   177  	for _, tc := range testCases {
   178  		s.Run(tc.name, func() {
   179  			cmd := cli.CmdQuerySwap()
   180  			out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
   181  
   182  			if tc.expectErr {
   183  				s.Require().Error(err)
   184  			} else {
   185  				s.Require().NoError(err)
   186  				var actual fswaptypes.QuerySwapResponse
   187  				s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual))
   188  				s.Require().Equal(tc.expected, &actual)
   189  			}
   190  		})
   191  	}
   192  }
   193  
   194  func (s *IntegrationTestSuite) TestCmdQuerySwaps() {
   195  	val := s.network.Validators[0]
   196  	clientCtx := val.ClientCtx
   197  	// avoid printing as yaml from CLI command
   198  	clientCtx.OutputFormat = jsonOutputFormat
   199  
   200  	testCases := []struct {
   201  		name      string
   202  		args      []string
   203  		expectErr bool
   204  		expected  proto.Message
   205  	}{
   206  		{
   207  			"valid query (default pagination)",
   208  			[]string{},
   209  			false,
   210  			&fswaptypes.QuerySwapsResponse{
   211  				Swaps:      []fswaptypes.Swap{s.dummySwap},
   212  				Pagination: &query.PageResponse{Total: 1},
   213  			},
   214  		},
   215  		{
   216  			"invalid query",
   217  			[]string{"extra"},
   218  			true,
   219  			nil,
   220  		},
   221  	}
   222  
   223  	for _, tc := range testCases {
   224  		s.Run(tc.name, func() {
   225  			cmd := cli.CmdQuerySwaps()
   226  			out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
   227  
   228  			if tc.expectErr {
   229  				s.Require().Error(err)
   230  			} else {
   231  				s.Require().NoError(err)
   232  				var actual fswaptypes.QuerySwapsResponse
   233  				s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual))
   234  				s.Require().Equal(tc.expected, &actual)
   235  			}
   236  		})
   237  	}
   238  }