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
  • Loading branch information
jaeseung-bae authored and mergify[bot] committed Jun 13, 2024
1 parent aaf3936 commit 08e5ac0
Show file tree
Hide file tree
Showing 8 changed files with 733 additions and 70 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Features

### Improvements
<<<<<<< HEAD
=======
* (docs) [\#1120](https://github.com/Finschia/finschia-sdk/pull/1120) Update links in x/foundation README.md
* (feat) [\#1121](https://github.com/Finschia/finschia-sdk/pull/1121) Add update-censorship cmd to x/foundation cli
* (server) [#1153](https://github.com/Finschia/finschia-sdk/pull/1153) remove grpc replace directive
* (crypto) [\#1163](https://github.com/Finschia/finschia-sdk/pull/1163) Update some secp256k1 logics with latest `dcrec`
* (x/crisis) [#1167](https://github.com/Finschia/finschia-sdk/pull/1167) Use `CacheContext()` in `AssertInvariants()`
* (chore) [\#1168](https://github.com/Finschia/finschia-sdk/pull/1168) Replace `ExactArgs(0)` with `NoArgs()` in `x/upgrade` module
* (server) [\#1175](https://github.com/Finschia/finschia-sdk/pull/1175) Use go embed for swagger
* (x/collection) [\#1287](https://github.com/Finschia/finschia-sdk/pull/1287) add nft id validation to MsgSendNFT
* (types) [\#1314](https://github.com/Finschia/finschia-sdk/pull/1314) replace IsEqual with Equal
* (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

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())
}
Loading

0 comments on commit 08e5ac0

Please sign in to comment.