cosmos-sdk/x/feegrant/keeper/grpc_query_test.go

229 lines
5.4 KiB
Go
Raw Normal View History

package keeper_test
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/feegrant"
)
func (suite *KeeperTestSuite) TestFeeAllowance() {
testCases := []struct {
name string
req *feegrant.QueryAllowanceRequest
expectErr bool
preRun func()
postRun func(_ *feegrant.QueryAllowanceResponse)
}{
{
"nil request",
nil,
true,
func() {},
func(*feegrant.QueryAllowanceResponse) {},
},
{
"fail: invalid granter",
&feegrant.QueryAllowanceRequest{
Granter: "invalid_granter",
Grantee: suite.addrs[0].String(),
},
true,
func() {},
func(*feegrant.QueryAllowanceResponse) {},
},
{
"fail: invalid grantee",
&feegrant.QueryAllowanceRequest{
Granter: suite.addrs[0].String(),
Grantee: "invalid_grantee",
},
true,
func() {},
func(*feegrant.QueryAllowanceResponse) {},
},
{
"fail: no grants",
&feegrant.QueryAllowanceRequest{
Granter: suite.addrs[0].String(),
Grantee: suite.addrs[1].String(),
},
true,
func() {},
func(*feegrant.QueryAllowanceResponse) {},
},
{
"valid query: expect single grant",
&feegrant.QueryAllowanceRequest{
Granter: suite.addrs[0].String(),
Grantee: suite.addrs[1].String(),
},
false,
func() {
fix: grants by granter pagination `total` (#11813) ## Description Closes: #11812 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2022-04-29 03:53:18 -07:00
suite.grantFeeAllowance(suite.addrs[0], suite.addrs[1])
},
func(response *feegrant.QueryAllowanceResponse) {
suite.Require().Equal(response.Allowance.Granter, suite.addrs[0].String())
suite.Require().Equal(response.Allowance.Grantee, suite.addrs[1].String())
},
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
tc.preRun()
resp, err := suite.keeper.Allowance(suite.ctx, tc.req)
if tc.expectErr {
suite.Require().Error(err)
} else {
suite.Require().NoError(err)
tc.postRun(resp)
}
})
}
}
func (suite *KeeperTestSuite) TestFeeAllowances() {
testCases := []struct {
name string
req *feegrant.QueryAllowancesRequest
expectErr bool
preRun func()
postRun func(_ *feegrant.QueryAllowancesResponse)
}{
{
"nil request",
nil,
true,
func() {},
func(*feegrant.QueryAllowancesResponse) {},
},
{
"fail: invalid grantee",
&feegrant.QueryAllowancesRequest{
Grantee: "invalid_grantee",
},
true,
func() {},
func(*feegrant.QueryAllowancesResponse) {},
},
{
"no grants",
&feegrant.QueryAllowancesRequest{
Grantee: suite.addrs[1].String(),
},
false,
func() {},
func(resp *feegrant.QueryAllowancesResponse) {
suite.Require().Equal(len(resp.Allowances), 0)
},
},
{
"valid query: expect single grant",
&feegrant.QueryAllowancesRequest{
Grantee: suite.addrs[1].String(),
},
false,
func() {
fix: grants by granter pagination `total` (#11813) ## Description Closes: #11812 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2022-04-29 03:53:18 -07:00
suite.grantFeeAllowance(suite.addrs[0], suite.addrs[1])
},
func(resp *feegrant.QueryAllowancesResponse) {
suite.Require().Equal(len(resp.Allowances), 1)
suite.Require().Equal(resp.Allowances[0].Granter, suite.addrs[0].String())
suite.Require().Equal(resp.Allowances[0].Grantee, suite.addrs[1].String())
},
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
tc.preRun()
resp, err := suite.keeper.Allowances(suite.ctx, tc.req)
if tc.expectErr {
suite.Require().Error(err)
} else {
suite.Require().NoError(err)
tc.postRun(resp)
}
})
}
}
func (suite *KeeperTestSuite) TestFeeAllowancesByGranter() {
testCases := []struct {
name string
req *feegrant.QueryAllowancesByGranterRequest
expectErr bool
preRun func()
postRun func(_ *feegrant.QueryAllowancesByGranterResponse)
}{
{
"nil request",
nil,
true,
func() {},
func(*feegrant.QueryAllowancesByGranterResponse) {},
},
{
"fail: invalid grantee",
&feegrant.QueryAllowancesByGranterRequest{
Granter: "invalid_grantee",
},
true,
func() {},
func(*feegrant.QueryAllowancesByGranterResponse) {},
},
{
"no grants",
&feegrant.QueryAllowancesByGranterRequest{
Granter: suite.addrs[0].String(),
},
false,
func() {},
func(resp *feegrant.QueryAllowancesByGranterResponse) {
suite.Require().Equal(len(resp.Allowances), 0)
},
},
{
"valid query: expect single grant",
&feegrant.QueryAllowancesByGranterRequest{
Granter: suite.addrs[0].String(),
},
false,
func() {
fix: grants by granter pagination `total` (#11813) ## Description Closes: #11812 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2022-04-29 03:53:18 -07:00
suite.grantFeeAllowance(suite.addrs[0], suite.addrs[1])
// adding this allowance to check whether the pagination working fine.
suite.grantFeeAllowance(suite.addrs[1], suite.addrs[2])
},
func(resp *feegrant.QueryAllowancesByGranterResponse) {
suite.Require().Equal(len(resp.Allowances), 1)
suite.Require().Equal(resp.Allowances[0].Granter, suite.addrs[0].String())
suite.Require().Equal(resp.Allowances[0].Grantee, suite.addrs[1].String())
fix: grants by granter pagination `total` (#11813) ## Description Closes: #11812 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2022-04-29 03:53:18 -07:00
suite.Require().Equal(resp.Pagination.Total, uint64(1))
},
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
tc.preRun()
resp, err := suite.keeper.AllowancesByGranter(suite.ctx, tc.req)
if tc.expectErr {
suite.Require().Error(err)
} else {
suite.Require().NoError(err)
tc.postRun(resp)
}
})
}
}
fix: grants by granter pagination `total` (#11813) ## Description Closes: #11812 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2022-04-29 03:53:18 -07:00
func (suite *KeeperTestSuite) grantFeeAllowance(granter, grantee sdk.AccAddress) {
exp := suite.sdkCtx.BlockTime().AddDate(1, 0, 0)
fix: grants by granter pagination `total` (#11813) ## Description Closes: #11812 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2022-04-29 03:53:18 -07:00
err := suite.app.FeeGrantKeeper.GrantAllowance(suite.sdkCtx, granter, grantee, &feegrant.BasicAllowance{
SpendLimit: sdk.NewCoins(sdk.NewInt64Coin("atom", 555)),
Expiration: &exp,
})
suite.Require().NoError(err)
}