Skip to content

Commit

Permalink
test: refactor some tests (#952)
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmic-vagabond authored Nov 15, 2024
1 parent b644b88 commit deaf2eb
Show file tree
Hide file tree
Showing 74 changed files with 253 additions and 261 deletions.
3 changes: 1 addition & 2 deletions x/accountedpool/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
keepertest "github.com/elys-network/elys/testutil/keeper"
"github.com/elys-network/elys/x/accountedpool/keeper"
"github.com/elys-network/elys/x/accountedpool/types"
Expand All @@ -13,7 +12,7 @@ import (

func setupMsgServer(t testing.TB) (types.MsgServer, context.Context) {
k, ctx := keepertest.AccountedPoolKeeper(t)
return keeper.NewMsgServerImpl(*k), sdk.WrapSDKContext(ctx)
return keeper.NewMsgServerImpl(*k), ctx
}

func TestMsgServer(t *testing.T) {
Expand Down
13 changes: 5 additions & 8 deletions x/accountedpool/keeper/query_accounted_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package keeper_test
import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
Expand All @@ -16,7 +15,6 @@ import (

func TestAccountedPoolQuerySingle(t *testing.T) {
keeper, ctx := keepertest.AccountedPoolKeeper(t)
wctx := sdk.WrapSDKContext(ctx)
msgs := createNAccountedPool(keeper, ctx, 2)
tests := []struct {
desc string
Expand Down Expand Up @@ -52,7 +50,7 @@ func TestAccountedPoolQuerySingle(t *testing.T) {
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
response, err := keeper.AccountedPool(wctx, tc.request)
response, err := keeper.AccountedPool(ctx, tc.request)
if tc.err != nil {
require.ErrorIs(t, err, tc.err)
} else {
Expand All @@ -68,7 +66,6 @@ func TestAccountedPoolQuerySingle(t *testing.T) {

func TestAccountedPoolQueryPaginated(t *testing.T) {
keeper, ctx := keepertest.AccountedPoolKeeper(t)
wctx := sdk.WrapSDKContext(ctx)
msgs := createNAccountedPool(keeper, ctx, 5)

request := func(next []byte, offset, limit uint64, total bool) *types.QueryAllAccountedPoolRequest {
Expand All @@ -84,7 +81,7 @@ func TestAccountedPoolQueryPaginated(t *testing.T) {
t.Run("ByOffset", func(t *testing.T) {
step := 2
for i := 0; i < len(msgs); i += step {
resp, err := keeper.AccountedPoolAll(wctx, request(nil, uint64(i), uint64(step), false))
resp, err := keeper.AccountedPoolAll(ctx, request(nil, uint64(i), uint64(step), false))
require.NoError(t, err)
require.LessOrEqual(t, len(resp.AccountedPool), step)
require.Subset(t,
Expand All @@ -97,7 +94,7 @@ func TestAccountedPoolQueryPaginated(t *testing.T) {
step := 2
var next []byte
for i := 0; i < len(msgs); i += step {
resp, err := keeper.AccountedPoolAll(wctx, request(next, 0, uint64(step), false))
resp, err := keeper.AccountedPoolAll(ctx, request(next, 0, uint64(step), false))
require.NoError(t, err)
require.LessOrEqual(t, len(resp.AccountedPool), step)
require.Subset(t,
Expand All @@ -108,7 +105,7 @@ func TestAccountedPoolQueryPaginated(t *testing.T) {
}
})
t.Run("Total", func(t *testing.T) {
resp, err := keeper.AccountedPoolAll(wctx, request(nil, 0, 0, true))
resp, err := keeper.AccountedPoolAll(ctx, request(nil, 0, 0, true))
require.NoError(t, err)
require.Equal(t, len(msgs), int(resp.Pagination.Total))
require.ElementsMatch(t,
Expand All @@ -117,7 +114,7 @@ func TestAccountedPoolQueryPaginated(t *testing.T) {
)
})
t.Run("InvalidRequest", func(t *testing.T) {
_, err := keeper.AccountedPoolAll(wctx, nil)
_, err := keeper.AccountedPoolAll(ctx, nil)
require.ErrorIs(t, err, status.Error(codes.InvalidArgument, "invalid request"))
})
}
4 changes: 2 additions & 2 deletions x/amm/keeper/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,13 @@ func (suite *AmmKeeperTestSuite) TestExecuteSwapRequests() {
switch msg := msg.(type) {
case *types.MsgSwapExactAmountIn:
_, err := msgServer.SwapExactAmountIn(
sdk.WrapSDKContext(suite.ctx),
suite.ctx,
msg,
)
suite.Require().NoError(err)
case *types.MsgSwapExactAmountOut:
_, err := msgServer.SwapExactAmountOut(
sdk.WrapSDKContext(suite.ctx),
suite.ctx,
msg,
)
suite.Require().NoError(err)
Expand Down
2 changes: 1 addition & 1 deletion x/amm/keeper/msg_server_create_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (suite *AmmKeeperTestSuite) TestMsgServerCreatePool() {
suite.app.AmmKeeper.SetParams(suite.ctx, params)

resp, err := msgServer.CreatePool(
sdk.WrapSDKContext(suite.ctx),
suite.ctx,
&types.MsgCreatePool{
Sender: sender.String(),
PoolParams: &tc.poolParams,
Expand Down
4 changes: 2 additions & 2 deletions x/amm/keeper/msg_server_exit_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (suite *AmmKeeperTestSuite) TestMsgServerExitPool() {
},
}
_, err = msgServer.CreatePool(
sdk.WrapSDKContext(suite.ctx),
suite.ctx,
&types.MsgCreatePool{
Sender: sender.String(),
PoolParams: &tc.poolParams,
Expand All @@ -147,7 +147,7 @@ func (suite *AmmKeeperTestSuite) TestMsgServerExitPool() {
pool := suite.app.AmmKeeper.GetAllPool(suite.ctx)[0]
suite.ctx = suite.ctx.WithBlockTime(suite.ctx.BlockTime().Add(time.Hour))
resp, err := msgServer.ExitPool(
sdk.WrapSDKContext(suite.ctx),
suite.ctx,
&types.MsgExitPool{
Sender: sender.String(),
PoolId: pool.PoolId,
Expand Down
15 changes: 5 additions & 10 deletions x/amm/keeper/msg_server_feed_multiple_external_liquidity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,8 @@ func (suite *AmmKeeperTestSuite) TestFeedMultipleExternalLiquidity() {
func() {
// msg server
msgServer := keeper.NewMsgServerImpl(*suite.app.AmmKeeper)
goCtx := sdk.WrapSDKContext(suite.ctx)

_, err := msgServer.FeedMultipleExternalLiquidity(goCtx, &types.MsgFeedMultipleExternalLiquidity{})
_, err := msgServer.FeedMultipleExternalLiquidity(suite.ctx, &types.MsgFeedMultipleExternalLiquidity{})
suite.Require().Error(err)
},
},
Expand All @@ -263,11 +262,10 @@ func (suite *AmmKeeperTestSuite) TestFeedMultipleExternalLiquidity() {
func() {
// msg server
msgServer := keeper.NewMsgServerImpl(*suite.app.AmmKeeper)
goCtx := sdk.WrapSDKContext(suite.ctx)

addr := suite.AddAccounts(1, nil)[0]

_, err := msgServer.FeedMultipleExternalLiquidity(goCtx, &types.MsgFeedMultipleExternalLiquidity{
_, err := msgServer.FeedMultipleExternalLiquidity(suite.ctx, &types.MsgFeedMultipleExternalLiquidity{
Sender: addr.String(),
Liquidity: []types.ExternalLiquidity{
{
Expand All @@ -293,7 +291,6 @@ func (suite *AmmKeeperTestSuite) TestFeedMultipleExternalLiquidity() {
func() {
// msg server
msgServer := keeper.NewMsgServerImpl(*suite.app.AmmKeeper)
goCtx := sdk.WrapSDKContext(suite.ctx)

addr := suite.AddAccounts(1, nil)[0]

Expand All @@ -302,7 +299,7 @@ func (suite *AmmKeeperTestSuite) TestFeedMultipleExternalLiquidity() {
IsActive: false,
})

_, err := msgServer.FeedMultipleExternalLiquidity(goCtx, &types.MsgFeedMultipleExternalLiquidity{
_, err := msgServer.FeedMultipleExternalLiquidity(suite.ctx, &types.MsgFeedMultipleExternalLiquidity{
Sender: addr.String(),
Liquidity: []types.ExternalLiquidity{
{
Expand All @@ -328,7 +325,6 @@ func (suite *AmmKeeperTestSuite) TestFeedMultipleExternalLiquidity() {
func() {
// msg server
msgServer := keeper.NewMsgServerImpl(*suite.app.AmmKeeper)
goCtx := sdk.WrapSDKContext(suite.ctx)

addr := suite.AddAccounts(1, nil)[0]

Expand All @@ -337,7 +333,7 @@ func (suite *AmmKeeperTestSuite) TestFeedMultipleExternalLiquidity() {
IsActive: true,
})

_, err := msgServer.FeedMultipleExternalLiquidity(goCtx, &types.MsgFeedMultipleExternalLiquidity{
_, err := msgServer.FeedMultipleExternalLiquidity(suite.ctx, &types.MsgFeedMultipleExternalLiquidity{
Sender: addr.String(),
Liquidity: []types.ExternalLiquidity{
{
Expand All @@ -364,7 +360,6 @@ func (suite *AmmKeeperTestSuite) TestFeedMultipleExternalLiquidity() {
func() {
// msg server
msgServer := keeper.NewMsgServerImpl(*suite.app.AmmKeeper)
goCtx := sdk.WrapSDKContext(suite.ctx)

addr := suite.AddAccounts(1, nil)[0]

Expand All @@ -376,7 +371,7 @@ func (suite *AmmKeeperTestSuite) TestFeedMultipleExternalLiquidity() {
IsActive: true,
})

_, err := msgServer.FeedMultipleExternalLiquidity(goCtx, &types.MsgFeedMultipleExternalLiquidity{
_, err := msgServer.FeedMultipleExternalLiquidity(suite.ctx, &types.MsgFeedMultipleExternalLiquidity{
Sender: addr.String(),
Liquidity: []types.ExternalLiquidity{
{
Expand Down
4 changes: 2 additions & 2 deletions x/amm/keeper/msg_server_join_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (suite *AmmKeeperTestSuite) TestMsgServerJoinPool() {
// execute function
msgServer := keeper.NewMsgServerImpl(*suite.app.AmmKeeper)
resp, err := msgServer.JoinPool(
sdk.WrapSDKContext(suite.ctx),
suite.ctx,
&types.MsgJoinPool{
Sender: sender.String(),
PoolId: 1,
Expand Down Expand Up @@ -302,7 +302,7 @@ func (suite *AmmKeeperTestSuite) TestMsgServerJoinPoolExploitScenario() {
// Step 5: New LP adds single-sided liquidity
msgServer := keeper.NewMsgServerImpl(*suite.app.AmmKeeper)
resp, err := msgServer.JoinPool(
sdk.WrapSDKContext(suite.ctx),
suite.ctx,
&types.MsgJoinPool{
Sender: sender.String(),
PoolId: 1,
Expand Down
6 changes: 3 additions & 3 deletions x/amm/keeper/msg_server_swap_exact_amount_in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (suite *AmmKeeperTestSuite) TestMsgServerSwapExactAmountIn() {

msgServer := keeper.NewMsgServerImpl(*suite.app.AmmKeeper)
resp, err := msgServer.SwapExactAmountIn(
sdk.WrapSDKContext(suite.ctx),
suite.ctx,
&types.MsgSwapExactAmountIn{
Sender: sender.String(),
Routes: tc.swapRoute,
Expand Down Expand Up @@ -275,7 +275,7 @@ func (suite *AmmKeeperTestSuite) TestMsgServerSlippageDifferenceWhenSplit() {
cacheCtx, _ := suite.ctx.CacheContext()
msgServer := keeper.NewMsgServerImpl(*suite.app.AmmKeeper)
resp, err := msgServer.SwapExactAmountIn(
sdk.WrapSDKContext(cacheCtx),
cacheCtx,
&types.MsgSwapExactAmountIn{
Sender: sender.String(),
Routes: swapRoute,
Expand All @@ -295,7 +295,7 @@ func (suite *AmmKeeperTestSuite) TestMsgServerSlippageDifferenceWhenSplit() {
cacheCtx, _ = suite.ctx.CacheContext()
for i := 0; i < 100; i++ {
resp, err = msgServer.SwapExactAmountIn(
sdk.WrapSDKContext(cacheCtx),
cacheCtx,
&types.MsgSwapExactAmountIn{
Sender: sender.String(),
Routes: swapRoute,
Expand Down
2 changes: 1 addition & 1 deletion x/amm/keeper/msg_server_swap_exact_amount_out_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (suite *AmmKeeperTestSuite) TestMsgServerSwapExactAmountOut() {

msgServer := keeper.NewMsgServerImpl(*suite.app.AmmKeeper)
resp, err := msgServer.SwapExactAmountOut(
sdk.WrapSDKContext(suite.ctx),
suite.ctx,
&types.MsgSwapExactAmountOut{
Sender: sender.String(),
Routes: tc.swapRoutes,
Expand Down
3 changes: 1 addition & 2 deletions x/amm/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"context"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
keepertest "github.com/elys-network/elys/testutil/keeper"
"github.com/elys-network/elys/x/amm/keeper"
"github.com/elys-network/elys/x/amm/types"
)

func setupMsgServer(t testing.TB) (types.MsgServer, context.Context) {
k, ctx, _, _ := keepertest.AmmKeeper(t)
return keeper.NewMsgServerImpl(*k), sdk.WrapSDKContext(ctx)
return keeper.NewMsgServerImpl(*k), ctx
}
4 changes: 2 additions & 2 deletions x/amm/keeper/msg_server_update_pool_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (suite *AmmKeeperTestSuite) TestMsgServerUpdatePoolParams() {
// execute function
msgServer := keeper.NewMsgServerImpl(*suite.app.AmmKeeper)
resp, err := msgServer.CreatePool(
sdk.WrapSDKContext(suite.ctx),
suite.ctx,
&types.MsgCreatePool{
Sender: sender.String(),
PoolParams: &tc.initialPoolParams,
Expand Down Expand Up @@ -188,7 +188,7 @@ func (suite *AmmKeeperTestSuite) TestMsgServerUpdatePoolParams() {
suite.Require().Equal(commitments.CommittedTokens[0].Amount.String(), tc.expLpCommitment.Amount.String())

resp2, err := msgServer.UpdatePoolParams(
sdk.WrapSDKContext(suite.ctx),
suite.ctx,
&types.MsgUpdatePoolParams{
Authority: sender.String(),
PoolId: pools[0].PoolId,
Expand Down
13 changes: 5 additions & 8 deletions x/amm/keeper/query_denom_liquidity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"strconv"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
Expand All @@ -17,7 +16,6 @@ import (

func TestDenomLiquidityQuerySingle(t *testing.T) {
keeper, ctx, _, _ := keepertest.AmmKeeper(t)
wctx := sdk.WrapSDKContext(ctx)
msgs := createNDenomLiquidity(keeper, ctx, 2)
for _, tc := range []struct {
desc string
Expand Down Expand Up @@ -52,7 +50,7 @@ func TestDenomLiquidityQuerySingle(t *testing.T) {
},
} {
t.Run(tc.desc, func(t *testing.T) {
response, err := keeper.DenomLiquidity(wctx, tc.request)
response, err := keeper.DenomLiquidity(ctx, tc.request)
if tc.err != nil {
require.ErrorIs(t, err, tc.err)
} else {
Expand All @@ -68,7 +66,6 @@ func TestDenomLiquidityQuerySingle(t *testing.T) {

func TestDenomLiquidityQueryPaginated(t *testing.T) {
keeper, ctx, _, _ := keepertest.AmmKeeper(t)
wctx := sdk.WrapSDKContext(ctx)
msgs := createNDenomLiquidity(keeper, ctx, 5)

request := func(next []byte, offset, limit uint64, total bool) *types.QueryAllDenomLiquidityRequest {
Expand All @@ -84,7 +81,7 @@ func TestDenomLiquidityQueryPaginated(t *testing.T) {
t.Run("ByOffset", func(t *testing.T) {
step := 2
for i := 0; i < len(msgs); i += step {
resp, err := keeper.DenomLiquidityAll(wctx, request(nil, uint64(i), uint64(step), false))
resp, err := keeper.DenomLiquidityAll(ctx, request(nil, uint64(i), uint64(step), false))
require.NoError(t, err)
require.LessOrEqual(t, len(resp.DenomLiquidity), step)
require.Subset(t,
Expand All @@ -97,7 +94,7 @@ func TestDenomLiquidityQueryPaginated(t *testing.T) {
step := 2
var next []byte
for i := 0; i < len(msgs); i += step {
resp, err := keeper.DenomLiquidityAll(wctx, request(next, 0, uint64(step), false))
resp, err := keeper.DenomLiquidityAll(ctx, request(next, 0, uint64(step), false))
require.NoError(t, err)
require.LessOrEqual(t, len(resp.DenomLiquidity), step)
require.Subset(t,
Expand All @@ -108,7 +105,7 @@ func TestDenomLiquidityQueryPaginated(t *testing.T) {
}
})
t.Run("Total", func(t *testing.T) {
resp, err := keeper.DenomLiquidityAll(wctx, request(nil, 0, 0, true))
resp, err := keeper.DenomLiquidityAll(ctx, request(nil, 0, 0, true))
require.NoError(t, err)
require.Equal(t, len(msgs), int(resp.Pagination.Total))
require.ElementsMatch(t,
Expand All @@ -117,7 +114,7 @@ func TestDenomLiquidityQueryPaginated(t *testing.T) {
)
})
t.Run("InvalidRequest", func(t *testing.T) {
_, err := keeper.DenomLiquidityAll(wctx, nil)
_, err := keeper.DenomLiquidityAll(ctx, nil)
require.ErrorIs(t, err, status.Error(codes.InvalidArgument, "invalid request"))
})
}
Loading

0 comments on commit deaf2eb

Please sign in to comment.