Skip to content

Commit

Permalink
chore: add unit test cases for payment module
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing committed Jul 27, 2023
1 parent 9cd5a4c commit e66a1f6
Show file tree
Hide file tree
Showing 16 changed files with 1,186 additions and 155 deletions.
52 changes: 52 additions & 0 deletions x/payment/keeper/auto_resume_record_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package keeper_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/bnb-chain/greenfield/testutil/sample"
"github.com/bnb-chain/greenfield/x/payment/types"
)

func TestAutoResumeRecord(t *testing.T) {
keeper, ctx, _ := makePaymentKeeper(t)

addr1 := sample.RandAccAddress()
record1 := &types.AutoResumeRecord{
Addr: addr1.String(),
Timestamp: 100,
}

addr2 := sample.RandAccAddress()
record2 := &types.AutoResumeRecord{
Addr: addr2.String(),
Timestamp: 200,
}

// set
keeper.SetAutoResumeRecord(ctx, record1)
keeper.SetAutoResumeRecord(ctx, record2)

// exits
// before the timestamp
exist := keeper.ExistsAutoResumeRecord(ctx, 90, addr1)
require.True(t, !exist)
exist = keeper.ExistsAutoResumeRecord(ctx, 101, addr1)
require.True(t, exist)

// at any time
exist = keeper.ExistsAutoResumeRecord(ctx, 0, addr1)
require.True(t, exist)
exist = keeper.ExistsAutoResumeRecord(ctx, 0, addr2)
require.True(t, exist)

// remove
keeper.RemoveAutoResumeRecord(ctx, record1.Timestamp, addr1)
keeper.RemoveAutoResumeRecord(ctx, record2.Timestamp, addr2)

exist = keeper.ExistsAutoResumeRecord(ctx, 0, addr1)
require.True(t, !exist)
exist = keeper.ExistsAutoResumeRecord(ctx, 0, addr2)
require.True(t, !exist)
}
22 changes: 0 additions & 22 deletions x/payment/keeper/auto_settle_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,6 @@ func (k Keeper) SetAutoSettleRecord(ctx sdk.Context, autoSettleRecord *types.Aut
), b)
}

// GetAutoSettleRecord returns a autoSettleRecord from its index
func (k Keeper) GetAutoSettleRecord(
ctx sdk.Context,
timestamp int64,
addr sdk.AccAddress,
) (*types.AutoSettleRecord, bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.AutoSettleRecordKeyPrefix)

b := store.Get(types.AutoSettleRecordKey(
timestamp,
addr,
))
if b == nil {
return nil, false
}

return &types.AutoSettleRecord{
Timestamp: timestamp,
Addr: addr.String(),
}, true
}

// RemoveAutoSettleRecord removes a autoSettleRecord from the store
func (k Keeper) RemoveAutoSettleRecord(
ctx sdk.Context,
Expand Down
42 changes: 42 additions & 0 deletions x/payment/keeper/auto_settle_record_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package keeper_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/bnb-chain/greenfield/testutil/sample"
"github.com/bnb-chain/greenfield/x/payment/types"
)

func TestAutoSettleRecord(t *testing.T) {
keeper, ctx, _ := makePaymentKeeper(t)

addr1 := sample.RandAccAddress()
record1 := &types.AutoSettleRecord{
Addr: addr1.String(),
Timestamp: 100,
}

addr2 := sample.RandAccAddress()
record2 := &types.AutoSettleRecord{
Addr: addr2.String(),
Timestamp: 200,
}

// set
keeper.SetAutoSettleRecord(ctx, record1)
keeper.SetAutoSettleRecord(ctx, record2)

// update to new time
keeper.UpdateAutoSettleRecord(ctx, addr1, record1.Timestamp, 110)

// update to remove
keeper.UpdateAutoSettleRecord(ctx, addr2, record2.Timestamp, 0)

// get all
records := keeper.GetAllAutoSettleRecord(ctx)
require.True(t, len(records) == 1)
require.True(t, records[0].Addr == addr1.String())
require.True(t, records[0].Timestamp == 110)
}
Loading

0 comments on commit e66a1f6

Please sign in to comment.