github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/feesplit/keeper/querier_test.go (about)

     1  package keeper_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/fibonacci-chain/fbc/app/crypto/ethsecp256k1"
     7  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     8  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/query"
     9  	abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types"
    10  	"github.com/fibonacci-chain/fbc/x/feesplit/types"
    11  )
    12  
    13  func (suite *KeeperTestSuite) TestFeeSplits() {
    14  	var (
    15  		req    *types.QueryFeeSplitsRequest
    16  		expRes *types.QueryFeeSplitsResponse
    17  	)
    18  
    19  	testCases := []struct {
    20  		name     string
    21  		path     []string
    22  		malleate func()
    23  		expPass  bool
    24  	}{
    25  		{
    26  			"no fee infos registered",
    27  			[]string{types.QueryFeeSplits},
    28  			func() {
    29  				req = &types.QueryFeeSplitsRequest{}
    30  				expRes = &types.QueryFeeSplitsResponse{Pagination: &query.PageResponse{}}
    31  			},
    32  			true,
    33  		},
    34  		{
    35  			"1 fee infos registered w/pagination",
    36  			[]string{types.QueryFeeSplits},
    37  			func() {
    38  				req = &types.QueryFeeSplitsRequest{
    39  					Pagination: &query.PageRequest{Limit: 10, CountTotal: true},
    40  				}
    41  				feeSplit := types.NewFeeSplit(contract, deployer, withdraw)
    42  				suite.app.FeeSplitKeeper.SetFeeSplit(suite.ctx, feeSplit)
    43  
    44  				expRes = &types.QueryFeeSplitsResponse{
    45  					Pagination: &query.PageResponse{Total: 1},
    46  					FeeSplits: []types.FeeSplitWithShare{
    47  						{
    48  							ContractAddress:   contract.Hex(),
    49  							DeployerAddress:   deployer.String(),
    50  							WithdrawerAddress: withdraw.String(),
    51  							Share:             suite.app.FeeSplitKeeper.GetParams(suite.ctx).DeveloperShares,
    52  						},
    53  					},
    54  				}
    55  			},
    56  			true,
    57  		},
    58  		{
    59  			"2 fee infos registered wo/pagination",
    60  			[]string{types.QueryFeeSplits},
    61  			func() {
    62  				req = &types.QueryFeeSplitsRequest{}
    63  				contract2 := ethsecp256k1.GenerateAddress()
    64  				feeSplit := types.NewFeeSplit(contract, deployer, withdraw)
    65  				feeSplit2 := types.NewFeeSplit(contract2, deployer, nil)
    66  				suite.app.FeeSplitKeeper.SetFeeSplit(suite.ctx, feeSplit)
    67  				suite.app.FeeSplitKeeper.SetFeeSplit(suite.ctx, feeSplit2)
    68  
    69  				expRes = &types.QueryFeeSplitsResponse{
    70  					Pagination: &query.PageResponse{Total: 2},
    71  					FeeSplits: []types.FeeSplitWithShare{
    72  						{
    73  							ContractAddress:   contract.Hex(),
    74  							DeployerAddress:   deployer.String(),
    75  							WithdrawerAddress: withdraw.String(),
    76  							Share:             suite.app.FeeSplitKeeper.GetParams(suite.ctx).DeveloperShares,
    77  						},
    78  						{
    79  							ContractAddress:   contract2.Hex(),
    80  							DeployerAddress:   deployer.String(),
    81  							WithdrawerAddress: deployer.String(),
    82  							Share:             suite.app.FeeSplitKeeper.GetParams(suite.ctx).DeveloperShares,
    83  						},
    84  					},
    85  				}
    86  			},
    87  			true,
    88  		},
    89  	}
    90  	for _, tc := range testCases {
    91  		suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
    92  			suite.SetupTest() // reset
    93  
    94  			tc.malleate()
    95  
    96  			data, err := suite.app.Codec().MarshalJSON(req)
    97  			suite.Require().NoError(err)
    98  			res, err := suite.querier(suite.ctx, tc.path, abci.RequestQuery{Data: data})
    99  			if tc.expPass {
   100  				suite.Require().NoError(err)
   101  
   102  				var resp types.QueryFeeSplitsResponse
   103  				err = suite.app.Codec().UnmarshalJSON(res, &resp)
   104  				suite.Require().NoError(err)
   105  				suite.Require().Equal(expRes.Pagination, resp.Pagination)
   106  				suite.Require().ElementsMatch(expRes.FeeSplits, resp.FeeSplits)
   107  			} else {
   108  				suite.Require().Error(err)
   109  			}
   110  		})
   111  	}
   112  }
   113  
   114  func (suite *KeeperTestSuite) TestFee() {
   115  	var (
   116  		req    *types.QueryFeeSplitRequest
   117  		expRes *types.QueryFeeSplitResponse
   118  	)
   119  
   120  	testCases := []struct {
   121  		name     string
   122  		path     []string
   123  		malleate func()
   124  		expPass  bool
   125  	}{
   126  		{
   127  			"empty contract address",
   128  			[]string{types.QueryFeeSplit},
   129  			func() {
   130  				req = &types.QueryFeeSplitRequest{}
   131  				expRes = &types.QueryFeeSplitResponse{}
   132  			},
   133  			false,
   134  		},
   135  		{
   136  			"invalid contract address",
   137  			[]string{types.QueryFeeSplit},
   138  			func() {
   139  				req = &types.QueryFeeSplitRequest{
   140  					ContractAddress: "1234",
   141  				}
   142  				expRes = &types.QueryFeeSplitResponse{}
   143  			},
   144  			false,
   145  		},
   146  		{
   147  			"fee info not found",
   148  			[]string{types.QueryFeeSplit},
   149  			func() {
   150  				req = &types.QueryFeeSplitRequest{
   151  					ContractAddress: contract.String(),
   152  				}
   153  				expRes = &types.QueryFeeSplitResponse{}
   154  			},
   155  			false,
   156  		},
   157  		{
   158  			"fee info found",
   159  			[]string{types.QueryFeeSplit},
   160  			func() {
   161  				feeSplit := types.NewFeeSplit(contract, deployer, withdraw)
   162  				suite.app.FeeSplitKeeper.SetFeeSplit(suite.ctx, feeSplit)
   163  
   164  				req = &types.QueryFeeSplitRequest{
   165  					ContractAddress: contract.Hex(),
   166  				}
   167  				expRes = &types.QueryFeeSplitResponse{FeeSplit: types.FeeSplitWithShare{
   168  					ContractAddress:   contract.Hex(),
   169  					DeployerAddress:   deployer.String(),
   170  					WithdrawerAddress: withdraw.String(),
   171  					Share:             suite.app.FeeSplitKeeper.GetParams(suite.ctx).DeveloperShares,
   172  				}}
   173  			},
   174  			true,
   175  		},
   176  	}
   177  	for _, tc := range testCases {
   178  		suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
   179  			suite.SetupTest() // reset
   180  
   181  			tc.malleate()
   182  
   183  			data, err := suite.app.Codec().MarshalJSON(req)
   184  			suite.Require().NoError(err)
   185  			res, err := suite.querier(suite.ctx, tc.path, abci.RequestQuery{Data: data})
   186  			if tc.expPass {
   187  				suite.Require().NoError(err)
   188  
   189  				var resp types.QueryFeeSplitResponse
   190  				err = suite.app.Codec().UnmarshalJSON(res, &resp)
   191  				suite.Require().NoError(err)
   192  				suite.Require().Equal(expRes, &resp)
   193  			} else {
   194  				suite.Require().Error(err)
   195  			}
   196  		})
   197  	}
   198  }
   199  
   200  func (suite *KeeperTestSuite) TestDeployerFees() {
   201  	var (
   202  		req    *types.QueryDeployerFeeSplitsRequest
   203  		expRes *types.QueryDeployerFeeSplitsResponse
   204  	)
   205  
   206  	testCases := []struct {
   207  		name     string
   208  		path     []string
   209  		malleate func()
   210  		expPass  bool
   211  	}{
   212  		{
   213  			"no contract registered",
   214  			[]string{types.QueryDeployerFeeSplits},
   215  			func() {
   216  				req = &types.QueryDeployerFeeSplitsRequest{}
   217  				expRes = &types.QueryDeployerFeeSplitsResponse{Pagination: &query.PageResponse{}}
   218  			},
   219  			false,
   220  		},
   221  		{
   222  			"invalid deployer address",
   223  			[]string{types.QueryDeployerFeeSplits},
   224  			func() {
   225  				req = &types.QueryDeployerFeeSplitsRequest{
   226  					DeployerAddress: "123",
   227  				}
   228  				expRes = &types.QueryDeployerFeeSplitsResponse{Pagination: &query.PageResponse{}}
   229  			},
   230  			false,
   231  		},
   232  		{
   233  			"1 fee registered w/pagination",
   234  			[]string{types.QueryDeployerFeeSplits},
   235  			func() {
   236  				req = &types.QueryDeployerFeeSplitsRequest{
   237  					Pagination:      &query.PageRequest{Limit: 10, CountTotal: true},
   238  					DeployerAddress: deployer.String(),
   239  				}
   240  
   241  				feeSplit := types.NewFeeSplit(contract, deployer, withdraw)
   242  				suite.app.FeeSplitKeeper.SetFeeSplit(suite.ctx, feeSplit)
   243  				suite.app.FeeSplitKeeper.SetDeployerMap(suite.ctx, deployer, contract)
   244  				suite.app.FeeSplitKeeper.SetWithdrawerMap(suite.ctx, withdraw, contract)
   245  
   246  				expRes = &types.QueryDeployerFeeSplitsResponse{
   247  					Pagination: &query.PageResponse{Total: 1},
   248  					ContractAddresses: []string{
   249  						contract.Hex(),
   250  					},
   251  				}
   252  			},
   253  			true,
   254  		},
   255  		{
   256  			"2 fee infos registered for one contract wo/pagination",
   257  			[]string{types.QueryDeployerFeeSplits},
   258  			func() {
   259  				req = &types.QueryDeployerFeeSplitsRequest{
   260  					DeployerAddress: deployer.String(),
   261  				}
   262  				contract2 := ethsecp256k1.GenerateAddress()
   263  				feeSplit := types.NewFeeSplit(contract, deployer, withdraw)
   264  				suite.app.FeeSplitKeeper.SetFeeSplit(suite.ctx, feeSplit)
   265  				suite.app.FeeSplitKeeper.SetDeployerMap(suite.ctx, deployer, contract)
   266  				suite.app.FeeSplitKeeper.SetWithdrawerMap(suite.ctx, withdraw, contract)
   267  
   268  				feeSplit2 := types.NewFeeSplit(contract2, deployer, nil)
   269  				suite.app.FeeSplitKeeper.SetFeeSplit(suite.ctx, feeSplit2)
   270  				suite.app.FeeSplitKeeper.SetDeployerMap(suite.ctx, deployer, contract2)
   271  
   272  				expRes = &types.QueryDeployerFeeSplitsResponse{
   273  					Pagination: &query.PageResponse{Total: 2},
   274  					ContractAddresses: []string{
   275  						contract.Hex(),
   276  						contract2.Hex(),
   277  					},
   278  				}
   279  			},
   280  			true,
   281  		},
   282  	}
   283  	for _, tc := range testCases {
   284  		suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
   285  			suite.SetupTest() // reset
   286  
   287  			tc.malleate()
   288  
   289  			data, err := suite.app.Codec().MarshalJSON(req)
   290  			suite.Require().NoError(err)
   291  			res, err := suite.querier(suite.ctx, tc.path, abci.RequestQuery{Data: data})
   292  			if tc.expPass {
   293  				suite.Require().NoError(err)
   294  
   295  				var resp types.QueryDeployerFeeSplitsResponse
   296  				err = suite.app.Codec().UnmarshalJSON(res, &resp)
   297  				suite.Require().NoError(err)
   298  				suite.Require().Equal(expRes.Pagination, resp.Pagination)
   299  				suite.Require().ElementsMatch(expRes.ContractAddresses, resp.ContractAddresses)
   300  			} else {
   301  				suite.Require().Error(err)
   302  			}
   303  		})
   304  	}
   305  }
   306  
   307  func (suite *KeeperTestSuite) TestWithdrawerFeeSplits() {
   308  	var (
   309  		req    *types.QueryWithdrawerFeeSplitsRequest
   310  		expRes *types.QueryWithdrawerFeeSplitsResponse
   311  	)
   312  
   313  	testCases := []struct {
   314  		name     string
   315  		path     []string
   316  		malleate func()
   317  		expPass  bool
   318  	}{
   319  		{
   320  			"no contract registered",
   321  			[]string{types.QueryWithdrawerFeeSplits},
   322  			func() {
   323  				req = &types.QueryWithdrawerFeeSplitsRequest{}
   324  				expRes = &types.QueryWithdrawerFeeSplitsResponse{Pagination: &query.PageResponse{}}
   325  			},
   326  			false,
   327  		},
   328  		{
   329  			"invalid withdraw address",
   330  			[]string{types.QueryWithdrawerFeeSplits},
   331  			func() {
   332  				req = &types.QueryWithdrawerFeeSplitsRequest{
   333  					WithdrawerAddress: "123",
   334  				}
   335  				expRes = &types.QueryWithdrawerFeeSplitsResponse{Pagination: &query.PageResponse{}}
   336  			},
   337  			false,
   338  		},
   339  		{
   340  			"1 fee registered w/pagination",
   341  			[]string{types.QueryWithdrawerFeeSplits},
   342  			func() {
   343  				req = &types.QueryWithdrawerFeeSplitsRequest{
   344  					Pagination:        &query.PageRequest{Limit: 10, CountTotal: true},
   345  					WithdrawerAddress: withdraw.String(),
   346  				}
   347  
   348  				feeSplit := types.NewFeeSplit(contract, deployer, withdraw)
   349  				suite.app.FeeSplitKeeper.SetFeeSplit(suite.ctx, feeSplit)
   350  				suite.app.FeeSplitKeeper.SetDeployerMap(suite.ctx, deployer, contract)
   351  				suite.app.FeeSplitKeeper.SetWithdrawerMap(suite.ctx, withdraw, contract)
   352  
   353  				expRes = &types.QueryWithdrawerFeeSplitsResponse{
   354  					Pagination: &query.PageResponse{Total: 1},
   355  					ContractAddresses: []string{
   356  						contract.Hex(),
   357  					},
   358  				}
   359  			},
   360  			true,
   361  		},
   362  		{
   363  			"2 fees registered for one withdraw address wo/pagination",
   364  			[]string{types.QueryWithdrawerFeeSplits},
   365  			func() {
   366  				req = &types.QueryWithdrawerFeeSplitsRequest{
   367  					WithdrawerAddress: withdraw.String(),
   368  				}
   369  				contract2 := ethsecp256k1.GenerateAddress()
   370  				deployer2 := sdk.AccAddress(ethsecp256k1.GenerateAddress().Bytes())
   371  
   372  				feeSplit := types.NewFeeSplit(contract, deployer, withdraw)
   373  				suite.app.FeeSplitKeeper.SetFeeSplit(suite.ctx, feeSplit)
   374  				suite.app.FeeSplitKeeper.SetDeployerMap(suite.ctx, deployer, contract)
   375  				suite.app.FeeSplitKeeper.SetWithdrawerMap(suite.ctx, withdraw, contract)
   376  
   377  				feeSplit2 := types.NewFeeSplit(contract2, deployer2, withdraw)
   378  				suite.app.FeeSplitKeeper.SetFeeSplit(suite.ctx, feeSplit2)
   379  				suite.app.FeeSplitKeeper.SetDeployerMap(suite.ctx, deployer2, contract2)
   380  				suite.app.FeeSplitKeeper.SetWithdrawerMap(suite.ctx, withdraw, contract2)
   381  
   382  				expRes = &types.QueryWithdrawerFeeSplitsResponse{
   383  					Pagination: &query.PageResponse{Total: 2},
   384  					ContractAddresses: []string{
   385  						contract.Hex(),
   386  						contract2.Hex(),
   387  					},
   388  				}
   389  			},
   390  			true,
   391  		},
   392  	}
   393  	for _, tc := range testCases {
   394  		suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
   395  			suite.SetupTest() // reset
   396  
   397  			tc.malleate()
   398  
   399  			data, err := suite.app.Codec().MarshalJSON(req)
   400  			suite.Require().NoError(err)
   401  			res, err := suite.querier(suite.ctx, tc.path, abci.RequestQuery{Data: data})
   402  			if tc.expPass {
   403  				suite.Require().NoError(err)
   404  
   405  				var resp types.QueryWithdrawerFeeSplitsResponse
   406  				err = suite.app.Codec().UnmarshalJSON(res, &resp)
   407  				suite.Require().NoError(err)
   408  				suite.Require().Equal(expRes.Pagination, resp.Pagination)
   409  				suite.Require().ElementsMatch(expRes.ContractAddresses, resp.ContractAddresses)
   410  			} else {
   411  				suite.Require().Error(err)
   412  			}
   413  		})
   414  	}
   415  }
   416  
   417  func (suite *KeeperTestSuite) TestQueryParams() {
   418  	expParams := types.DefaultParams()
   419  
   420  	res, err := suite.querier(suite.ctx, []string{types.QueryParameters}, abci.RequestQuery{Data: nil})
   421  	suite.Require().NoError(err)
   422  	var resp types.QueryParamsResponse
   423  	err = suite.app.Codec().UnmarshalJSON(res, &resp)
   424  	suite.Require().NoError(err)
   425  	suite.Require().Equal(expParams, resp.Params)
   426  }