Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add more unittest for MsgIssue of x/token #1128

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (ostracon) [\#1089](https://github.com/Finschia/finschia-sdk/pull/1089) Bump up ostracon from v1.1.1 to v1.1.1-449aa3148b12
* (ostracon) [\#1099](https://github.com/Finschia/finschia-sdk/pull/1099) Remove libsodium vrf library.
* (refactor) [\#1114](https://github.com/Finschia/finschia-sdk/pull/1114) Check statistics and balance on x/collection mint and burn operations
* (x/token) [\#1128](https://github.com/Finschia/finschia-sdk/pull/1128) add more unittest for MsgIssue of x/token

### Bug Fixes
* (ledger) [\#1040](https://github.com/Finschia/finschia-sdk/pull/1040) Fix a bug(unable to connect nano S plus ledger on ubuntu)
Expand Down
101 changes: 101 additions & 0 deletions x/token/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"fmt"
abci "github.com/tendermint/tendermint/abci/types"

sdk "github.com/Finschia/finschia-sdk/types"
Expand Down Expand Up @@ -264,6 +265,106 @@ func (s *KeeperTestSuite) TestMsgIssue() {
}
}

func (s *KeeperTestSuite) TestMsgIssue2() {
tkxkd0159 marked this conversation as resolved.
Show resolved Hide resolved
ownerAddr := s.vendor.String()
toAddr := s.vendor.String()

testCases := map[string]struct {
mintable bool
amount sdk.Int
err error
events sdk.Events
}{
"mintable true": {
mintable: true,
amount: sdk.NewInt(10),
events: sdk.Events{
sdk.Event{Type: "lbm.token.v1.EventIssued", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("creator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("decimals"), Value: []uint8("0"), Index: false}, {Key: []uint8("meta"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("mintable"), Value: []uint8("true"), Index: false}, {Key: []uint8("name"), Value: []uint8("\"test\""), Index: false}, {Key: []uint8("symbol"), Value: []uint8("\"TT\""), Index: false}, {Key: []uint8("uri"), Value: []uint8("\"\""), Index: false}}},
sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}, {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MODIFY\""), Index: false}}},
sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}, {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MINT\""), Index: false}}},
sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}, {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_BURN\""), Index: false}}},
sdk.Event{Type: "lbm.token.v1.EventMinted", Attributes: []abci.EventAttribute{{Key: []uint8("amount"), Value: []uint8("\"10\""), Index: false}, {Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("to"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}}},
},
},
"mintable false": {
mintable: false,
amount: sdk.NewInt(10),
events: sdk.Events{
sdk.Event{Type: "lbm.token.v1.EventIssued", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("creator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("decimals"), Value: []uint8("0"), Index: false}, {Key: []uint8("meta"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("mintable"), Value: []uint8("false"), Index: false}, {Key: []uint8("name"), Value: []uint8("\"test\""), Index: false}, {Key: []uint8("symbol"), Value: []uint8("\"TT\""), Index: false}, {Key: []uint8("uri"), Value: []uint8("\"\""), Index: false}}},
sdk.Event{Type: "lbm.token.v1.EventGranted", Attributes: []abci.EventAttribute{{Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("grantee"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("granter"), Value: []uint8("\"\""), Index: false}, {Key: []uint8("permission"), Value: []uint8("\"PERMISSION_MODIFY\""), Index: false}}},
sdk.Event{Type: "lbm.token.v1.EventMinted", Attributes: []abci.EventAttribute{{Key: []uint8("amount"), Value: []uint8("\"10\""), Index: false}, {Key: []uint8("contract_id"), Value: []uint8("\"fee15a74\""), Index: false}, {Key: []uint8("operator"), Value: []uint8(fmt.Sprintf("\"%s\"", ownerAddr)), Index: false}, {Key: []uint8("to"), Value: []uint8(fmt.Sprintf("\"%s\"", toAddr)), Index: false}}},
},
},
}

