Skip to content

Commit

Permalink
Add tests for get bond factor
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Mar 27, 2024
1 parent d00930f commit 83666b3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
6 changes: 6 additions & 0 deletions testutil/keeper/emissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,9 @@ func GetEmissionsParamStoreMock(t testing.TB, keeper *keeper.Keeper) *emissionsm
require.True(t, ok)
return m
}

func GetEmissionsStakingMock(t testing.TB, keeper *keeper.Keeper) *emissionsmocks.EmissionStakingKeeper {
cbk, ok := keeper.GetStakingKeeper().(*emissionsmocks.EmissionStakingKeeper)
require.True(t, ok)
return cbk
}
60 changes: 60 additions & 0 deletions x/emissions/keeper/block_rewards_components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,63 @@ func TestKeeper_GetBlockRewardComponent(t *testing.T) {
require.NotEqual(t, sdk.ZeroDec(), durationFactor)
})
}

func TestKeeper_GetBondFactor(t *testing.T) {
t.Run("should return 0 if current bond ratio is 0", func(t *testing.T) {
k, ctx, _, _ := keepertest.EmissionsKeeper(t)

bondFactor := k.GetBondFactor(ctx, k.GetStakingKeeper())
require.Equal(t, sdk.ZeroDec(), bondFactor)
})

t.Run("should return max bond factor if bond factor exceeds max bond factor", func(t *testing.T) {
k, ctx, _, _ := keepertest.EmissionKeeperWithMockOptions(t, keepertest.EmissionMockOptions{
UseStakingMock: true,
})

params := emissionstypes.DefaultParams()
params.TargetBondRatio = "0.5"
params.MaxBondFactor = "1.1"
params.MinBondFactor = "0.9"
k.SetParams(ctx, params)

stakingMock := keepertest.GetEmissionsStakingMock(t, k)
stakingMock.On("BondedRatio", ctx).Return(sdk.MustNewDecFromStr("0.25"))
bondFactor := k.GetBondFactor(ctx, k.GetStakingKeeper())
require.Equal(t, sdk.MustNewDecFromStr(params.MaxBondFactor), bondFactor)
})

t.Run("should return min bond factor if bond factor below min bond factor", func(t *testing.T) {
k, ctx, _, _ := keepertest.EmissionKeeperWithMockOptions(t, keepertest.EmissionMockOptions{
UseStakingMock: true,
})

params := emissionstypes.DefaultParams()
params.TargetBondRatio = "0.5"
params.MaxBondFactor = "1.1"
params.MinBondFactor = "0.9"
k.SetParams(ctx, params)

stakingMock := keepertest.GetEmissionsStakingMock(t, k)
stakingMock.On("BondedRatio", ctx).Return(sdk.MustNewDecFromStr("0.75"))
bondFactor := k.GetBondFactor(ctx, k.GetStakingKeeper())
require.Equal(t, sdk.MustNewDecFromStr(params.MinBondFactor), bondFactor)
})

t.Run("should return calculated bond factor if bond factor in range", func(t *testing.T) {
k, ctx, _, _ := keepertest.EmissionKeeperWithMockOptions(t, keepertest.EmissionMockOptions{
UseStakingMock: true,
})

params := emissionstypes.DefaultParams()
params.TargetBondRatio = "0.5"
params.MaxBondFactor = "1.1"
params.MinBondFactor = "0.9"
k.SetParams(ctx, params)

stakingMock := keepertest.GetEmissionsStakingMock(t, k)
stakingMock.On("BondedRatio", ctx).Return(sdk.MustNewDecFromStr("0.5"))
bondFactor := k.GetBondFactor(ctx, k.GetStakingKeeper())
require.Equal(t, sdk.OneDec(), bondFactor)
})
}

0 comments on commit 83666b3

Please sign in to comment.