Skip to content

Commit

Permalink
test: add tests and simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hallazzang committed Oct 27, 2021
1 parent 98bc934 commit 3774e27
Showing 1 changed file with 84 additions and 20 deletions.
104 changes: 84 additions & 20 deletions x/farming/keeper/invariants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,8 @@ func (suite *KeeperTestSuite) TestPositiveStakingAmountInvariant() {
_, broken = farmingkeeper.PositiveStakingAmountInvariant(k)(ctx)
suite.Require().True(broken)

// Delete the zero-amount staking
k.DeleteStaking(ctx, denom1, suite.addrs[1])
_, broken = farmingkeeper.PositiveStakingAmountInvariant(k)(ctx)
suite.Require().False(broken)

// Negative-amount staking
k.SetStaking(ctx, denom1, suite.addrs[2], types.Staking{
k.SetStaking(ctx, denom1, suite.addrs[1], types.Staking{
Amount: sdk.NewInt(-1),
StartingEpoch: 1,
})
Expand All @@ -57,13 +52,8 @@ func (suite *KeeperTestSuite) TestPositiveQueuedStakingAmountInvariant() {
_, broken = farmingkeeper.PositiveQueuedStakingAmountInvariant(k)(ctx)
suite.Require().True(broken)

// Delete the zero-amount queued staking
k.DeleteQueuedStaking(ctx, denom1, suite.addrs[1])
_, broken = farmingkeeper.PositiveQueuedStakingAmountInvariant(k)(ctx)
suite.Require().False(broken)

// Negative-amount queued staking
k.SetQueuedStaking(ctx, denom1, suite.addrs[2], types.QueuedStaking{
k.SetQueuedStaking(ctx, denom1, suite.addrs[1], types.QueuedStaking{
Amount: sdk.NewInt(-1),
})
_, broken = farmingkeeper.PositiveQueuedStakingAmountInvariant(k)(ctx)
Expand Down Expand Up @@ -114,15 +104,15 @@ func (suite *KeeperTestSuite) TestStakingReservedAmountInvariant() {
// Send coins into the staking reserve acc.
// Staking amount in the store <= balance of staking reserve acc. This should be OK.
err := suite.app.BankKeeper.SendCoins(
ctx, suite.addrs[1], k.GetStakingReservePoolAcc(ctx), sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000)))
ctx, suite.addrs[1], k.GetStakingReservePoolAcc(ctx), sdk.NewCoins(sdk.NewInt64Coin(denom1, 1)))
suite.Require().NoError(err)
_, broken = farmingkeeper.StakingReservedAmountInvariant(k)(ctx)
suite.Require().False(broken)

// Send coins from staking reserve acc to another acc.
// Staking amount in the store < balance of staking reserve acc. This shouldn't be OK.
err = suite.app.BankKeeper.SendCoins(
ctx, k.GetStakingReservePoolAcc(ctx), suite.addrs[1], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000001)))
ctx, k.GetStakingReservePoolAcc(ctx), suite.addrs[1], sdk.NewCoins(sdk.NewInt64Coin(denom1, 2)))
suite.Require().NoError(err)
_, broken = farmingkeeper.StakingReservedAmountInvariant(k)(ctx)
suite.Require().True(broken)
Expand All @@ -141,14 +131,22 @@ func (suite *KeeperTestSuite) TestRemainingRewardsAmountInvariant() {
_, broken := farmingkeeper.RemainingRewardsAmountInvariant(k)(ctx)
suite.Require().False(broken)

// Withdrawable rewards amount in the store < balance of rewards reserve acc.
// Withdrawable rewards amount in the store > balance of rewards reserve acc.
// Should not be OK.
k.SetHistoricalRewards(ctx, denom1, 1, types.HistoricalRewards{
CumulativeUnitRewards: sdk.NewDecCoins(sdk.NewInt64DecCoin(denom3, 3)),
})
_, broken = farmingkeeper.RemainingRewardsAmountInvariant(k)(ctx)
suite.Require().True(broken)

// Withdrawable rewards amount in the store <= balance of rewards reserve acc.
// Should be OK.
k.SetHistoricalRewards(ctx, denom1, 1, types.HistoricalRewards{
CumulativeUnitRewards: sdk.NewDecCoins(sdk.NewInt64DecCoin(denom3, 1)),
})
_, broken = farmingkeeper.RemainingRewardsAmountInvariant(k)(ctx)
suite.Require().False(broken)

// Reset.
k.SetHistoricalRewards(ctx, denom1, 1, types.HistoricalRewards{
CumulativeUnitRewards: sdk.NewDecCoins(sdk.NewInt64DecCoin(denom3, 2)),
Expand All @@ -159,15 +157,15 @@ func (suite *KeeperTestSuite) TestRemainingRewardsAmountInvariant() {
// Send coins into the rewards reserve acc.
// Should be OK.
err := suite.app.BankKeeper.SendCoins(
ctx, suite.addrs[1], k.GetRewardsReservePoolAcc(ctx), sdk.NewCoins(sdk.NewInt64Coin(denom3, 1000000)))
ctx, suite.addrs[1], k.GetRewardsReservePoolAcc(ctx), sdk.NewCoins(sdk.NewInt64Coin(denom3, 1)))
suite.Require().NoError(err)
_, broken = farmingkeeper.RemainingRewardsAmountInvariant(k)(ctx)
suite.Require().False(broken)

// Send coins from the rewards reserve acc to another acc.
// Should not be OK.
err = suite.app.BankKeeper.SendCoins(
ctx, k.GetRewardsReservePoolAcc(ctx), suite.addrs[1], sdk.NewCoins(sdk.NewInt64Coin(denom3, 1000001)))
ctx, k.GetRewardsReservePoolAcc(ctx), suite.addrs[1], sdk.NewCoins(sdk.NewInt64Coin(denom3, 2)))
suite.Require().NoError(err)
_, broken = farmingkeeper.RemainingRewardsAmountInvariant(k)(ctx)
suite.Require().True(broken)
Expand All @@ -185,7 +183,7 @@ func (suite *KeeperTestSuite) TestNonNegativeOutstandingRewardsInvariant() {
// Zero-amount outstanding rewards
// It's acceptable, and for the initial epoch, the outstanding rewards is set to 0.
k.SetOutstandingRewards(ctx, denom2, types.OutstandingRewards{
Rewards: sdk.DecCoins{sdk.DecCoin{Denom: denom3, Amount: sdk.ZeroDec()}},
Rewards: sdk.DecCoins{},
})
_, broken = farmingkeeper.NonNegativeOutstandingRewardsInvariant(k)(ctx)
suite.Require().False(broken)
Expand All @@ -203,16 +201,82 @@ func (suite *KeeperTestSuite) TestNonNegativeOutstandingRewardsInvariant() {
}

func (suite *KeeperTestSuite) TestOutstandingRewardsAmountInvariant() {
k, ctx := suite.keeper, suite.ctx

suite.SetFixedAmountPlan(1, suite.addrs[4], map[string]string{denom1: "1"}, map[string]int64{denom3: 1000000})

suite.Stake(suite.addrs[0], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000)))
suite.AdvanceEpoch()
suite.AdvanceEpoch()

_, broken := farmingkeeper.OutstandingRewardsAmountInvariant(k)(ctx)
suite.Require().False(broken)

// Outstanding rewards amount > balance of rewards reserve acc.
// Should not be OK.
k.SetOutstandingRewards(ctx, denom1, types.OutstandingRewards{
Rewards: sdk.NewDecCoins(sdk.NewInt64DecCoin(denom3, 1000001)),
})
_, broken = farmingkeeper.OutstandingRewardsAmountInvariant(k)(ctx)
suite.Require().True(broken)

// Outstanding rewards amount <= balance of rewards reserve acc.
// Should be OK.
k.SetOutstandingRewards(ctx, denom1, types.OutstandingRewards{
Rewards: sdk.NewDecCoins(sdk.NewInt64DecCoin(denom3, 999999)),
})
_, broken = farmingkeeper.OutstandingRewardsAmountInvariant(k)(ctx)
suite.Require().False(broken)

// Reset.
k.SetOutstandingRewards(ctx, denom1, types.OutstandingRewards{
Rewards: sdk.NewDecCoins(sdk.NewInt64DecCoin(denom3, 1000000)),
})
_, broken = farmingkeeper.OutstandingRewardsAmountInvariant(k)(ctx)
suite.Require().False(broken)

// Send coins into the rewards reserve acc. Should be OK.
err := suite.app.BankKeeper.SendCoins(
ctx, suite.addrs[1], k.GetRewardsReservePoolAcc(ctx), sdk.NewCoins(sdk.NewInt64Coin(denom3, 1)))
suite.Require().NoError(err)
_, broken = farmingkeeper.OutstandingRewardsAmountInvariant(k)(ctx)
suite.Require().False(broken)

// Send coins from the rewards reserve acc to another acc. Should not be OK.
err = suite.app.BankKeeper.SendCoins(
ctx, k.GetRewardsReservePoolAcc(ctx), suite.addrs[1], sdk.NewCoins(sdk.NewInt64Coin(denom3, 2)))
suite.Require().NoError(err)
_, broken = farmingkeeper.OutstandingRewardsAmountInvariant(k)(ctx)
suite.Require().True(broken)
}

func (suite *KeeperTestSuite) TestNonNegativeHistoricalRewardsInvariant() {
k, ctx := suite.keeper, suite.ctx

// This is normal.
k.SetHistoricalRewards(ctx, denom1, 1, types.HistoricalRewards{
CumulativeUnitRewards: sdk.NewDecCoins(sdk.NewInt64DecCoin(denom3, 1000000)),
})
_, broken := farmingkeeper.NonNegativeHistoricalRewardsInvariant(k)(ctx)
suite.Require().False(broken)

// Zero-amount historical rewards
k.SetHistoricalRewards(ctx, denom2, 1, types.HistoricalRewards{
CumulativeUnitRewards: sdk.DecCoins{},
})
_, broken = farmingkeeper.NonNegativeHistoricalRewardsInvariant(k)(ctx)
suite.Require().False(broken)

// Negative-amount historical rewards
k.SetHistoricalRewards(ctx, denom2, 1, types.HistoricalRewards{
CumulativeUnitRewards: sdk.DecCoins{sdk.DecCoin{Denom: denom3, Amount: sdk.NewDec(-1)}},
})
_, broken = farmingkeeper.NonNegativeHistoricalRewardsInvariant(k)(ctx)
suite.Require().True(broken)
}

func (suite *KeeperTestSuite) TestPositiveTotalStakingsAmountInvariant() {

}

func (suite *KeeperTestSuite) TestPlanTerminationStatusInvariant() {

}

0 comments on commit 3774e27

Please sign in to comment.