Skip to content

Commit

Permalink
test(commitment): increase coverage from 40 to 76% (#872)
Browse files Browse the repository at this point in the history
* test(commitment): increase coverage from 40 to 57%

* test(commitments): increased to 73%

* test(amm): increased test coverage to 76%

---------

Co-authored-by: Amit Yadav <amy29981@gmail.com>
  • Loading branch information
cosmic-vagabond and amityadav0 authored Oct 24, 2024
1 parent eb60389 commit 65af35b
Show file tree
Hide file tree
Showing 13 changed files with 1,283 additions and 11 deletions.
38 changes: 29 additions & 9 deletions x/commitment/keeper/commitments.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ func (k Keeper) SetCommitments(ctx sdk.Context, commitments types.Commitments) {
store.Set(key, b)
}

// SetLegacyCommitments set a specific commitments in the store from its index
func (k Keeper) SetLegacyCommitments(ctx sdk.Context, commitments types.Commitments) {
if !k.HasLegacyCommitments(ctx, commitments.Creator) {
params := k.GetParams(ctx)
params.NumberOfCommitments++
k.SetParams(ctx, params)
}
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.LegacyKeyPrefix(types.LegacyCommitmentsKeyPrefix))
key := types.LegacyCommitmentsKey(commitments.Creator)
b := k.cdc.MustMarshal(&commitments)
store.Set(key, b)
}

// GetAllCommitments returns all commitments
func (k Keeper) GetAllCommitments(ctx sdk.Context) (list []*types.Commitments) {
store := ctx.KVStore(k.storeKey)
Expand All @@ -37,19 +50,21 @@ func (k Keeper) GetAllCommitments(ctx sdk.Context) (list []*types.Commitments) {
}

// remove after migration
func (k Keeper) GetAllLegacyCommitments(ctx sdk.Context) (list []*types.Commitments) {
func (k Keeper) GetAllLegacyCommitments(ctx sdk.Context) []*types.Commitments {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.LegacyKeyPrefix(types.LegacyCommitmentsKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})

defer iterator.Close()

list := []*types.Commitments{}

for ; iterator.Valid(); iterator.Next() {
var val types.Commitments
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, &val)
}

return
return list
}

// GetCommitments returns a commitments from its index
Expand Down Expand Up @@ -159,7 +174,7 @@ func (k Keeper) BurnEdenBoost(ctx sdk.Context, creator sdk.AccAddress, denom str
}
err := commitments.SubClaimed(sdk.NewCoin(denom, claimedRemovalAmount))
if err != nil {
return err
return err // never happens
}

amount = amount.Sub(claimedRemovalAmount)
Expand All @@ -175,9 +190,11 @@ func (k Keeper) BurnEdenBoost(ctx sdk.Context, creator sdk.AccAddress, denom str
return nil
}

