## Description This PR works towards #12133 and does a fumpt for consistency once sdk.Int is merged. The suggested merge order is to begin with sdk.Int, merge in the various linter PR's one by one, and then finally merge the changes to CI from #12134 --- ### 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... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [x] 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 - [x] reviewed "Files changed" and left comments if necessary - [x] 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)
477 lines
9.8 KiB
Go
477 lines
9.8 KiB
Go
package testutil
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/cosmos/cosmos-sdk/testutil/rest"
|
|
"github.com/cosmos/cosmos-sdk/x/nft"
|
|
)
|
|
|
|
func (s *IntegrationTestSuite) TestQueryBalanceGRPC() {
|
|
val := s.network.Validators[0]
|
|
testCases := []struct {
|
|
name string
|
|
args struct {
|
|
ClassID string
|
|
Owner string
|
|
}
|
|
expectErr bool
|
|
errMsg string
|
|
expectValue uint64
|
|
}{
|
|
{
|
|
name: "fail not exist class id",
|
|
args: struct {
|
|
ClassID string
|
|
Owner string
|
|
}{
|
|
ClassID: "invalid_class_id",
|
|
Owner: s.owner.String(),
|
|
},
|
|
expectErr: true,
|
|
errMsg: "invalid class id",
|
|
expectValue: 0,
|
|
},
|
|
{
|
|
name: "fail not exist owner",
|
|
args: struct {
|
|
ClassID string
|
|
Owner string
|
|
}{
|
|
ClassID: ExpNFT.ClassId,
|
|
Owner: s.owner.String(),
|
|
},
|
|
expectErr: false,
|
|
expectValue: 0,
|
|
},
|
|
{
|
|
name: "success",
|
|
args: struct {
|
|
ClassID string
|
|
Owner string
|
|
}{
|
|
ClassID: ExpNFT.ClassId,
|
|
Owner: val.Address.String(),
|
|
},
|
|
expectErr: false,
|
|
expectValue: 1,
|
|
},
|
|
}
|
|
balanceURL := val.APIAddress + "/cosmos/nft/v1beta1/balance/%s/%s"
|
|
for _, tc := range testCases {
|
|
uri := fmt.Sprintf(balanceURL, tc.args.Owner, tc.args.ClassID)
|
|
s.Run(tc.name, func() {
|
|
resp, _ := rest.GetRequest(uri)
|
|
if tc.expectErr {
|
|
s.Require().Contains(string(resp), tc.errMsg)
|
|
} else {
|
|
var g nft.QueryBalanceResponse
|
|
err := val.ClientCtx.Codec.UnmarshalJSON(resp, &g)
|
|
s.Require().NoError(err)
|
|
s.Require().Equal(tc.expectValue, g.Amount)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func (s *IntegrationTestSuite) TestQueryOwnerGRPC() {
|
|
val := s.network.Validators[0]
|
|
|
|
testCases := []struct {
|
|
name string
|
|
args struct {
|
|
ClassID string
|
|
ID string
|
|
}
|
|
expectErr bool
|
|
errMsg string
|
|
expectResult string
|
|
}{
|
|
{
|
|
name: "class id is invalid",
|
|
args: struct {
|
|
ClassID string
|
|
ID string
|
|
}{
|
|
ClassID: "invalid_class_id",
|
|
ID: ExpNFT.Id,
|
|
},
|
|
expectErr: true,
|
|
errMsg: "invalid class id",
|
|
expectResult: "",
|
|
},
|
|
{
|
|
name: "class id does not exist",
|
|
args: struct {
|
|
ClassID string
|
|
ID string
|
|
}{
|
|
ClassID: "class-id",
|
|
ID: ExpNFT.Id,
|
|
},
|
|
expectErr: false,
|
|
expectResult: "",
|
|
},
|
|
{
|
|
name: "nft id is invalid",
|
|
args: struct {
|
|
ClassID string
|
|
ID string
|
|
}{
|
|
ClassID: ExpNFT.ClassId,
|
|
ID: "invalid_nft_id",
|
|
},
|
|
expectErr: true,
|
|
expectResult: "",
|
|
},
|
|
{
|
|
name: "nft id does not exist",
|
|
args: struct {
|
|
ClassID string
|
|
ID string
|
|
}{
|
|
ClassID: ExpNFT.ClassId,
|
|
ID: "nft-id",
|
|
},
|
|
expectErr: false,
|
|
expectResult: "",
|
|
},
|
|
{
|
|
name: "nft exist",
|
|
args: struct {
|
|
ClassID string
|
|
ID string
|
|
}{
|
|
ClassID: ExpNFT.ClassId,
|
|
ID: ExpNFT.Id,
|
|
},
|
|
expectErr: false,
|
|
expectResult: val.Address.String(),
|
|
},
|
|
}
|
|
ownerURL := val.APIAddress + "/cosmos/nft/v1beta1/owner/%s/%s"
|
|
for _, tc := range testCases {
|
|
uri := fmt.Sprintf(ownerURL, tc.args.ClassID, tc.args.ID)
|
|
s.Run(tc.name, func() {
|
|
resp, err := rest.GetRequest(uri)
|
|
if tc.expectErr {
|
|
s.Require().Contains(string(resp), tc.errMsg)
|
|
} else {
|
|
s.Require().NoError(err)
|
|
var result nft.QueryOwnerResponse
|
|
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &result)
|
|
s.Require().NoError(err)
|
|
s.Require().EqualValues(tc.expectResult, result.Owner)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func (s *IntegrationTestSuite) TestQuerySupplyGRPC() {
|
|
val := s.network.Validators[0]
|
|
|
|
testCases := []struct {
|
|
name string
|
|
args struct {
|
|
ClassID string
|
|
}
|
|
expectErr bool
|
|
errMsg string
|
|
expectResult uint64
|
|
}{
|
|
{
|
|
name: "class id is invalid",
|
|
args: struct {
|
|
ClassID string
|
|
}{
|
|
ClassID: "invalid_class_id",
|
|
},
|
|
expectErr: true,
|
|
errMsg: "invalid class id",
|
|
expectResult: 0,
|
|
},
|
|
{
|
|
name: "class id does not exist",
|
|
args: struct {
|
|
ClassID string
|
|
}{
|
|
ClassID: "class-id",
|
|
},
|
|
expectErr: false,
|
|
expectResult: 0,
|
|
},
|
|
{
|
|
name: "class id exist",
|
|
args: struct {
|
|
ClassID string
|
|
}{
|
|
ClassID: ExpNFT.ClassId,
|
|
},
|
|
expectErr: false,
|
|
expectResult: 1,
|
|
},
|
|
}
|
|
supplyURL := val.APIAddress + "/cosmos/nft/v1beta1/supply/%s"
|
|
for _, tc := range testCases {
|
|
uri := fmt.Sprintf(supplyURL, tc.args.ClassID)
|
|
s.Run(tc.name, func() {
|
|
resp, err := rest.GetRequest(uri)
|
|
if tc.expectErr {
|
|
s.Require().Contains(string(resp), tc.errMsg)
|
|
} else {
|
|
s.Require().NoError(err)
|
|
var result nft.QuerySupplyResponse
|
|
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &result)
|
|
s.Require().NoError(err)
|
|
s.Require().EqualValues(tc.expectResult, result.Amount)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func (s *IntegrationTestSuite) TestQueryNFTsGRPC() {
|
|
val := s.network.Validators[0]
|
|
testCases := []struct {
|
|
name string
|
|
args struct {
|
|
ClassID string
|
|
Owner string
|
|
}
|
|
expectErr bool
|
|
errorMsg string
|
|
expectResult []*nft.NFT
|
|
}{
|
|
{
|
|
name: "classID and owner are both empty",
|
|
args: struct {
|
|
ClassID string
|
|
Owner string
|
|
}{},
|
|
errorMsg: "must provide at least one of classID or owner",
|
|
expectErr: true,
|
|
expectResult: []*nft.NFT{},
|
|
},
|
|
{
|
|
name: "classID is invalid",
|
|
args: struct {
|
|
ClassID string
|
|
Owner string
|
|
}{
|
|
ClassID: "invalid_class_id",
|
|
},
|
|
expectErr: true,
|
|
expectResult: []*nft.NFT{},
|
|
},
|
|
{
|
|
name: "classID does not exist",
|
|
args: struct {
|
|
ClassID string
|
|
Owner string
|
|
}{
|
|
ClassID: "class-id",
|
|
},
|
|
expectErr: false,
|
|
expectResult: []*nft.NFT{},
|
|
},
|
|
{
|
|
name: "success query by classID",
|
|
args: struct {
|
|
ClassID string
|
|
Owner string
|
|
}{
|
|
ClassID: ExpNFT.ClassId,
|
|
},
|
|
expectErr: false,
|
|
expectResult: []*nft.NFT{&ExpNFT},
|
|
},
|
|
{
|
|
name: "success query by owner",
|
|
args: struct {
|
|
ClassID string
|
|
Owner string
|
|
}{
|
|
Owner: val.Address.String(),
|
|
},
|
|
expectErr: false,
|
|
expectResult: []*nft.NFT{&ExpNFT},
|
|
},
|
|
{
|
|
name: "success query by owner and classID",
|
|
args: struct {
|
|
ClassID string
|
|
Owner string
|
|
}{
|
|
ClassID: ExpNFT.ClassId,
|
|
Owner: val.Address.String(),
|
|
},
|
|
expectErr: false,
|
|
expectResult: []*nft.NFT{&ExpNFT},
|
|
},
|
|
}
|
|
nftsOfClassURL := val.APIAddress + "/cosmos/nft/v1beta1/nfts?class_id=%s&owner=%s"
|
|
for _, tc := range testCases {
|
|
uri := fmt.Sprintf(nftsOfClassURL, tc.args.ClassID, tc.args.Owner)
|
|
s.Run(tc.name, func() {
|
|
resp, err := rest.GetRequest(uri)
|
|
if tc.expectErr {
|
|
s.Require().Contains(string(resp), tc.errorMsg)
|
|
} else {
|
|
s.Require().NoError(err)
|
|
var result nft.QueryNFTsResponse
|
|
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &result)
|
|
s.Require().NoError(err)
|
|
s.Require().EqualValues(tc.expectResult, result.Nfts)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func (s *IntegrationTestSuite) TestQueryNFTGRPC() {
|
|
val := s.network.Validators[0]
|
|
testCases := []struct {
|
|
name string
|
|
args struct {
|
|
ClassID string
|
|
ID string
|
|
}
|
|
expectErr bool
|
|
errorMsg string
|
|
}{
|
|
{
|
|
name: "class id is invalid",
|
|
args: struct {
|
|
ClassID string
|
|
ID string
|
|
}{
|
|
ClassID: "invalid_class_id",
|
|
ID: ExpNFT.Id,
|
|
},
|
|
expectErr: true,
|
|
errorMsg: "invalid class id",
|
|
},
|
|
{
|
|
name: "class id does not exist",
|
|
args: struct {
|
|
ClassID string
|
|
ID string
|
|
}{
|
|
ClassID: "class",
|
|
ID: ExpNFT.Id,
|
|
},
|
|
expectErr: true,
|
|
errorMsg: "not found nft",
|
|
},
|
|
{
|
|
name: "nft id is invalid",
|
|
args: struct {
|
|
ClassID string
|
|
ID string
|
|
}{
|
|
ClassID: ExpNFT.ClassId,
|
|
ID: "invalid_nft_id",
|
|
},
|
|
expectErr: true,
|
|
errorMsg: "invalid nft id",
|
|
},
|
|
{
|
|
name: "nft id does not exist",
|
|
args: struct {
|
|
ClassID string
|
|
ID string
|
|
}{
|
|
ClassID: ExpNFT.ClassId,
|
|
ID: "nft-id",
|
|
},
|
|
expectErr: true,
|
|
errorMsg: "not found nft",
|
|
},
|
|
{
|
|
name: "exist nft",
|
|
args: struct {
|
|
ClassID string
|
|
ID string
|
|
}{
|
|
ClassID: ExpNFT.ClassId,
|
|
ID: ExpNFT.Id,
|
|
},
|
|
expectErr: false,
|
|
},
|
|
}
|
|
nftURL := val.APIAddress + "/cosmos/nft/v1beta1/nfts/%s/%s"
|
|
for _, tc := range testCases {
|
|
uri := fmt.Sprintf(nftURL, tc.args.ClassID, tc.args.ID)
|
|
s.Run(tc.name, func() {
|
|
resp, err := rest.GetRequest(uri)
|
|
if tc.expectErr {
|
|
s.Require().Contains(string(resp), tc.errorMsg)
|
|
} else {
|
|
s.Require().NoError(err)
|
|
var result nft.QueryNFTResponse
|
|
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &result)
|
|
s.Require().NoError(err)
|
|
s.Require().EqualValues(ExpNFT, *result.Nft)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func (s *IntegrationTestSuite) TestQueryClassGRPC() {
|
|
val := s.network.Validators[0]
|
|
testCases := []struct {
|
|
name string
|
|
args struct {
|
|
ClassID string
|
|
}
|
|
expectErr bool
|
|
errorMsg string
|
|
}{
|
|
{
|
|
name: "class id does not exist",
|
|
args: struct {
|
|
ClassID string
|
|
}{
|
|
ClassID: "class-id",
|
|
},
|
|
expectErr: true,
|
|
errorMsg: "not found class",
|
|
},
|
|
{
|
|
name: "class id exist",
|
|
args: struct {
|
|
ClassID string
|
|
}{
|
|
ClassID: ExpNFT.ClassId,
|
|
},
|
|
expectErr: false,
|
|
},
|
|
}
|
|
classURL := val.APIAddress + "/cosmos/nft/v1beta1/classes/%s"
|
|
for _, tc := range testCases {
|
|
uri := fmt.Sprintf(classURL, tc.args.ClassID)
|
|
s.Run(tc.name, func() {
|
|
resp, err := rest.GetRequest(uri)
|
|
if tc.expectErr {
|
|
s.Require().Contains(string(resp), tc.errorMsg)
|
|
} else {
|
|
s.Require().NoError(err)
|
|
var result nft.QueryClassResponse
|
|
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &result)
|
|
s.Require().NoError(err)
|
|
s.Require().EqualValues(ExpClass, *result.Class)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func (s *IntegrationTestSuite) TestQueryClassesGRPC() {
|
|
val := s.network.Validators[0]
|
|
classURL := val.APIAddress + "/cosmos/nft/v1beta1/classes"
|
|
resp, err := rest.GetRequest(classURL)
|
|
s.Require().NoError(err)
|
|
var result nft.QueryClassesResponse
|
|
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &result)
|
|
s.Require().NoError(err)
|
|
s.Require().Len(result.Classes, 1)
|
|
s.Require().EqualValues(ExpClass, *result.Classes[0])
|
|
}
|