Skip to content

Commit

Permalink
test: remove setupKeeper from crosschain module (#1992)
Browse files Browse the repository at this point in the history
* Remove setupKeeper from crosschain module

* Changelog

---------

Co-authored-by: Lucas Bertrand <[email protected]>
  • Loading branch information
skosito and lumtis authored Apr 5, 2024
1 parent bc3a88c commit 47661d4
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 115 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
* [1955](https://github.com/zeta-chain/node/pull/1955) - improve emissions module coverage
* [1941](https://github.com/zeta-chain/node/pull/1941) - add unit tests for zetabridge package
* [1985](https://github.com/zeta-chain/node/pull/1985) - improve fungible module coverage
* [1992](https://github.com/zeta-chain/node/pull/1992) - remove setupKeeper from crosschain module

### Fixes

Expand Down
27 changes: 15 additions & 12 deletions x/crosschain/keeper/gas_price_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package keeper
package keeper_test

import (
"strconv"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
"github.com/zeta-chain/zetacore/x/crosschain/keeper"
"github.com/zeta-chain/zetacore/x/crosschain/types"
)

// Keeper Tests
func createNGasPrice(keeper *Keeper, ctx sdk.Context, n int) []types.GasPrice {
func createNGasPrice(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.GasPrice {
items := make([]types.GasPrice, n)
for i := range items {
items[i].Creator = "any"
Expand All @@ -22,26 +24,27 @@ func createNGasPrice(keeper *Keeper, ctx sdk.Context, n int) []types.GasPrice {
}

func TestGasPriceGet(t *testing.T) {
keeper, ctx := setupKeeper(t)
items := createNGasPrice(keeper, ctx, 10)
k, ctx, _, _ := keepertest.CrosschainKeeper(t)
items := createNGasPrice(k, ctx, 10)
for _, item := range items {
rst, found := keeper.GetGasPrice(ctx, item.ChainId)
rst, found := k.GetGasPrice(ctx, item.ChainId)
require.True(t, found)
require.Equal(t, item, rst)
}
}

func TestGasPriceRemove(t *testing.T) {
keeper, ctx := setupKeeper(t)
items := createNGasPrice(keeper, ctx, 10)
k, ctx, _, _ := keepertest.CrosschainKeeper(t)
items := createNGasPrice(k, ctx, 10)
for _, item := range items {
keeper.RemoveGasPrice(ctx, item.Index)
_, found := keeper.GetGasPrice(ctx, item.ChainId)
k.RemoveGasPrice(ctx, item.Index)
_, found := k.GetGasPrice(ctx, item.ChainId)
require.False(t, found)
}
}

func TestGasPriceGetAll(t *testing.T) {
keeper, ctx := setupKeeper(t)
items := createNGasPrice(keeper, ctx, 10)
require.Equal(t, items, keeper.GetAllGasPrice(ctx))
k, ctx, _, _ := keepertest.CrosschainKeeper(t)
items := createNGasPrice(k, ctx, 10)
require.Equal(t, items, k.GetAllGasPrice(ctx))
}
21 changes: 11 additions & 10 deletions x/crosschain/keeper/grpc_query_gas_price_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package keeper
package keeper_test

import (
"fmt"
Expand All @@ -7,15 +7,16 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/stretchr/testify/require"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
"github.com/zeta-chain/zetacore/x/crosschain/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func TestGasPriceQuerySingle(t *testing.T) {
keeper, ctx := setupKeeper(t)
k, ctx, _, _ := keepertest.CrosschainKeeper(t)
wctx := sdk.WrapSDKContext(ctx)
msgs := createNGasPrice(keeper, ctx, 2)
msgs := createNGasPrice(k, ctx, 2)
for _, tc := range []struct {
desc string
request *types.QueryGetGasPriceRequest
Expand Down Expand Up @@ -49,7 +50,7 @@ func TestGasPriceQuerySingle(t *testing.T) {
} {
tc := tc
t.Run(tc.desc, func(t *testing.T) {
response, err := keeper.GasPrice(wctx, tc.request)
response, err := k.GasPrice(wctx, tc.request)
if tc.err != nil {
require.Error(t, err)
} else {
Expand All @@ -60,9 +61,9 @@ func TestGasPriceQuerySingle(t *testing.T) {
}

func TestGasPriceQueryPaginated(t *testing.T) {
keeper, ctx := setupKeeper(t)
k, ctx, _, _ := keepertest.CrosschainKeeper(t)
wctx := sdk.WrapSDKContext(ctx)
msgs := createNGasPrice(keeper, ctx, 5)
msgs := createNGasPrice(k, ctx, 5)

request := func(next []byte, offset, limit uint64, total bool) *types.QueryAllGasPriceRequest {
return &types.QueryAllGasPriceRequest{
Expand All @@ -77,7 +78,7 @@ func TestGasPriceQueryPaginated(t *testing.T) {
t.Run("ByOffset", func(t *testing.T) {
step := 2
for i := 0; i < len(msgs); i += step {
resp, err := keeper.GasPriceAll(wctx, request(nil, uint64(i), uint64(step), false))
resp, err := k.GasPriceAll(wctx, request(nil, uint64(i), uint64(step), false))
require.NoError(t, err)
for j := i; j < len(msgs) && j < i+step; j++ {
require.Equal(t, &msgs[j], resp.GasPrice[j-i])
Expand All @@ -88,7 +89,7 @@ func TestGasPriceQueryPaginated(t *testing.T) {
step := 2
var next []byte
for i := 0; i < len(msgs); i += step {
resp, err := keeper.GasPriceAll(wctx, request(next, 0, uint64(step), false))
resp, err := k.GasPriceAll(wctx, request(next, 0, uint64(step), false))
require.NoError(t, err)
for j := i; j < len(msgs) && j < i+step; j++ {
require.Equal(t, &msgs[j], resp.GasPrice[j-i])
Expand All @@ -97,12 +98,12 @@ func TestGasPriceQueryPaginated(t *testing.T) {
}
})
t.Run("Total", func(t *testing.T) {
resp, err := keeper.GasPriceAll(wctx, request(nil, 0, 0, true))
resp, err := k.GasPriceAll(wctx, request(nil, 0, 0, true))
require.NoError(t, err)
require.Equal(t, len(msgs), int(resp.Pagination.Total))
})
t.Run("InvalidRequest", func(t *testing.T) {
_, err := keeper.GasPriceAll(wctx, nil)
_, err := k.GasPriceAll(wctx, nil)
require.ErrorIs(t, err, status.Error(codes.InvalidArgument, "invalid request"))
})
}
33 changes: 17 additions & 16 deletions x/crosschain/keeper/grpc_query_last_block_height_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package keeper
package keeper_test

import (
"math"
Expand All @@ -7,15 +7,16 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/stretchr/testify/require"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
"github.com/zeta-chain/zetacore/x/crosschain/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func TestLastBlockHeightQuerySingle(t *testing.T) {
keeper, ctx := setupKeeper(t)
k, ctx, _, _ := keepertest.CrosschainKeeper(t)
wctx := sdk.WrapSDKContext(ctx)
msgs := createNLastBlockHeight(keeper, ctx, 2)
msgs := createNLastBlockHeight(k, ctx, 2)
for _, tc := range []struct {
desc string
request *types.QueryGetLastBlockHeightRequest
Expand Down Expand Up @@ -44,7 +45,7 @@ func TestLastBlockHeightQuerySingle(t *testing.T) {
} {
tc := tc
t.Run(tc.desc, func(t *testing.T) {
response, err := keeper.LastBlockHeight(wctx, tc.request)
response, err := k.LastBlockHeight(wctx, tc.request)
if tc.err != nil {
require.ErrorIs(t, err, tc.err)
} else {
Expand All @@ -56,30 +57,30 @@ func TestLastBlockHeightQuerySingle(t *testing.T) {

func TestLastBlockHeightLimits(t *testing.T) {
t.Run("should err if last send height is max int", func(t *testing.T) {
keeper, ctx := setupKeeper(t)
k, ctx, _, _ := keepertest.CrosschainKeeper(t)
wctx := sdk.WrapSDKContext(ctx)
keeper.SetLastBlockHeight(ctx, types.LastBlockHeight{
k.SetLastBlockHeight(ctx, types.LastBlockHeight{
Index: "index",
LastSendHeight: math.MaxInt64,
})

res, err := keeper.LastBlockHeight(wctx, &types.QueryGetLastBlockHeightRequest{
res, err := k.LastBlockHeight(wctx, &types.QueryGetLastBlockHeightRequest{
Index: "index",
})
require.Nil(t, res)
require.Error(t, err)
})

t.Run("should err if last receive height is max int", func(t *testing.T) {
keeper, ctx := setupKeeper(t)
k, ctx, _, _ := keepertest.CrosschainKeeper(t)
wctx := sdk.WrapSDKContext(ctx)
keeper.SetLastBlockHeight(ctx, types.LastBlockHeight{
k.SetLastBlockHeight(ctx, types.LastBlockHeight{
Index: "index",
LastSendHeight: 10,
LastReceiveHeight: math.MaxInt64,
})

res, err := keeper.LastBlockHeight(wctx, &types.QueryGetLastBlockHeightRequest{
res, err := k.LastBlockHeight(wctx, &types.QueryGetLastBlockHeightRequest{
Index: "index",
})
require.Nil(t, res)
Expand All @@ -88,9 +89,9 @@ func TestLastBlockHeightLimits(t *testing.T) {
}

func TestLastBlockHeightQueryPaginated(t *testing.T) {
keeper, ctx := setupKeeper(t)
k, ctx, _, _ := keepertest.CrosschainKeeper(t)
wctx := sdk.WrapSDKContext(ctx)
msgs := createNLastBlockHeight(keeper, ctx, 5)
msgs := createNLastBlockHeight(k, ctx, 5)

request := func(next []byte, offset, limit uint64, total bool) *types.QueryAllLastBlockHeightRequest {
return &types.QueryAllLastBlockHeightRequest{
Expand All @@ -105,7 +106,7 @@ func TestLastBlockHeightQueryPaginated(t *testing.T) {
t.Run("ByOffset", func(t *testing.T) {
step := 2
for i := 0; i < len(msgs); i += step {
resp, err := keeper.LastBlockHeightAll(wctx, request(nil, uint64(i), uint64(step), false))
resp, err := k.LastBlockHeightAll(wctx, request(nil, uint64(i), uint64(step), false))
require.NoError(t, err)
for j := i; j < len(msgs) && j < i+step; j++ {
require.Equal(t, &msgs[j], resp.LastBlockHeight[j-i])
Expand All @@ -116,7 +117,7 @@ func TestLastBlockHeightQueryPaginated(t *testing.T) {
step := 2
var next []byte
for i := 0; i < len(msgs); i += step {
resp, err := keeper.LastBlockHeightAll(wctx, request(next, 0, uint64(step), false))
resp, err := k.LastBlockHeightAll(wctx, request(next, 0, uint64(step), false))
require.NoError(t, err)
for j := i; j < len(msgs) && j < i+step; j++ {
require.Equal(t, &msgs[j], resp.LastBlockHeight[j-i])
Expand All @@ -125,12 +126,12 @@ func TestLastBlockHeightQueryPaginated(t *testing.T) {
}
})
t.Run("Total", func(t *testing.T) {
resp, err := keeper.LastBlockHeightAll(wctx, request(nil, 0, 0, true))
resp, err := k.LastBlockHeightAll(wctx, request(nil, 0, 0, true))
require.NoError(t, err)
require.Equal(t, len(msgs), int(resp.Pagination.Total))
})
t.Run("InvalidRequest", func(t *testing.T) {
_, err := keeper.LastBlockHeightAll(wctx, nil)
_, err := k.LastBlockHeightAll(wctx, nil)
require.ErrorIs(t, err, status.Error(codes.InvalidArgument, "invalid request"))
})
}
65 changes: 0 additions & 65 deletions x/crosschain/keeper/keeper_test.go

This file was deleted.

26 changes: 14 additions & 12 deletions x/crosschain/keeper/last_block_height_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package keeper
package keeper_test

import (
"fmt"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
"github.com/zeta-chain/zetacore/x/crosschain/keeper"
"github.com/zeta-chain/zetacore/x/crosschain/types"
)

func createNLastBlockHeight(keeper *Keeper, ctx sdk.Context, n int) []types.LastBlockHeight {
func createNLastBlockHeight(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.LastBlockHeight {
items := make([]types.LastBlockHeight, n)
for i := range items {
items[i].Creator = "any"
Expand All @@ -20,26 +22,26 @@ func createNLastBlockHeight(keeper *Keeper, ctx sdk.Context, n int) []types.Last
}

func TestLastBlockHeightGet(t *testing.T) {
keeper, ctx := setupKeeper(t)
items := createNLastBlockHeight(keeper, ctx, 10)
k, ctx, _, _ := keepertest.CrosschainKeeper(t)
items := createNLastBlockHeight(k, ctx, 10)
for _, item := range items {
rst, found := keeper.GetLastBlockHeight(ctx, item.Index)
rst, found := k.GetLastBlockHeight(ctx, item.Index)
require.True(t, found)
require.Equal(t, item, rst)
}
}
func TestLastBlockHeightRemove(t *testing.T) {
keeper, ctx := setupKeeper(t)
items := createNLastBlockHeight(keeper, ctx, 10)
k, ctx, _, _ := keepertest.CrosschainKeeper(t)
items := createNLastBlockHeight(k, ctx, 10)
for _, item := range items {
keeper.RemoveLastBlockHeight(ctx, item.Index)
_, found := keeper.GetLastBlockHeight(ctx, item.Index)
k.RemoveLastBlockHeight(ctx, item.Index)
_, found := k.GetLastBlockHeight(ctx, item.Index)
require.False(t, found)
}
}

func TestLastBlockHeightGetAll(t *testing.T) {
keeper, ctx := setupKeeper(t)
items := createNLastBlockHeight(keeper, ctx, 10)
require.Equal(t, items, keeper.GetAllLastBlockHeight(ctx))
k, ctx, _, _ := keepertest.CrosschainKeeper(t)
items := createNLastBlockHeight(k, ctx, 10)
require.Equal(t, items, k.GetAllLastBlockHeight(ctx))
}

0 comments on commit 47661d4

Please sign in to comment.