Skip to content

Commit

Permalink
Add unit test for VoteGauge
Browse files Browse the repository at this point in the history
  • Loading branch information
jununifi committed Jun 27, 2024
1 parent 57307b2 commit d0631b4
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 11 deletions.
8 changes: 4 additions & 4 deletions x/liquidityincentive/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
"github.com/sunriselayer/sunrise/x/liquidityincentive/types"
)

func setupMsgServer(t testing.TB) (keeper.Keeper, types.MsgServer, context.Context) {
k, _, ctx := keepertest.LiquidityincentiveKeeper(t)
return k, keeper.NewMsgServerImpl(k), ctx
func setupMsgServer(t testing.TB) (keeper.Keeper, keepertest.LiquidityIncentiveMocks, types.MsgServer, context.Context) {
k, mocks, ctx := keepertest.LiquidityincentiveKeeper(t)
return k, mocks, keeper.NewMsgServerImpl(k), ctx
}

func TestMsgServer(t *testing.T) {
k, ms, ctx := setupMsgServer(t)
k, _, ms, ctx := setupMsgServer(t)
require.NotNil(t, ms)
require.NotNil(t, ctx)
require.NotEmpty(t, k)
Expand Down
70 changes: 69 additions & 1 deletion x/liquidityincentive/keeper/msg_server_vote_gauge_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,71 @@
package keeper_test

// TODO: add test for VoteGauge
import (
"testing"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

"github.com/sunriselayer/sunrise/x/liquidityincentive/types"
liquiditypooltypes "github.com/sunriselayer/sunrise/x/liquiditypool/types"
)

func TestMsgVoteGauge(t *testing.T) {
sender := "sunrise126ss57ayztn5287spvxq0dpdfarj6rk0v3p06f"
k, mocks, ms, ctx := setupMsgServer(t)
params := types.DefaultParams()
require.NoError(t, k.SetParams(ctx, params))
wctx := sdk.UnwrapSDKContext(ctx)

mocks.LiquiditypoolKeeper.EXPECT().GetPool(gomock.Any(), uint64(1)).Return(liquiditypooltypes.Pool{}, true).AnyTimes()
mocks.LiquiditypoolKeeper.EXPECT().GetPool(gomock.Any(), uint64(2)).Return(liquiditypooltypes.Pool{}, true).AnyTimes()
mocks.LiquiditypoolKeeper.EXPECT().GetPool(gomock.Any(), uint64(3)).Return(liquiditypooltypes.Pool{}, false).AnyTimes()

// default params
testCases := []struct {
name string
input *types.MsgVoteGauge
expErr bool
expErrMsg string
}{
{
name: "not available pool",
input: &types.MsgVoteGauge{
Sender: sender,
Weights: []types.PoolWeight{{PoolId: 3, Weight: math.LegacyOneDec()}},
},
expErr: true,
expErrMsg: "pool not found",
},
{
name: "available pools",
input: &types.MsgVoteGauge{
Sender: sender,
Weights: []types.PoolWeight{{PoolId: 1, Weight: math.LegacyNewDecWithPrec(50, 2)}, {PoolId: 1, Weight: math.LegacyNewDecWithPrec(50, 2)}},
},
expErr: false,
},
{
name: "empty votes",
input: &types.MsgVoteGauge{
Sender: sender,
Weights: []types.PoolWeight{},
},
expErr: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, err := ms.VoteGauge(wctx, tc.input)
if tc.expErr {
require.Error(t, err)
require.Contains(t, err.Error(), tc.expErrMsg)
} else {
require.NoError(t, err)
}
})
}
}
2 changes: 1 addition & 1 deletion x/liquidityincentive/keeper/msg_update_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestMsgUpdateParams(t *testing.T) {
k, ms, ctx := setupMsgServer(t)
k, _, ms, ctx := setupMsgServer(t)
params := types.DefaultParams()
require.NoError(t, k.SetParams(ctx, params))
wctx := sdk.UnwrapSDKContext(ctx)
Expand Down
2 changes: 1 addition & 1 deletion x/liquidityincentive/module/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestGenesis(t *testing.T) {
// this line is used by starport scaffolding # genesis/test/state
}

k, ctx := keepertest.LiquidityincentiveKeeper(t)
k, _, ctx := keepertest.LiquidityincentiveKeeper(t)
liquidityincentive.InitGenesis(ctx, k, genesisState)
got := liquidityincentive.ExportGenesis(ctx, k)
require.NotNil(t, got)
Expand Down
6 changes: 3 additions & 3 deletions x/liquiditypool/keeper/msg_server_position_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,15 @@ func TestMsgServerDecreaseLiquidity(t *testing.T) {
request: &types.MsgDecreaseLiquidity{
Sender: sender,
Id: 0,
Liquidity: resp.Liquidity,
Liquidity: resp.Liquidity.String(),
},
},
{
desc: "Unauthorized",
request: &types.MsgDecreaseLiquidity{
Sender: "B",
Id: 0,
Liquidity: resp.Liquidity,
Liquidity: resp.Liquidity.String(),
},
err: sdkerrors.ErrUnauthorized,
},
Expand All @@ -226,7 +226,7 @@ func TestMsgServerDecreaseLiquidity(t *testing.T) {
request: &types.MsgDecreaseLiquidity{
Sender: sender,
Id: 10,
Liquidity: resp.Liquidity,
Liquidity: resp.Liquidity.String(),
},
err: sdkerrors.ErrKeyNotFound,
},
Expand Down
2 changes: 1 addition & 1 deletion x/liquiditypool/keeper/query_calculations.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (k Keeper) CalculationCreatePosition(ctx context.Context, req *types.QueryC
return nil, err
}

liquidityDelta := math.LegacyZeroDec()
var liquidityDelta math.LegacyDec
if req.Denom == pool.DenomBase {
liquidityDelta = types.LiquidityBase(amount, pool.CurrentSqrtPrice, sqrtPriceUpperTick)
_, actualAmountQuote, err := pool.CalcActualAmounts(lowerTick.Int64(), upperTick.Int64(), liquidityDelta)
Expand Down

0 comments on commit d0631b4

Please sign in to comment.