Skip to content

Commit

Permalink
chore: add additional testcases for fswap (#1415)
Browse files Browse the repository at this point in the history
* chore: add more test for grpc_query

* chore: add more test for config and msg validation

* chore: add more test for genesis-related things

* chore: add TestMsgSetSwap for msg server

* chore: add more test for keeper

* chore: fix for lint

* chore: update changelog

* chore: rename kai to kei

* chore: introduce stubGenesisState func for DRY

* chore: remove unnecessary code

* chore: remove unnecessary code

(cherry picked from commit 10726cf)

# Conflicts:
#	CHANGELOG.md
#	x/fswap/keeper/genesis_test.go
#	x/fswap/keeper/grpc_query_test.go
#	x/fswap/keeper/keeper_test.go
#	x/fswap/keeper/msg_server_test.go
#	x/fswap/types/genesis_test.go
#	x/fswap/types/msgs_test.go
  • Loading branch information
jaeseung-bae authored and mergify[bot] committed Jun 13, 2024
1 parent 464eeed commit e58cb3f
Show file tree
Hide file tree
Showing 8 changed files with 1,757 additions and 0 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements
* (types) [\#1314](https://github.com/Finschia/finschia-sdk/pull/1314) replace IsEqual with Equal
<<<<<<< HEAD
=======
* (x/fswap) [\#1363](https://github.com/Finschia/finschia-sdk/pull/1363) introduce new event for MakeSwapProposal
* (x/fbridge) [\#1366](https://github.com/Finschia/finschia-sdk/pull/1366) Set target denom as module parameters
* (x/fbridge) [\#1369](https://github.com/Finschia/finschia-sdk/pull/1369) Add the event of `SetBridgeStatus`
* (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/fbridge) [\#1395](https://github.com/Finschia/finschia-sdk/pull/1395) Return error instead of panic for behaviors triggered by client
* (x/fswap) [\#1396](https://github.com/Finschia/finschia-sdk/pull/1396) refactor to use snake_case in proto
* (x/fswap) [\#1391](https://github.com/Finschia/finschia-sdk/pull/1391) add cli_test for fswap module
* (x/fswap) [\#1415](https://github.com/Finschia/finschia-sdk/pull/1415) add more testcases for fswap module
>>>>>>> 10726cfaf (chore: add additional testcases for fswap (#1415))
### Bug Fixes
* (x/auth) [#1281](https://github.com/Finschia/finschia-sdk/pull/1281) `ModuleAccount.Validate` now reports a nil `.BaseAccount` instead of panicking. (backport #1274)
Expand Down
104 changes: 104 additions & 0 deletions x/fswap/keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
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")
return &types.GenesisState{
Swaps: []types.Swap{
{
FromDenom: "aaa",
ToDenom: "bbb",
AmountCapForToDenom: sdk.NewInt(1234567890000),
SwapRate: testSwapRate,
},
},
SwapStats: types.SwapStats{
SwapCount: 1,
},
Swappeds: []types.Swapped{
{
FromCoinAmount: sdk.Coin{
Denom: "aaa",
Amount: sdk.ZeroInt(),
},
ToCoinAmount: sdk.Coin{
Denom: "bbb",
Amount: sdk.ZeroInt(),
},
},
},
}
}
Loading

0 comments on commit e58cb3f

Please sign in to comment.