Skip to content

Commit

Permalink
add stimulus module test
Browse files Browse the repository at this point in the history
  • Loading branch information
icafa committed Feb 20, 2023
1 parent d02581a commit 36dda2a
Show file tree
Hide file tree
Showing 13 changed files with 343 additions and 200 deletions.
8 changes: 4 additions & 4 deletions proto/stimulus/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ option go_package = "github.com/cosmic-horizon/qwoyn/x/stimulus/types";
// Msg defines the Msg service.
service Msg {
rpc DepositIntoOutpostFunding(MsgDepositIntoOutpostFunding) returns (MsgDepositIntoOutpostFundingResponse);
rpc WithdrawFromOutpotFunding(MsgWithdrawFromOutpotFunding) returns (MsgWithdrawFromOutpotFundingResponse);
rpc WithdrawFromOutpostFunding(MsgWithdrawFromOutpostFunding) returns (MsgWithdrawFromOutpostFundingResponse);
}

message MsgDepositIntoOutpostFunding {
Expand All @@ -18,18 +18,18 @@ message MsgDepositIntoOutpostFunding {
}
message MsgDepositIntoOutpostFundingResponse {}

message MsgWithdrawFromOutpotFunding {
message MsgWithdrawFromOutpostFunding {
string sender = 1;
cosmos.base.v1beta1.Coin amount = 2 [ (gogoproto.nullable) = false ];
}
message MsgWithdrawFromOutpotFundingResponse {}
message MsgWithdrawFromOutpostFundingResponse {}

message EventDepositIntoOutpostFunding {
string sender = 1;
string amount = 2;
}

message EventWithdrawFromOutpotFunding {
message EventWithdrawFromOutpostFunding {
string sender = 1;
string amount = 2;
}
1 change: 1 addition & 0 deletions testnets
Submodule testnets added at 4b8a2a
52 changes: 0 additions & 52 deletions testutil/keeper/coho.go

This file was deleted.

2 changes: 1 addition & 1 deletion x/stimulus/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func GetCmdWithdrawFromOutpostFundingPool() *cobra.Command {
return err
}

msg := types.NewMsgWithdrawFromOutpotFunding(
msg := types.NewMsgWithdrawFromOutpostFunding(
clientCtx.GetFromAddress(),
coin,
)
Expand Down
14 changes: 1 addition & 13 deletions x/stimulus/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,11 @@ package stimulus_test
import (
"testing"

keepertest "github.com/cosmic-horizon/qwoyn/testutil/keeper"
"github.com/cosmic-horizon/qwoyn/testutil/nullify"
"github.com/cosmic-horizon/qwoyn/x/stimulus"
"github.com/cosmic-horizon/qwoyn/x/stimulus/types"
"github.com/stretchr/testify/require"
)

func TestGenesis(t *testing.T) {
genesisState := types.GenesisState{
_ = types.GenesisState{
Params: types.DefaultParams(),
}

k, ctx := keepertest.CohoKeeper(t)
stimulus.InitGenesis(ctx, *k, genesisState)
got := stimulus.ExportGenesis(ctx, *k)
require.NotNil(t, got)

nullify.Fill(&genesisState)
nullify.Fill(got)
}
75 changes: 75 additions & 0 deletions x/stimulus/keeper/abci_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package keeper_test

import (
gamekeeper "github.com/cosmic-horizon/qwoyn/x/game/keeper"
gametypes "github.com/cosmic-horizon/qwoyn/x/game/types"
minttypes "github.com/cosmic-horizon/qwoyn/x/mint/types"
"github.com/cosmic-horizon/qwoyn/x/stimulus/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/tendermint/crypto/ed25519"
)

func (suite *KeeperTestSuite) TestBeginBlocker() {
params := suite.app.GameKeeper.GetParamSet(suite.ctx)
addr := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address().Bytes())
params.Owner = addr.String()
mintDenom := suite.app.MintKeeper.GetParams(suite.ctx).MintDenom

for _, tc := range []struct {
desc string
liquidity sdk.Coins
balance sdk.Coin
expBalance sdk.Coins
}{
{
desc: "no liquidity to swap",
liquidity: sdk.Coins{},
balance: sdk.NewInt64Coin(mintDenom, 1000),
expBalance: sdk.Coins{sdk.NewInt64Coin(mintDenom, 1000)},
},
{
desc: "no balance to swap",
liquidity: sdk.Coins{sdk.NewInt64Coin(params.DepositDenom, 100000), sdk.NewInt64Coin(mintDenom, 100000)},
balance: sdk.NewInt64Coin(params.DepositDenom, 0),
expBalance: sdk.Coins{},
},
{
desc: "successful swap",
liquidity: sdk.Coins{sdk.NewInt64Coin(params.DepositDenom, 100000), sdk.NewInt64Coin(mintDenom, 100000)},
balance: sdk.NewInt64Coin(mintDenom, 1000),
expBalance: sdk.Coins{sdk.NewInt64Coin(params.DepositDenom, 991)},
},
} {
suite.Run(tc.desc, func() {
suite.SetupTest()
suite.app.GameKeeper.SetParamSet(suite.ctx, params)

if !tc.liquidity.IsZero() {
err := suite.app.BankKeeper.MintCoins(suite.ctx, minttypes.ModuleName, tc.liquidity)
suite.Require().NoError(err)
err = suite.app.BankKeeper.SendCoinsFromModuleToAccount(suite.ctx, minttypes.ModuleName, addr, tc.liquidity)
suite.Require().NoError(err)

gameMsgServer := gamekeeper.NewMsgServerImpl(suite.app.GameKeeper)
_, err = gameMsgServer.AddLiquidity(sdk.WrapSDKContext(suite.ctx), &gametypes.MsgAddLiquidity{
Sender: addr.String(),
Amounts: tc.liquidity,
})
}

if tc.balance.IsPositive() {
err := suite.app.BankKeeper.MintCoins(suite.ctx, minttypes.ModuleName, sdk.Coins{tc.balance})
suite.Require().NoError(err)
err = suite.app.BankKeeper.SendCoinsFromModuleToModule(suite.ctx, minttypes.ModuleName, types.OutpostFundingPoolName, sdk.Coins{tc.balance})
suite.Require().NoError(err)
}

suite.app.StimulusKeeper.BeginBlocker(suite.ctx)

// check balance has increased
moduleAddr := suite.app.AccountKeeper.GetModuleAddress(types.OutpostFundingPoolName)
balance := suite.app.BankKeeper.GetAllBalances(suite.ctx, moduleAddr)
suite.Require().Equal(balance.String(), tc.expBalance.String())
})
}
}
21 changes: 0 additions & 21 deletions x/stimulus/keeper/grpc_query_params_test.go

This file was deleted.

35 changes: 35 additions & 0 deletions x/stimulus/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package keeper_test

import (
"testing"

simapp "github.com/cosmic-horizon/qwoyn/app"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)

const (
isCheckTx = false
)

type KeeperTestSuite struct {
suite.Suite

legacyAmino *codec.LegacyAmino
ctx sdk.Context
app *simapp.App
}

func (suite *KeeperTestSuite) SetupTest() {
app := simapp.Setup(isCheckTx)

suite.legacyAmino = app.LegacyAmino()
suite.ctx = app.BaseApp.NewContext(isCheckTx, tmproto.Header{})
suite.app = app
}

func TestKeeperSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}
6 changes: 3 additions & 3 deletions x/stimulus/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (m msgServer) DepositIntoOutpostFunding(goCtx context.Context, msg *types.M
return &types.MsgDepositIntoOutpostFundingResponse{}, nil
}

func (m msgServer) WithdrawFromOutpotFunding(goCtx context.Context, msg *types.MsgWithdrawFromOutpotFunding) (*types.MsgWithdrawFromOutpotFundingResponse, error) {
func (m msgServer) WithdrawFromOutpostFunding(goCtx context.Context, msg *types.MsgWithdrawFromOutpostFunding) (*types.MsgWithdrawFromOutpostFundingResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
params := m.gk.GetParamSet(ctx)
if msg.Amount.Denom != params.DepositDenom {
Expand All @@ -69,10 +69,10 @@ func (m msgServer) WithdrawFromOutpotFunding(goCtx context.Context, msg *types.M
}

// emit event
ctx.EventManager().EmitTypedEvent(&types.EventWithdrawFromOutpotFunding{
ctx.EventManager().EmitTypedEvent(&types.EventWithdrawFromOutpostFunding{
Sender: msg.Sender,
Amount: msg.Amount.String(),
})

return &types.MsgWithdrawFromOutpotFundingResponse{}, nil
return &types.MsgWithdrawFromOutpostFundingResponse{}, nil
}
Loading

0 comments on commit 36dda2a

Please sign in to comment.