Skip to content

Commit

Permalink
CNS-169: add tests for CreateSubscription, EpochStart
Browse files Browse the repository at this point in the history
  • Loading branch information
orenl-lava committed Feb 28, 2023
1 parent a6beced commit aa42352
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 0 deletions.
51 changes: 51 additions & 0 deletions x/subscription/keeper/epoch_start_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package keeper_test

import (
"testing"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/lavanet/lava/relayer/sigs"
testkeeper "github.com/lavanet/lava/testutil/keeper"
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types"
"github.com/lavanet/lava/x/subscription/types"
"github.com/stretchr/testify/require"
)

func TestSubscriptionExpire(t *testing.T) {
_, keepers, _ctx := testkeeper.InitAllKeepers(t)
ctx := sdk.UnwrapSDKContext(_ctx)

keeper := keepers.Subscription
bank := keepers.BankKeeper

_, account := sigs.GenerateFloatingKey()
coins := sdk.NewCoins(sdk.NewCoin(epochstoragetypes.TokenDenom, sdk.NewInt(10000)))
bank.SetBalance(ctx, account, coins)

sub := types.Subscription{
Creator: account.String(),
Consumer: account.String(),
PackageIndex: "testpkg",
}

// advance block to reach time > 0
_ctx = testkeeper.AdvanceBlock(_ctx, keepers, time.Minute)
ctx = sdk.UnwrapSDKContext(_ctx)

err := keeper.CreateSubscription(ctx, sub)
require.Nil(t, err)

sub, found := keeper.GetSubscription(ctx, account.String())
require.True(t, found)

// change expiration time to 1 second ago
sub.ExpiryTime = uint64(ctx.BlockTime().Add(-time.Second).UTC().Unix())
keeper.SetSubscription(ctx, sub)

// trigger EpochStart() processing
keeper.EpochStart(ctx)

sub, found = keeper.GetSubscription(ctx, account.String())
require.False(t, found)
}
118 changes: 118 additions & 0 deletions x/subscription/keeper/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/lavanet/lava/relayer/sigs"
keepertest "github.com/lavanet/lava/testutil/keeper"
"github.com/lavanet/lava/testutil/nullify"
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types"
"github.com/lavanet/lava/x/subscription/keeper"
"github.com/lavanet/lava/x/subscription/types"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -67,3 +68,120 @@ func TestSubscriptionGetAll(t *testing.T) {
nullify.Fill(keeper.GetAllSubscription(ctx)),
)
}

func TestCreateSubscription(t *testing. T) {
_, keepers, _ctx := keepertest.InitAllKeepers(t)
ctx := sdk.UnwrapSDKContext(_ctx)

keeper := keepers.Subscription
bank := keepers.BankKeeper

creators := []struct {
address string
amount int64
}{
{
address: "FILL",
amount: 100000,
},
{
address: "FILL",
amount: 1,
},
{
address: "invalid creator",
amount: 0,
},
}

for i := range creators {
if creators[i].address == "FILL" {
_, addr := sigs.GenerateFloatingKey()
creators[i].address = addr.String()
coins := sdk.NewCoins(sdk.NewCoin(
epochstoragetypes.TokenDenom,
sdk.NewInt(creators[i].amount),
))
bank.SetBalance(ctx, addr, coins)
}
}

consumers := make([]string, 4)
for i := range consumers {
_, addr := sigs.GenerateFloatingKey()
consumers[i] = addr.String()
}
consumers[3] = "invalid consumer"

template := []struct {
name string
index string
creator int
consumers []int
success bool
}{
{
name: "create subscriptions",
index: "testpkg",
creator: 0,
consumers: []int{0, 1},
success: true,
},
{
name: "invalid creator",
index: "testpkg",
creator: 2,
consumers: []int{2},
success: false,
},
{
name: "invalid consumer",
index: "testpkg",
creator: 0,
consumers: []int{3},
success: false,
},
{
name: "insufficient funds",
index: "testpkg",
creator: 1,
consumers: []int{2},
success: false,
},
{
name: "invalid package",
index: "",
creator: 0,
consumers: []int{2},
success: false,
},
{
name: "double subscription",
index: "testpkg",
creator: 0,
consumers: []int{0},
success: false,
},
}

for _, tt := range template {
for _, consumer := range tt.consumers {
t.Run(tt.name, func(t *testing.T) {
sub := types.Subscription{
Creator: creators[tt.creator].address,
Consumer: consumers[consumer],
PackageIndex: tt.index,
}

err := keeper.CreateSubscription(ctx, sub)
if tt.success {
require.Nil(t, err, tt.name)
_, found := keeper.GetSubscription(ctx, sub.Consumer)
require.True(t, found, tt.name)
} else {
require.NotNil(t, err, tt.name)
}
})
}
}
}

0 comments on commit aa42352

Please sign in to comment.