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

test: add cli_test for fswap module #1391

Merged
merged 9 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -64,6 +64,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/fswap) [\#1372](https://github.com/Finschia/finschia-sdk/pull/1372) support message based proposals
* (x/fswap) [\#1387](https://github.com/Finschia/finschia-sdk/pull/1387) add new Swap query to get a single swap
* (x/fswap) [\#1382](https://github.com/Finschia/finschia-sdk/pull/1382) add validation & unit tests in fswap module
* (x/fswap) [\#1391](https://github.com/Finschia/finschia-sdk/pull/1391) add cli_test for fswap module

### Bug Fixes
* chore(deps) [\#1141](https://github.com/Finschia/finschia-sdk/pull/1141) Bump github.com/cosmos/ledger-cosmos-go from 0.12.2 to 0.13.2 to fix ledger signing issue
Expand Down
9 changes: 5 additions & 4 deletions x/fswap/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const (
FlagSwapRate = "swap-rate"
)

type ToDenomMeta struct {
Metadata bank.Metadata `json:"metadata"`
}

170210 marked this conversation as resolved.
Show resolved Hide resolved
// GetTxCmd returns the transaction commands for this module
func GetTxCmd() *cobra.Command {
cmd := &cobra.Command{
Expand Down Expand Up @@ -232,10 +236,7 @@ func validateGenerateOnly(cmd *cobra.Command) error {
}

func parseToDenomMetadata(jsonDenomMetadata string) (bank.Metadata, error) {
type toDenomMeta struct {
Metadata bank.Metadata `json:"metadata"`
}
denomMeta := toDenomMeta{}
denomMeta := ToDenomMeta{}
if err := json.Unmarshal([]byte(jsonDenomMetadata), &denomMeta); err != nil {
return bank.Metadata{}, err
}
Expand Down
18 changes: 18 additions & 0 deletions x/fswap/client/testutil/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//go:build norace
// +build norace

package testutil

import (
"testing"

"github.com/stretchr/testify/suite"

"github.com/Finschia/finschia-sdk/testutil/network"
)

func TestIntegrationTestSuite(t *testing.T) {
cfg := network.DefaultConfig()
cfg.NumValidators = 1
suite.Run(t, NewIntegrationTestSuite(cfg))
}
226 changes: 226 additions & 0 deletions x/fswap/client/testutil/grpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
package testutil

import (
"fmt"

"github.com/gogo/protobuf/proto"

"github.com/Finschia/finschia-sdk/testutil"
"github.com/Finschia/finschia-sdk/testutil/rest"
sdk "github.com/Finschia/finschia-sdk/types"
grpctypes "github.com/Finschia/finschia-sdk/types/grpc"
"github.com/Finschia/finschia-sdk/types/query"
fswaptypes "github.com/Finschia/finschia-sdk/x/fswap/types"
)

func (s *IntegrationTestSuite) TestGRPCQuerySwap() {
val := s.network.Validators[0]
baseURL := val.APIAddress

testCases := []struct {
name string
url string
expectedErr bool
expected proto.Message
}{
{
"test query swap with valid query string",
fmt.Sprintf("%s/lbm/fswap/v1/swap?fromDenom=stake&toDenom=dummy", baseURL),
false,
&fswaptypes.QuerySwapResponse{
Swap: s.dummySwap,
},
},
{
"test query swap with not existed swap pairs",
fmt.Sprintf("%s/lbm/fswap/v1/swap?fromDenom=fake&toDenom=dummy", baseURL),
true,
&fswaptypes.QuerySwapResponse{},
},
{
"test query swap with nil toDenom",
fmt.Sprintf("%s/lbm/fswap/v1/swap?fromDenom=fake", baseURL),
true,
&fswaptypes.QuerySwapResponse{},
},
{
"test query swap with nil fromDenom",
fmt.Sprintf("%s/lbm/fswap/v1/swap?toDenom=fake", baseURL),
true,
&fswaptypes.QuerySwapResponse{},
},
}

for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
resp, err := rest.GetRequest(tc.url)
s.Require().NoError(err)
var valRes fswaptypes.QuerySwapResponse
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &valRes)

if tc.expectedErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)
s.Require().Equal(tc.expected.String(), valRes.String())
}
})
}
}

func (s *IntegrationTestSuite) TestGRPCQuerySwaps() {
val := s.network.Validators[0]
baseURL := val.APIAddress

testCases := []struct {
name string
url string
expectedErr bool
expected proto.Message
}{
{
"test query swaps",
fmt.Sprintf("%s/lbm/fswap/v1/swaps", baseURL),
false,
&fswaptypes.QuerySwapsResponse{
Swaps: []fswaptypes.Swap{s.dummySwap},
Pagination: &query.PageResponse{Total: 1},
},
},
}

for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
resp, err := rest.GetRequest(tc.url)
s.Require().NoError(err)
var valRes fswaptypes.QuerySwapsResponse
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &valRes)

if tc.expectedErr {
s.Require().Error(err)

Check warning on line 102 in x/fswap/client/testutil/grpc.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/client/testutil/grpc.go#L102

Added line #L102 was not covered by tests
} else {
s.Require().NoError(err)
s.Require().Equal(tc.expected.String(), valRes.String())
}
})
}
}