// define a function to check MsgIssue result
checkerIssueResult := func(ctx sdk.Context, contractId string, expectedMintable bool, expectedAmount sdk.Int) {
// check contract
contract, err := s.queryServer.Contract(sdk.WrapSDKContext(ctx), &token.QueryContractRequest{ContractId: contractId})
s.Require().NoError(err)
s.Require().Equal(expectedMintable, contract.Contract.Mintable)

// check supply
supply, err := s.queryServer.Supply(sdk.WrapSDKContext(ctx), &token.QuerySupplyRequest{ContractId: contractId})
s.Require().NoError(err)
s.Require().Equal(expectedAmount, supply.Amount)

// check mint
mint, err := s.queryServer.Minted(sdk.WrapSDKContext(ctx), &token.QueryMintedRequest{ContractId: contractId})
s.Require().NoError(err)
s.Require().Equal(expectedAmount, mint.Amount)

// check burnt
burn, err := s.queryServer.Burnt(sdk.WrapSDKContext(ctx), &token.QueryBurntRequest{ContractId: contractId})
s.Require().NoError(err)
s.Require().Equal(sdk.ZeroInt(), burn.Amount)

// check owner balance
balance, err := s.queryServer.Balance(sdk.WrapSDKContext(ctx), &token.QueryBalanceRequest{
ContractId: contractId,
Address: s.vendor.String(),
})
s.Require().NoError(err)
s.Require().Equal(expectedAmount, balance.Amount)
}

for name, tc := range testCases {
s.Run(name, func() {
ctx, _ := s.ctx.CacheContext()

req := &token.MsgIssue{
Owner: s.vendor.String(),
To: s.vendor.String(),
Mintable: tc.mintable,
Name: "test",
Symbol: "TT",
Amount: tc.amount,
}
res, err := s.msgServer.Issue(sdk.WrapSDKContext(ctx), req)
s.Require().ErrorIs(err, tc.err)
if tc.err != nil {
return
}

s.Require().NotNil(res)
s.Require().Equal(tc.events, ctx.EventManager().Events())

// check result status
checkerIssueResult(ctx, res.ContractId, tc.mintable, tc.amount)

// Second request for the same request
res2, err := s.msgServer.Issue(sdk.WrapSDKContext(ctx), req)
s.Require().ErrorIs(err, tc.err)
if tc.err != nil {
return
}
// check result status
checkerIssueResult(ctx, res2.ContractId, tc.mintable, tc.amount)
s.Require().NotEqual(res.ContractId, res2.ContractId)
})
}
}

func (s *KeeperTestSuite) TestMsgGrantPermission() {
testCases := map[string]struct {
contractID string
Expand Down
66 changes: 66 additions & 0 deletions x/token/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,50 @@ func TestMsgIssue(t *testing.T) {
amount: sdk.OneInt(),
err: token.ErrInvalidTokenSymbol,
},
"invalid symbol - start number": {
owner: addrs[0],
to: addrs[1],
name: "test",
symbol: "1TT",
imageUri: "some URI",
meta: "some meta",
decimals: 8,
amount: sdk.OneInt(),
err: token.ErrInvalidTokenSymbol,
},
"invalid symbol - start lowercase letter": {
owner: addrs[0],
to: addrs[1],
name: "test",
symbol: "tTT",
imageUri: "some URI",
meta: "some meta",
decimals: 8,
amount: sdk.OneInt(),
err: token.ErrInvalidTokenSymbol,
},
"invalid symbol - include lowercase letter": {
owner: addrs[0],
to: addrs[1],
name: "test",
symbol: "TtT",
imageUri: "some URI",
meta: "some meta",
decimals: 8,
amount: sdk.OneInt(),
err: token.ErrInvalidTokenSymbol,
},
"invalid symbol - long length": {
owner: addrs[0],
to: addrs[1],
name: "test",
symbol: "TTTTT6",
imageUri: "some URI",
meta: "some meta",
decimals: 8,
amount: sdk.OneInt(),
err: token.ErrInvalidTokenSymbol,
},
"invalid image uri": {
owner: addrs[0],
to: addrs[1],
Expand Down Expand Up @@ -390,6 +434,28 @@ func TestMsgIssue(t *testing.T) {
amount: sdk.OneInt(),
err: token.ErrInvalidTokenDecimals,
},
"invalid decimals - negative": {
owner: addrs[0],
to: addrs[1],
name: "test",
symbol: "TT",
imageUri: "some URI",
meta: "some meta",
decimals: -1,
amount: sdk.OneInt(),
err: token.ErrInvalidTokenDecimals,
},
"invalid amount": {
owner: addrs[0],
to: addrs[1],
name: "test",
symbol: "TT",
imageUri: "some URI",
meta: "some meta",
decimals: 8,
amount: sdk.ZeroInt(),
err: token.ErrInvalidAmount,
},
}

for name, tc := range testCases {
Expand Down
Loading