err = k.hooks.BeforeEdenBCommitChange(ctx, creator)
if err != nil {
return err
if k.hooks != nil {
err = k.hooks.BeforeEdenBCommitChange(ctx, creator)
if err != nil {
return err
}
}

// Subtract the amount from the committed balance
Expand All @@ -188,9 +205,12 @@ func (k Keeper) BurnEdenBoost(ctx sdk.Context, creator sdk.AccAddress, denom str

k.SetCommitments(ctx, commitments)

err = k.hooks.CommitmentChanged(ctx, creator, sdk.Coins{sdk.NewCoin(denom, amount)})
if err != nil {
return err
if k.hooks != nil {
err = k.hooks.CommitmentChanged(ctx, creator, sdk.Coins{sdk.NewCoin(denom, amount)})
if err != nil {
return err
}
}

return nil
}
211 changes: 211 additions & 0 deletions x/commitment/keeper/commitments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper_test
import (
"testing"

"cosmossdk.io/math"
"github.com/cometbft/cometbft/crypto/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
keepertest "github.com/elys-network/elys/testutil/keeper"
Expand Down Expand Up @@ -32,3 +33,213 @@ func TestKeeper_SetGetRemoveCommitments(t *testing.T) {
commitments = keeper.GetCommitments(ctx, addr)
assert.True(t, commitments.IsEmpty())
}

// TestKeeper_GetAllCommitments tests the GetAllCommitments function
func TestKeeper_GetAllCommitments(t *testing.T) {
keeper, ctx := keepertest.CommitmentKeeper(t)

addr1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
commitments := []*types.Commitments{
{
Creator: addr1.String(),
},
}

// Set the commitments
for _, commitment := range commitments {
keeper.SetCommitments(ctx, *commitment)
}

// Test GetAllCommitments
retrievedCommitments := keeper.GetAllCommitments(ctx)
assert.Equal(t, commitments, retrievedCommitments)
}

// TestKeeper_GetAllLegacyCommitments tests the GetAllLegacyCommitments function
func TestKeeper_GetAllLegacyCommitments(t *testing.T) {
keeper, ctx := keepertest.CommitmentKeeper(t)

addr1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
commitments := []*types.Commitments{
{
Creator: addr1.String(),
},
}
// Set the commitments
for _, commitment := range commitments {
keeper.SetLegacyCommitments(ctx, *commitment)
}

// Test GetAllLegacyCommitments
retrievedCommitments := keeper.GetAllLegacyCommitments(ctx)
assert.Equal(t, commitments, retrievedCommitments)
}

// TestKeeper_DeleteLegacyCommitments tests the DeleteLegacyCommitments function
func TestKeeper_DeleteLegacyCommitments(t *testing.T) {
keeper, ctx := keepertest.CommitmentKeeper(t)

addr := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
commitments := types.Commitments{
Creator: addr.String(),
}

// Set the commitments
keeper.SetLegacyCommitments(ctx, commitments)

// Test DeleteLegacyCommitments
keeper.DeleteLegacyCommitments(ctx, addr.String())

// Test that commitments are removed
found := keeper.HasLegacyCommitments(ctx, addr.String())
assert.False(t, found)
}

// TestKeeper_IterateCommitments tests the IterateCommitments function
func TestKeeper_IterateCommitments(t *testing.T) {
keeper, ctx := keepertest.CommitmentKeeper(t)

addr1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
commitments := []*types.Commitments{
{
Creator: addr1.String(),
},
}

// Set the commitments
for _, commitment := range commitments {
keeper.SetCommitments(ctx, *commitment)
}

// Test IterateCommitments
var retrievedCommitments []*types.Commitments
keeper.IterateCommitments(ctx, func(commitment types.Commitments) bool {
retrievedCommitments = append(retrievedCommitments, &commitment)
return false
})
assert.Equal(t, commitments, retrievedCommitments)
}

// TestKeeper_IterateCommitments tests the IterateCommitments function with handlerFn returning true
func TestKeeper_IterateCommitmentsWithHandlerFn(t *testing.T) {
keeper, ctx := keepertest.CommitmentKeeper(t)

addr1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
commitments := []*types.Commitments{
{
Creator: addr1.String(),
},
}

// Set the commitments
for _, commitment := range commitments {
keeper.SetCommitments(ctx, *commitment)
}

// Test IterateCommitments
var retrievedCommitments []*types.Commitments
keeper.IterateCommitments(ctx, func(commitment types.Commitments) bool {
retrievedCommitments = append(retrievedCommitments, &commitment)
return true
})
assert.Equal(t, commitments[:1], retrievedCommitments)
}

// TestKeeper_TotalNumberOfCommitments tests the TotalNumberOfCommitments function
func TestKeeper_TotalNumberOfCommitments(t *testing.T) {
keeper, ctx := keepertest.CommitmentKeeper(t)

addr1 := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
commitments := []*types.Commitments{
{
Creator: addr1.String(),
},
}

// Set the commitments
for _, commitment := range commitments {
keeper.SetCommitments(ctx, *commitment)
}

// Test TotalNumberOfCommitments
total := keeper.TotalNumberOfCommitments(ctx)
assert.Equal(t, len(commitments), int(total))
}

// TestKeeper_BurnEdenBoost tests the BurnEdenBoost function
func TestKeeper_BurnEdenBoost(t *testing.T) {
// define a test matrix that will cover all the use cases
tests := []struct {
name string
claimedAmount math.Int
committedTokens math.Int
deductAmount math.Int
expectedError bool
}{
{
name: "deduct amount is zero",
claimedAmount: sdk.NewInt(100),
committedTokens: sdk.NewInt(100),
deductAmount: sdk.NewInt(0),
expectedError: false,
},
{
name: "deduct amount is greater than claimed amount",
claimedAmount: sdk.NewInt(100),
committedTokens: sdk.NewInt(100),
deductAmount: sdk.NewInt(200),
expectedError: false,
},
{
name: "deduct amount is greater than claimed amount with no committed tokens",
claimedAmount: sdk.NewInt(100),
committedTokens: sdk.NewInt(0),
deductAmount: sdk.NewInt(200),
expectedError: false,
},
{
name: "deduct amount is less than claimed amount",
claimedAmount: sdk.NewInt(100),
committedTokens: sdk.NewInt(100),
deductAmount: sdk.NewInt(50),
expectedError: false,
},
{
name: "deduct amount is equal to claimed amount",
claimedAmount: sdk.NewInt(100),
committedTokens: sdk.NewInt(100),
deductAmount: sdk.NewInt(100),
expectedError: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
keeper, ctx := keepertest.CommitmentKeeper(t)

addr := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
commitments := types.Commitments{
Creator: addr.String(),
}

// Set the commitments
keeper.SetCommitments(ctx, commitments)

// Set the claimed amount
commitments.AddClaimed(sdk.NewCoin("denom", tt.claimedAmount))
keeper.SetCommitments(ctx, commitments)

// Add committed amount
commitments.AddCommittedTokens("denom", tt.committedTokens, 0)
keeper.SetCommitments(ctx, commitments)

// Test BurnEdenBoost
err := keeper.BurnEdenBoost(ctx, addr, "denom", tt.deductAmount)
if tt.expectedError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
79 changes: 79 additions & 0 deletions x/commitment/keeper/deposit_liquid_tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,85 @@ import (
"github.com/stretchr/testify/require"
)

// TestDepositLiquidTokensWithNoEntry tests the deposit of liquid tokens with no assetprofile entry
func TestDepositLiquidTokensWithNoEntry(t *testing.T) {
app := simapp.InitElysTestApp(true)

ctx := app.BaseApp.NewContext(false, tmproto.Header{})
// Create a test context and keeper
keeper := app.CommitmentKeeper

addr := simapp.AddTestAddrs(app, ctx, 1, sdk.NewInt(1000000))

// Mint 100ueden
edenToken := sdk.NewCoins(sdk.NewCoin(ptypes.Eden, sdk.NewInt(100)))

err := app.BankKeeper.MintCoins(ctx, types.ModuleName, edenToken)
require.NoError(t, err)
err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, addr[0], edenToken)
require.NoError(t, err)

creator := addr[0]

// Set up the commitments for the creator
commitments := types.Commitments{
Creator: creator.String(),
CommittedTokens: []*types.CommittedTokens{
{
Denom: ptypes.Eden,
Amount: sdk.NewInt(50),
},
},
Claimed: sdk.Coins{sdk.NewCoin(ptypes.Eden, sdk.NewInt(150))},
}
keeper.SetCommitments(ctx, commitments)

// Deposit liquid eden to become claimed state
err = keeper.DepositLiquidTokensClaimed(ctx, ptypes.Eden, sdk.NewInt(100), creator)
require.Error(t, err)
}

// TestDepositLiquidTokensWithEntryDisabled tests the deposit of liquid tokens with assetprofile entry disabled
func TestDepositLiquidTokensWithEntryDisabled(t *testing.T) {
app := simapp.InitElysTestApp(true)

ctx := app.BaseApp.NewContext(false, tmproto.Header{})
// Create a test context and keeper
keeper := app.CommitmentKeeper

addr := simapp.AddTestAddrs(app, ctx, 1, sdk.NewInt(1000000))

// Mint 100ueden
edenToken := sdk.NewCoins(sdk.NewCoin(ptypes.Eden, sdk.NewInt(100)))

err := app.BankKeeper.MintCoins(ctx, types.ModuleName, edenToken)
require.NoError(t, err)
err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, addr[0], edenToken)
require.NoError(t, err)

creator := addr[0]

// Set assetprofile entry for denom
app.AssetprofileKeeper.SetEntry(ctx, assetprofiletypes.Entry{BaseDenom: ptypes.Eden, CommitEnabled: false})

// Set up the commitments for the creator
commitments := types.Commitments{
Creator: creator.String(),
CommittedTokens: []*types.CommittedTokens{
{
Denom: ptypes.Eden,
Amount: sdk.NewInt(50),
},
},
Claimed: sdk.Coins{sdk.NewCoin(ptypes.Eden, sdk.NewInt(150))},
}
keeper.SetCommitments(ctx, commitments)

// Deposit liquid eden to become claimed state
err = keeper.DepositLiquidTokensClaimed(ctx, ptypes.Eden, sdk.NewInt(100), creator)
require.Error(t, err)
}

func TestDepositLiquidTokens(t *testing.T) {
app := simapp.InitElysTestApp(true)

Expand Down
Loading

0 comments on commit 65af35b

Please sign in to comment.