func (s *IntegrationTestSuite) TestGRPCQuerySwapped() {
val := s.network.Validators[0]
baseURL := val.APIAddress

testCases := []struct {
name string
url string
expectedErr bool
expected proto.Message
}{
{
"test query swapped with valid query string",
fmt.Sprintf("%s/lbm/fswap/v1/swapped?fromDenom=stake&toDenom=dummy", baseURL),
false,
&fswaptypes.QuerySwappedResponse{
FromCoinAmount: sdk.NewCoin("stake", sdk.ZeroInt()),
ToCoinAmount: sdk.NewCoin("dummy", sdk.ZeroInt()),
},
},
{
"test query swapped with not existed swap pairs",
fmt.Sprintf("%s/lbm/fswap/v1/swapped?fromDenom=fake&toDenom=dummy", baseURL),
true,
&fswaptypes.QuerySwappedResponse{},
},
{
"test query swapped with nil toDenom",
fmt.Sprintf("%s/lbm/fswap/v1/swapped?fromDenom=fake", baseURL),
true,
&fswaptypes.QuerySwappedResponse{},
},
{
"test query swapped with nil fromDenom",
fmt.Sprintf("%s/lbm/fswap/v1/swapped?toDenom=fake", baseURL),
true,
&fswaptypes.QuerySwappedResponse{},
},
}

for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
resp, err := testutil.GetRequestWithHeaders(tc.url, map[string]string{
grpctypes.GRPCBlockHeightHeader: "1",
})
s.Require().NoError(err)
var valRes fswaptypes.QuerySwappedResponse
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &valRes)

if tc.expectedErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)
s.Require().Equal(tc.expected.String(), valRes.String())
}
})
}
}

func (s *IntegrationTestSuite) TestGRPCQueryTotalSwappableAmount() {
val := s.network.Validators[0]
baseURL := val.APIAddress

testCases := []struct {
name string
url string
expectedErr bool
expected proto.Message
}{
{
"test query total_swappable_to_coin_amount with valid query string",
fmt.Sprintf("%s/lbm/fswap/v1/total_swappable_to_coin_amount?fromDenom=stake&toDenom=dummy", baseURL),
false,
&fswaptypes.QueryTotalSwappableToCoinAmountResponse{
SwappableAmount: sdk.NewCoin("dummy", s.dummySwap.AmountCapForToDenom),
},
},
{
"test query total_swappable_to_coin_amount with not existed swap pairs",
fmt.Sprintf("%s/lbm/fswap/v1/total_swappable_to_coin_amount?fromDenom=fake&toDenom=dummy", baseURL),
true,
&fswaptypes.QueryTotalSwappableToCoinAmountResponse{},
},
{
"test query total_swappable_to_coin_amount with nil toDenom",
fmt.Sprintf("%s/lbm/fswap/v1/total_swappable_to_coin_amount?fromDenom=fake", baseURL),
true,
&fswaptypes.QueryTotalSwappableToCoinAmountResponse{},
},
{
"test query total_swappable_to_coin_amount with nil fromDenom",
fmt.Sprintf("%s/lbm/fswap/v1/total_swappable_to_coin_amount?toDenom=fake", baseURL),
true,
&fswaptypes.QueryTotalSwappableToCoinAmountResponse{},
},
}

for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
resp, err := testutil.GetRequestWithHeaders(tc.url, map[string]string{
grpctypes.GRPCBlockHeightHeader: "1",
})
s.Require().NoError(err)
var valRes fswaptypes.QueryTotalSwappableToCoinAmountResponse
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &valRes)

if tc.expectedErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)
s.Require().Equal(tc.expected.String(), valRes.String())
}
})
}
}
Loading
Loading