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 additional testcases for fswap (backport #1415) #1420

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Features

### Improvements

* (x/fswap) [\#1415](https://github.com/Finschia/finschia-sdk/pull/1415) add more testcases for fswap module

### Bug Fixes

### Removed
Expand Down
77 changes: 67 additions & 10 deletions x/fswap/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,81 @@ package keeper_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/Finschia/finschia-sdk/simapp"
"github.com/Finschia/finschia-sdk/testutil/testdata"
sdk "github.com/Finschia/finschia-sdk/types"
"github.com/Finschia/finschia-sdk/x/fswap/types"
)

func (s *KeeperTestSuite) TestInitAndExportGenesis() {
ctx, _ := s.ctx.CacheContext()
testGenesis := stubGenesisState()
err := s.keeper.InitGenesis(ctx, testGenesis)
s.Require().NoError(err)

exportGenesis := s.keeper.ExportGenesis(ctx)
fmt.Println(len(exportGenesis.GetSwaps()))
s.Require().Equal(testGenesis, exportGenesis)
s.Require().Equal(testGenesis.GetSwaps(), exportGenesis.GetSwaps())
s.Require().Equal(testGenesis.GetSwapStats(), exportGenesis.GetSwapStats())
s.Require().Equal(testGenesis.GetSwappeds(), exportGenesis.GetSwappeds())
}

func TestInitGenesis(t *testing.T) {
checkTx := false
app := simapp.Setup(checkTx)
testdata.RegisterInterfaces(app.InterfaceRegistry())
testdata.RegisterMsgServer(app.MsgServiceRouter(), testdata.MsgServerImpl{})
ctx := app.BaseApp.NewContext(checkTx, tmproto.Header{})
keeper := app.FswapKeeper

tests := []struct {
name string
genState *types.GenesisState
expectedError error
}{
{
name: "valid",
genState: stubGenesisState(),
expectedError: nil,
},
{
name: "invalid: swapCount",
genState: func() *types.GenesisState {
state := stubGenesisState()
state.SwapStats.SwapCount = -1
return state
}(),
expectedError: types.ErrInvalidState,
},
{
name: "invalid: swaps count exceeds limit",
genState: func() *types.GenesisState {
state := stubGenesisState()
state.Swaps = append(state.Swaps, state.Swaps[0])
state.Swappeds = append(state.Swappeds, state.Swappeds[0])
state.SwapStats.SwapCount = 2
return state
}(),
expectedError: types.ErrCanNotHaveMoreSwap,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
err := keeper.InitGenesis(ctx, tc.genState)
require.ErrorIs(t, tc.expectedError, err)
})
}
}

