github.com/Finschia/finschia-sdk@v0.48.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  		tc := tc
    66  
    67  		s.Run(name, func() {
    68  			cmd := cli.NewQueryCmdBalance()
    69  			out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...))
    70  			if !tc.valid {
    71  				s.Require().Error(err)
    72  				return
    73  			}
    74  			s.Require().NoError(err)
    75  
    76  			var actual token.QueryBalanceResponse
    77  			s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String())
    78  			s.Require().Equal(tc.expected, &actual)
    79  		})
    80  	}
    81  }
    82  
    83  func (s *IntegrationTestSuite) TestNewQueryCmdToken() {
    84  	val := s.network.Validators[0]
    85  	commonArgs := []string{
    86  		fmt.Sprintf("--%s=%d", flags.FlagHeight, s.setupHeight),
    87  		fmt.Sprintf("--%s=json", ostcli.OutputFlag),
    88  	}
    89  
    90  	testCases := map[string]struct {
    91  		args     []string
    92  		valid    bool
    93  		expected proto.Message
    94  	}{
    95  		"valid query": {
    96  			[]string{
    97  				s.classes[0].Id,
    98  			},
    99  			true,
   100  			&token.QueryContractResponse{
   101  				Contract: s.classes[0],
   102  			},
   103  		},
   104  		"extra args": {
   105  			[]string{
   106  				s.classes[0].Id,
   107  				"extra",
   108  			},
   109  			false,
   110  			nil,
   111  		},
   112  		"not enough args": {
   113  			[]string{},
   114  			false,
   115  			nil,
   116  		},
   117  	}
   118  
   119  	for name, tc := range testCases {
   120  		tc := tc
   121  
   122  		s.Run(name, func() {
   123  			cmd := cli.NewQueryCmdContract()
   124  			out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...))
   125  			if !tc.valid {
   126  				s.Require().Error(err)
   127  				return
   128  			}
   129  			s.Require().NoError(err)
   130  
   131  			var actual token.QueryContractResponse
   132  			s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String())
   133  			s.Require().Equal(tc.expected, &actual)
   134  		})
   135  	}
   136  }
   137  
   138  func (s *IntegrationTestSuite) TestNewQueryCmdGranteeGrants() {
   139  	val := s.network.Validators[0]
   140  	commonArgs := []string{
   141  		fmt.Sprintf("--%s=%d", flags.FlagHeight, s.setupHeight),
   142  		fmt.Sprintf("--%s=json", ostcli.OutputFlag),
   143  	}
   144  
   145  	testCases := map[string]struct {
   146  		args     []string
   147  		valid    bool
   148  		expected proto.Message
   149  	}{
   150  		"valid query": {
   151  			[]string{
   152  				s.classes[0].Id,
   153  				s.vendor.String(),
   154  			},
   155  			true,
   156  			&token.QueryGranteeGrantsResponse{
   157  				Grants: []token.Grant{
   158  					{
   159  						Grantee:    s.vendor.String(),
   160  						Permission: token.PermissionModify,
   161  					},
   162  					{
   163  						Grantee:    s.vendor.String(),
   164  						Permission: token.PermissionMint,
   165  					},
   166  					{
   167  						Grantee:    s.vendor.String(),
   168  						Permission: token.PermissionBurn,
   169  					},
   170  				},
   171  				Pagination: &query.PageResponse{
   172  					Total: 3,
   173  				},
   174  			},
   175  		},
   176  		"extra args": {
   177  			[]string{
   178  				s.classes[0].Id,
   179  				s.vendor.String(),
   180  				"extra",
   181  			},
   182  			false,
   183  			nil,
   184  		},
   185  		"not enough args": {
   186  			[]string{
   187  				s.classes[0].Id,
   188  			},
   189  			false,
   190  			nil,
   191  		},
   192  	}
   193  
   194  	for name, tc := range testCases {
   195  		tc := tc
   196  
   197  		s.Run(name, func() {
   198  			cmd := cli.NewQueryCmdGranteeGrants()
   199  			out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...))
   200  			if !tc.valid {
   201  				s.Require().Error(err)
   202  				return
   203  			}
   204  			s.Require().NoError(err)
   205  
   206  			var actual token.QueryGranteeGrantsResponse
   207  			s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String())
   208  			s.Require().Equal(tc.expected, &actual)
   209  		})
   210  	}
   211  }
   212  
   213  func (s *IntegrationTestSuite) TestNewQueryCmdIsOperatorFor() {
   214  	val := s.network.Validators[0]
   215  	commonArgs := []string{
   216  		fmt.Sprintf("--%s=%d", flags.FlagHeight, s.setupHeight),
   217  		fmt.Sprintf("--%s=json", ostcli.OutputFlag),
   218  	}
   219  
   220  	testCases := map[string]struct {
   221  		args     []string
   222  		valid    bool
   223  		expected proto.Message
   224  	}{
   225  		"valid query": {
   226  			[]string{
   227  				s.classes[0].Id,
   228  				s.vendor.String(),
   229  				s.customer.String(),
   230  			},
   231  			true,
   232  			&token.QueryIsOperatorForResponse{
   233  				Authorized: true,
   234  			},
   235  		},
   236  		"extra args": {
   237  			[]string{
   238  				s.classes[0].Id,
   239  				s.vendor.String(),
   240  				s.customer.String(),
   241  				"extra",
   242  			},
   243  			false,
   244  			nil,
   245  		},
   246  		"not enough args": {
   247  			[]string{
   248  				s.classes[0].Id,
   249  				s.vendor.String(),
   250  			},
   251  			false,
   252  			nil,
   253  		},
   254  	}
   255  
   256  	for name, tc := range testCases {
   257  		tc := tc
   258  
   259  		s.Run(name, func() {
   260  			cmd := cli.NewQueryCmdIsOperatorFor()
   261  			out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...))
   262  			if !tc.valid {
   263  				s.Require().Error(err)
   264  				return
   265  			}
   266  			s.Require().NoError(err)
   267  
   268  			var actual token.QueryIsOperatorForResponse
   269  			s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String())
   270  			s.Require().Equal(tc.expected, &actual)
   271  		})
   272  	}
   273  }
   274  
   275  func (s *IntegrationTestSuite) TestNewQueryCmdHoldersByOperator() {
   276  	val := s.network.Validators[0]
   277  	commonArgs := []string{
   278  		fmt.Sprintf("--%s=%d", flags.FlagHeight, s.setupHeight),
   279  		fmt.Sprintf("--%s=json", ostcli.OutputFlag),
   280  	}
   281  
   282  	testCases := map[string]struct {
   283  		args     []string
   284  		valid    bool
   285  		expected proto.Message
   286  	}{
   287  		"valid query": {
   288  			[]string{
   289  				s.classes[0].Id,
   290  				s.vendor.String(),
   291  			},
   292  			true,
   293  			&token.QueryHoldersByOperatorResponse{
   294  				Holders:    []string{s.customer.String()},
   295  				Pagination: &query.PageResponse{},
   296  			},
   297  		},
   298  		"extra args": {
   299  			[]string{
   300  				s.classes[0].Id,
   301  				s.vendor.String(),
   302  				"extra",
   303  			},
   304  			false,
   305  			nil,
   306  		},
   307  		"not enough args": {
   308  			[]string{
   309  				s.classes[0].Id,
   310  			},
   311  			false,
   312  			nil,
   313  		},
   314  	}
   315  
   316  	for name, tc := range testCases {
   317  		tc := tc
   318  
   319  		s.Run(name, func() {
   320  			cmd := cli.NewQueryCmdHoldersByOperator()
   321  			out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, append(tc.args, commonArgs...))
   322  			if !tc.valid {
   323  				s.Require().Error(err)
   324  				return
   325  			}
   326  			s.Require().NoError(err)
   327  
   328  			var actual token.QueryHoldersByOperatorResponse
   329  			s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &actual), out.String())
   330  			s.Require().Equal(tc.expected, &actual)
   331  		})
   332  	}
   333  }