From c2fc7bc3f00ef59ae525bd5e8c4052d87f9830d2 Mon Sep 17 00:00:00 2001 From: omerlavanet Date: Tue, 26 Nov 2024 14:06:06 +0200 Subject: [PATCH] wip on tests --- x/dualstaking/keeper/delegate_credit_test.go | 45 ++++++++++++++++---- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/x/dualstaking/keeper/delegate_credit_test.go b/x/dualstaking/keeper/delegate_credit_test.go index 68153f8929..e8123c1dd9 100644 --- a/x/dualstaking/keeper/delegate_credit_test.go +++ b/x/dualstaking/keeper/delegate_credit_test.go @@ -27,21 +27,23 @@ func TestCalculateCredit(t *testing.T) { name string delegation types.Delegation expectedCredit sdk.Coin + currentTime time.Time expectedCreditTimestamp int64 }{ { - name: "initial delegation", + name: "initial delegation 10days ago with no credit", delegation: types.Delegation{ Amount: sdk.NewCoin(bondDenom, sdk.NewInt(1000)), Credit: sdk.NewCoin(bondDenom, sdk.ZeroInt()), - Timestamp: timeNow.Add(-time.Hour * 24 * 10).Unix(), + Timestamp: timeNow.Add(-time.Hour * 24 * 10).Unix(), // was done 10 days ago CreditTimestamp: 0, }, expectedCredit: sdk.NewCoin(bondDenom, sdk.NewInt(1000)), + currentTime: timeNow, expectedCreditTimestamp: timeNow.Add(-time.Hour * 24 * 10).Unix(), }, { - name: "delegation with existing credit", + name: "delegation with existing credit equal time increase", delegation: types.Delegation{ Amount: sdk.NewCoin(bondDenom, sdk.NewInt(2000)), Credit: sdk.NewCoin(bondDenom, sdk.NewInt(1000)), @@ -49,23 +51,50 @@ func TestCalculateCredit(t *testing.T) { CreditTimestamp: timeNow.Add(-time.Hour * 24 * 10).Unix(), }, expectedCredit: sdk.NewCoin(bondDenom, sdk.NewInt(1500)), - expectedCreditTimestamp: timeNow.Add(-time.Hour * 24 * 5).Unix(), + currentTime: timeNow, + expectedCreditTimestamp: timeNow.Add(-time.Hour * 24 * 10).Unix(), }, { - name: "delegation older than 30 days", + name: "delegation with existing credit equal time decrease", + delegation: types.Delegation{ + Amount: sdk.NewCoin(bondDenom, sdk.NewInt(1000)), + Credit: sdk.NewCoin(bondDenom, sdk.NewInt(2000)), + Timestamp: timeNow.Add(-time.Hour * 24 * 5).Unix(), + CreditTimestamp: timeNow.Add(-time.Hour * 24 * 10).Unix(), + }, + expectedCredit: sdk.NewCoin(bondDenom, sdk.NewInt(1500)), + currentTime: timeNow, + expectedCreditTimestamp: timeNow.Add(-time.Hour * 24 * 10).Unix(), + }, + { + name: "delegation older than 30 days no credit", delegation: types.Delegation{ Amount: sdk.NewCoin(bondDenom, sdk.NewInt(3000)), - Credit: sdk.NewCoin(bondDenom, sdk.NewInt(1500)), + Credit: sdk.NewCoin(bondDenom, sdk.ZeroInt()), Timestamp: timeNow.Add(-time.Hour * 24 * 40).Unix(), - CreditTimestamp: timeNow.Add(-time.Hour * 24 * 35).Unix(), + CreditTimestamp: 0, + }, + expectedCredit: sdk.NewCoin(bondDenom, sdk.NewInt(3000)), + currentTime: timeNow, + expectedCreditTimestamp: timeNow.Add(-time.Hour * 24 * 30).Unix(), + }, + { + name: "delegation older than 30 days with credit", + delegation: types.Delegation{ + Amount: sdk.NewCoin(bondDenom, sdk.NewInt(3000)), + Credit: sdk.NewCoin(bondDenom, sdk.NewInt(7000)), + Timestamp: timeNow.Add(-time.Hour * 24 * 40).Unix(), + CreditTimestamp: timeNow.Add(-time.Hour * 24 * 50).Unix(), }, expectedCredit: sdk.NewCoin(bondDenom, sdk.NewInt(3000)), - expectedCreditTimestamp: timeNow.Add(-time.Hour * 24 * 40).Unix(), + currentTime: timeNow, + expectedCreditTimestamp: timeNow.Add(-time.Hour * 24 * 30).Unix(), }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + ctx = ctx.WithBlockTime(tt.currentTime) credit, creditTimestamp := k.CalculateCredit(ctx, tt.delegation) require.Equal(t, tt.expectedCredit, credit) require.Equal(t, tt.expectedCreditTimestamp, creditTimestamp)