Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gartnera committed Sep 12, 2024
1 parent 7e3356a commit 2474213
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
7 changes: 5 additions & 2 deletions x/crosschain/keeper/grpc_query_cctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
// MaxLookbackNonce is the maximum number of nonces to look back to find missed pending cctxs
MaxLookbackNonce = 1000

defaultPageSize = 100
DefaultPageSize = 100
)

func (k Keeper) ZetaAccounting(
Expand All @@ -48,8 +48,11 @@ func (k Keeper) CctxAll(c context.Context, req *types.QueryAllCctxRequest) (*typ
store := ctx.KVStore(k.storeKey)
sendStore := prefix.NewStore(store, types.KeyPrefix(types.CCTXKey))

if req.Pagination == nil {
req.Pagination = &query.PageRequest{}
}
if req.Pagination.Limit == 0 {
req.Pagination.Limit = defaultPageSize
req.Pagination.Limit = DefaultPageSize
}

pageRes, err := query.Paginate(sendStore, req.Pagination, func(_ []byte, value []byte) error {
Expand Down
38 changes: 38 additions & 0 deletions x/crosschain/keeper/grpc_query_cctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/stretchr/testify/require"

keepertest "github.com/zeta-chain/node/testutil/keeper"
Expand Down Expand Up @@ -288,3 +289,40 @@ func TestKeeper_CctxByNonce(t *testing.T) {
require.Equal(t, res.CrossChainTx.CctxStatus.LastUpdateTimestamp, ctx.BlockTime().Unix())
})
}

func TestKeeper_CctxAll(t *testing.T) {
t.Run("empty request", func(t *testing.T) {
k, ctx, _, _ := keepertest.CrosschainKeeper(t)
_, err := k.CctxAll(ctx, &types.QueryAllCctxRequest{})
require.NoError(t, err)
})

t.Run("default page size", func(t *testing.T) {
k, ctx, _, zk := keepertest.CrosschainKeeper(t)
chainID := getValidEthChainID()
tss := sample.Tss()
zk.ObserverKeeper.SetTSS(ctx, tss)
_ = createCctxWithNonceRange(t, ctx, *k, 1000, 2000, chainID, tss, zk)

res, err := k.CctxAll(ctx, &types.QueryAllCctxRequest{})
require.NoError(t, err)
require.Len(t, res.CrossChainTx, keeper.DefaultPageSize)
})

t.Run("page size provided", func(t *testing.T) {
k, ctx, _, zk := keepertest.CrosschainKeeper(t)
chainID := getValidEthChainID()
tss := sample.Tss()
zk.ObserverKeeper.SetTSS(ctx, tss)
_ = createCctxWithNonceRange(t, ctx, *k, 1000, 2000, chainID, tss, zk)
testPageSize := 200

res, err := k.CctxAll(ctx, &types.QueryAllCctxRequest{
Pagination: &query.PageRequest{
Limit: uint64(testPageSize),
},
})
require.NoError(t, err)
require.Len(t, res.CrossChainTx, testPageSize)
})
}

0 comments on commit 2474213

Please sign in to comment.