func stubGenesisState() *types.GenesisState {
testSwapRate, _ := sdk.NewDecFromStr("1234567890")
testGenesis := &types.GenesisState{
return &types.GenesisState{
Swaps: []types.Swap{
{
FromDenom: "aaa",
Expand All @@ -35,13 +101,4 @@ func (s *KeeperTestSuite) TestInitAndExportGenesis() {
},
},
}
err := s.keeper.InitGenesis(ctx, testGenesis)
s.Require().NoError(err)

exportGenesis := s.keeper.ExportGenesis(ctx)
fmt.Println(len(exportGenesis.GetSwaps()))
s.Require().Equal(testGenesis, exportGenesis)
s.Require().Equal(testGenesis.GetSwaps(), exportGenesis.GetSwaps())
s.Require().Equal(testGenesis.GetSwapStats(), exportGenesis.GetSwapStats())
s.Require().Equal(testGenesis.GetSwappeds(), exportGenesis.GetSwappeds())
}
232 changes: 206 additions & 26 deletions x/fswap/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/Finschia/finschia-sdk/baseapp"
"github.com/Finschia/finschia-sdk/simapp"
sdk "github.com/Finschia/finschia-sdk/types"
"github.com/Finschia/finschia-sdk/types/query"
bank "github.com/Finschia/finschia-sdk/x/bank/types"
"github.com/Finschia/finschia-sdk/x/fswap/keeper"
"github.com/Finschia/finschia-sdk/x/fswap/types"
Expand Down Expand Up @@ -42,7 +43,7 @@ func (s *FSwapQueryTestSuite) SetupTest() {
s.queryClient = types.NewQueryClient(queryHelper)
s.keeper = s.app.FswapKeeper

pebSwapRateForCony, err := sdk.NewDecFromStr("148079656000000")
keiSwapRateForCony, err := sdk.NewDecFromStr("148079656000000")
s.Require().NoError(err)
swapCap := sdk.NewInt(1000)
s.Require().NoError(err)
Expand All @@ -52,7 +53,7 @@ func (s *FSwapQueryTestSuite) SetupTest() {
FromDenom: s.fromDenom,
ToDenom: s.toDenom,
AmountCapForToDenom: swapCap,
SwapRate: pebSwapRateForCony,
SwapRate: keiSwapRateForCony,
}
s.toDenomMetadata = bank.Metadata{
Description: "This is metadata for to-coin",
Expand Down Expand Up @@ -88,48 +89,227 @@ func (s *FSwapQueryTestSuite) SetupTest() {
func (s *FSwapQueryTestSuite) TestQuerySwapRequest() {
tests := []struct {
name string
FromDenom string
ToDenom string
wantErr bool
request *types.QuerySwapRequest
expectedResponse *types.QuerySwapResponse
expectedGrpcCode codes.Code
}{
{
name: "valid",
FromDenom: s.fromDenom,
ToDenom: s.toDenom,
wantErr: false,
name: "valid",
request: &types.QuerySwapRequest{
FromDenom: s.fromDenom,
ToDenom: s.toDenom,
},
expectedResponse: &types.QuerySwapResponse{
Swap: types.Swap{
FromDenom: s.swap.FromDenom,
ToDenom: s.swap.ToDenom,
AmountCapForToDenom: s.swap.AmountCapForToDenom,
SwapRate: s.swap.SwapRate,
},
},
expectedGrpcCode: codes.OK,
},
{
name: "invalid: empty fromDenom",
FromDenom: "",
ToDenom: s.toDenom,
wantErr: true,
name: "invalid: empty fromDenom",
request: &types.QuerySwapRequest{
FromDenom: "",
ToDenom: s.toDenom,
},
expectedGrpcCode: codes.InvalidArgument,
},
{
name: "invalid: empty toDenom",
FromDenom: s.fromDenom,
ToDenom: "",
wantErr: true,
name: "invalid: empty toDenom",
request: &types.QuerySwapRequest{
FromDenom: s.fromDenom,
ToDenom: "",
},
expectedGrpcCode: codes.InvalidArgument,
},
{
name: "invalid: the same fromDenom and toDenom",
FromDenom: s.fromDenom,
ToDenom: s.fromDenom,
wantErr: true,
name: "invalid: the same fromDenom and toDenom",
request: &types.QuerySwapRequest{
FromDenom: s.fromDenom,
ToDenom: s.fromDenom,
},
expectedGrpcCode: codes.InvalidArgument,
},
{
name: "invalid: unregistered swap",
request: &types.QuerySwapRequest{
FromDenom: s.toDenom,
ToDenom: s.fromDenom,
},
expectedGrpcCode: codes.NotFound,
},
}
for _, tc := range tests {
s.Run(tc.name, func() {
response, err := s.queryClient.Swap(s.ctx.Context(), tc.request)
s.Require().Equal(tc.expectedResponse, response)
actualGrpcCode := status.Code(err)
s.Require().Equal(tc.expectedGrpcCode, actualGrpcCode, actualGrpcCode.String())
})
}
}

func (s *FSwapQueryTestSuite) TestQuerySwappedRequest() {
tests := []struct {
name string
request *types.QuerySwappedRequest
expectedResponse *types.QuerySwappedResponse
expectedGrpcCode codes.Code
}{
{
name: "valid",
request: &types.QuerySwappedRequest{
FromDenom: s.fromDenom,
ToDenom: s.toDenom,
},
expectedResponse: &types.QuerySwappedResponse{
FromCoinAmount: sdk.NewCoin(s.fromDenom, sdk.ZeroInt()),
ToCoinAmount: sdk.NewCoin(s.toDenom, sdk.ZeroInt()),
},
expectedGrpcCode: codes.OK,
},
{
name: "invalid: empty fromDenom",
request: &types.QuerySwappedRequest{
FromDenom: "",
ToDenom: s.toDenom,
},
expectedResponse: nil,
expectedGrpcCode: codes.Unknown,
},
{
name: "invalid: empty toDenom",
request: &types.QuerySwappedRequest{
FromDenom: s.fromDenom,
ToDenom: "",
},
expectedResponse: nil,
expectedGrpcCode: codes.Unknown,
},
{
name: "invalid: unregistered swap",
request: &types.QuerySwappedRequest{
FromDenom: s.toDenom,
ToDenom: s.fromDenom,
},
expectedResponse: nil,
expectedGrpcCode: codes.Unknown,
},
}
for _, tc := range tests {
s.Run(tc.name, func() {
m := &types.QuerySwapRequest{
FromDenom: tc.FromDenom,
ToDenom: tc.ToDenom,
}
response, err := s.queryClient.Swapped(s.ctx.Context(), tc.request)
s.Require().Equal(tc.expectedResponse, response)
actualGrpcCode := status.Code(err)
s.Require().Equal(tc.expectedGrpcCode, actualGrpcCode, actualGrpcCode.String())
})
}
}

_, err := s.queryClient.Swap(s.ctx.Context(), m)
func (s *FSwapQueryTestSuite) TestQueryTotalSwappableToCoinAmountRequest() {
tests := []struct {
name string
request *types.QueryTotalSwappableToCoinAmountRequest
expectedResponse *types.QueryTotalSwappableToCoinAmountResponse
expectedGrpcCode codes.Code
}{
{
name: "valid",
request: &types.QueryTotalSwappableToCoinAmountRequest{
FromDenom: s.fromDenom,
ToDenom: s.toDenom,
},
expectedResponse: &types.QueryTotalSwappableToCoinAmountResponse{
SwappableAmount: sdk.NewCoin(s.toDenom, s.swap.AmountCapForToDenom),
},
expectedGrpcCode: codes.OK,
},
{
name: "invalid: empty fromDenom",
request: &types.QueryTotalSwappableToCoinAmountRequest{
FromDenom: "",
ToDenom: s.toDenom,
},
expectedResponse: nil,
expectedGrpcCode: codes.Unknown,
},
{
name: "invalid: empty toDenom",
request: &types.QueryTotalSwappableToCoinAmountRequest{
FromDenom: s.fromDenom,
ToDenom: "",
},
expectedResponse: nil,
expectedGrpcCode: codes.Unknown,
},
{
name: "invalid: unregistered swap",
request: &types.QueryTotalSwappableToCoinAmountRequest{
FromDenom: s.toDenom,
ToDenom: s.fromDenom,
},
expectedResponse: nil,
expectedGrpcCode: codes.Unknown,
},
}
for _, tc := range tests {
s.Run(tc.name, func() {
response, err := s.queryClient.TotalSwappableToCoinAmount(s.ctx.Context(), tc.request)
s.Require().Equal(tc.expectedResponse, response)
actualGrpcCode := status.Code(err)
s.Require().Equal(tc.expectedGrpcCode, actualGrpcCode, actualGrpcCode.String())
})
}
}

func (s *FSwapQueryTestSuite) TestQuerySwapsRequest() {
tests := []struct {
name string
request *types.QuerySwapsRequest
expectedResponse *types.QuerySwapsResponse
expectedGrpcCode codes.Code
}{
{
name: "valid",
request: &types.QuerySwapsRequest{
Pagination: nil,
},
expectedResponse: &types.QuerySwapsResponse{
Swaps: []types.Swap{
{
FromDenom: s.swap.FromDenom,
ToDenom: s.swap.ToDenom,
AmountCapForToDenom: s.swap.AmountCapForToDenom,
SwapRate: s.swap.SwapRate,
},
},
Pagination: &query.PageResponse{
NextKey: nil,
Total: 1,
},
},
expectedGrpcCode: codes.OK,
},
{
name: "invalid request",
request: &types.QuerySwapsRequest{
Pagination: &query.PageRequest{
Key: []byte("invalid-key"),
Offset: 1,
Limit: 0,
},
},
expectedResponse: nil,
expectedGrpcCode: codes.Unknown,
},
}
for _, tc := range tests {
s.Run(tc.name, func() {
response, err := s.queryClient.Swaps(s.ctx.Context(), tc.request)
s.Require().Equal(tc.expectedResponse, response)
actualGrpcCode := status.Code(err)
s.Require().Equal(tc.expectedGrpcCode, actualGrpcCode, actualGrpcCode.String())
})
Expand Down
Loading
Loading