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

     1  package testutil
     2  
     3  import (
     4  	"fmt"
     5  
     6  	ostcli "github.com/Finschia/ostracon/libs/cli"
     7  	"github.com/gogo/protobuf/proto"
     8  
     9  	"github.com/Finschia/finschia-sdk/client/flags"
    10  	clitestutil "github.com/Finschia/finschia-sdk/testutil/cli"
    11  	"github.com/Finschia/finschia-sdk/types/query"
    12  	"github.com/Finschia/finschia-sdk/x/token"
    13  	"github.com/Finschia/finschia-sdk/x/token/client/cli"
    14  )
    15  
    16  func (s *IntegrationTestSuite) TestNewQueryCmdBalance() {
    17  	val := s.network.Validators[0]
    18  	commonArgs := []string{
    19  		fmt.Sprintf("--%s=%d", flags.FlagHeight, s.setupHeight),
    20  		fmt.Sprintf("--%s=json", ostcli.OutputFlag),
    21  	}
    22  
    23  	testCases := map[string]struct {
    24  		args     []string
    25  		valid    bool
    26  		expected proto.Message
    27  	}{
    28  		"valid query": {
    29  			[]string{
    30  				s.classes[0].Id,
    31  				s.customer.String(),
    32  			},
    33  			true,
    34  			&token.QueryBalanceResponse{
    35  				Amount: s.balance,
    36  			},
    37  		},
    38  		"extra args": {
    39  			[]string{
    40  				s.classes[0].Id,
    41  				s.customer.String(),
    42  				"extra",
    43  			},
    44  			false,
    45  			nil,
    46  		},
    47  		"not enough args": {
    48  			[]string{
    49  				s.classes[0].Id,
    50  			},
    51  			false,
    52  			nil,
    53  		},
    54  		"invalid address": {
    55  			[]string{
    56  				s.classes[0].Id,
    57  				"invalid",
    58  			},
    59  			false,
    60  			nil,
    61  		},
    62  	}
    63  
    64  	for name, tc := range testCases {
    65  		s.Run(name, func() {
    66  			cmd := cli.NewQueryCmdBalance()
    67  			out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...))
    68  			if !tc.valid {
    69  				s.Require().Error(err)
    70  				return
    71  			}
    72  			s.Require().NoError(err)
    73  
    74  			var actual token.QueryBalanceResponse
    75  			s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String())
    76  			s.Require().Equal(tc.expected, &actual)
    77  		})
    78  	}
    79  }
    80  
    81  func (s *IntegrationTestSuite) TestNewQueryCmdToken() {
    82  	val := s.network.Validators[0]
    83  	commonArgs := []string{
    84  		fmt.Sprintf("--%s=%d", flags.FlagHeight, s.setupHeight),
    85  		fmt.Sprintf("--%s=json", ostcli.OutputFlag),
    86  	}
    87  
    88  	testCases := map[string]struct {
    89  		args     []string
    90  		valid    bool
    91  		expected proto.Message
    92  	}{
    93  		"valid query": {
    94  			[]string{
    95  				s.classes[0].Id,
    96  			},
    97  			true,
    98  			&token.QueryContractResponse{
    99  				Contract: s.classes[0],
   100  			},
   101  		},
   102  		"extra args": {
   103  			[]string{
   104  				s.classes[0].Id,
   105  				"extra",
   106  			},
   107  			false,
   108  			nil,
   109  		},
   110  		"not enough args": {
   111  			[]string{},
   112  			false,
   113  			nil,
   114  		},
   115  	}
   116  
   117  	for name, tc := range testCases {
   118  		s.Run(name, func() {
   119  			cmd := cli.NewQueryCmdContract()
   120  			out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...))
   121  			if !tc.valid {
   122  				s.Require().Error(err)
   123  				return
   124  			}
   125  			s.Require().NoError(err)
   126  
   127  			var actual token.QueryContractResponse
   128  			s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String())
   129  			s.Require().Equal(tc.expected, &actual)
   130  		})
   131  	}
   132  }
   133  
   134  func (s *IntegrationTestSuite) TestNewQueryCmdGranteeGrants() {
   135  	val := s.network.Validators[0]
   136  	commonArgs := []string{
   137  		fmt.Sprintf("--%s=%d", flags.FlagHeight, s.setupHeight),
   138  		fmt.Sprintf("--%s=json", ostcli.OutputFlag),
   139  	}
   140  
   141  	testCases := map[string]struct {
   142  		args     []string
   143  		valid    bool
   144  		expected proto.Message
   145  	}{
   146  		"valid query": {
   147  			[]string{
   148  				s.classes[0].Id,
   149  				s.vendor.String(),
   150  			},
   151  			true,
   152  			&token.QueryGranteeGrantsResponse{
   153  				Grants: []token.Grant{
   154  					{
   155  						Grantee:    s.vendor.String(),
   156  						Permission: token.PermissionModify,
   157  					},
   158  					{
   159  						Grantee:    s.vendor.String(),
   160  						Permission: token.PermissionMint,
   161  					},
   162  					{
   163  						Grantee:    s.vendor.String(),
   164  						Permission: token.PermissionBurn,
   165  					},
   166  				},
   167  				Pagination: &query.PageResponse{
   168  					Total: 3,
   169  				},
   170  			},
   171  		},
   172  		"extra args": {
   173  			[]string{
   174  				s.classes[0].Id,
   175  				s.vendor.String(),
   176  				"extra",
   177  			},
   178  			false,
   179  			nil,
   180  		},
   181  		"not enough args": {
   182  			[]string{
   183  				s.classes[0].Id,
   184  			},
   185  			false,
   186  			nil,
   187  		},
   188  	}
   189  
   190  	for name, tc := range testCases {
   191  		s.Run(name, func() {
   192  			cmd := cli.NewQueryCmdGranteeGrants()
   193  			out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...))
   194  			if !tc.valid {
   195  				s.Require().Error(err)
   196  				return
   197  			}
   198  			s.Require().NoError(err)
   199  
   200  			var actual token.QueryGranteeGrantsResponse
   201  			s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String())
   202  			s.Require().Equal(tc.expected, &actual)
   203  		})
   204  	}
   205  }
   206  
   207  func (s *IntegrationTestSuite) TestNewQueryCmdIsOperatorFor() {
   208  	val := s.network.Validators[0]
   209  	commonArgs := []string{
   210  		fmt.Sprintf("--%s=%d", flags.FlagHeight, s.setupHeight),
   211  		fmt.Sprintf("--%s=json", ostcli.OutputFlag),
   212  	}
   213  
   214  	testCases := map[string]struct {
   215  		args     []string
   216  		valid    bool
   217  		expected proto.Message
   218  	}{
   219  		"valid query": {
   220  			[]string{
   221  				s.classes[0].Id,
   222  				s.vendor.String(),
   223  				s.customer.String(),
   224  			},
   225  			true,
   226  			&token.QueryIsOperatorForResponse{
   227  				Authorized: true,
   228  			},
   229  		},
   230  		"extra args": {
   231  			[]string{
   232  				s.classes[0].Id,
   233  				s.vendor.String(),
   234  				s.customer.String(),
   235  				"extra",
   236  			},
   237  			false,
   238  			nil,
   239  		},
   240  		"not enough args": {
   241  			[]string{
   242  				s.classes[0].Id,
   243  				s.vendor.String(),
   244  			},
   245  			false,
   246  			nil,
   247  		},
   248  	}
   249  
   250  	for name, tc := range testCases {
   251  		s.Run(name, func() {
   252  			cmd := cli.NewQueryCmdIsOperatorFor()
   253  			out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...))
   254  			if !tc.valid {
   255  				s.Require().Error(err)
   256  				return
   257  			}
   258  			s.Require().NoError(err)
   259  
   260  			var actual token.QueryIsOperatorForResponse
   261  			s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String())
   262  			s.Require().Equal(tc.expected, &actual)
   263  		})
   264  	}
   265  }
   266  
   267  func (s *IntegrationTestSuite) TestNewQueryCmdHoldersByOperator() {
   268  	val := s.network.Validators[0]
   269  	commonArgs := []string{
   270  		fmt.Sprintf("--%s=%d", flags.FlagHeight, s.setupHeight),
   271  		fmt.Sprintf("--%s=json", ostcli.OutputFlag),
   272  	}
   273  
   274  	testCases := map[string]struct {
   275  		args     []string
   276  		valid    bool
   277  		expected proto.Message
   278  	}{
   279  		"valid query": {
   280  			[]string{
   281  				s.classes[0].Id,
   282  				s.vendor.String(),
   283  			},
   284  			true,
   285  			&token.QueryHoldersByOperatorResponse{
   286  				Holders:    []string{s.customer.String()},
   287  				Pagination: &query.PageResponse{},
   288  			},
   289  		},
   290  		"extra args": {
   291  			[]string{
   292  				s.classes[0].Id,
   293  				s.vendor.String(),
   294  				"extra",
   295  			},
   296  			false,
   297  			nil,
   298  		},
   299  		"not enough args": {
   300  			[]string{
   301  				s.classes[0].Id,
   302  			},
   303  			false,
   304  			nil,
   305  		},
   306  	}
   307  
   308  	for name, tc := range testCases {
   309  		s.Run(name, func() {
   310  			cmd := cli.NewQueryCmdHoldersByOperator()
   311  			out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...))
   312  			if !tc.valid {
   313  				s.Require().Error(err)
   314  				return
   315  			}
   316  			s.Require().NoError(err)
   317  
   318  			var actual token.QueryHoldersByOperatorResponse
   319  			s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String())
   320  			s.Require().Equal(tc.expected, &actual)
   321  		})
   322  	}
   323  }