|
| 1 | +package models |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/stretchr/testify/assert" |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +func TestFundingSchedule_CalculateNextOccurrence(t *testing.T) { |
| 12 | + t.Run("simple", func(t *testing.T) { |
| 13 | + rule, err := NewRule("FREQ=DAILY") |
| 14 | + require.NoError(t, err, "must be able to create a rule") |
| 15 | + |
| 16 | + originalOccurrence := time.Now().Add(-1 * time.Minute) |
| 17 | + |
| 18 | + fundingSchedule := FundingSchedule{ |
| 19 | + AccountId: 1234, |
| 20 | + BankAccountId: 1234, |
| 21 | + Name: "Testing #123", |
| 22 | + Description: t.Name(), |
| 23 | + Rule: rule, |
| 24 | + LastOccurrence: nil, |
| 25 | + NextOccurrence: originalOccurrence, |
| 26 | + } |
| 27 | + |
| 28 | + assert.Nil(t, fundingSchedule.LastOccurrence, "last occurrence should still be nil") |
| 29 | + |
| 30 | + ok := fundingSchedule.CalculateNextOccurrence(context.Background(), time.Local) |
| 31 | + assert.True(t, ok, "should calculate next occurrence") |
| 32 | + assert.NotNil(t, fundingSchedule.LastOccurrence, "last occurrence should no longer be nil") |
| 33 | + assert.Equal(t, originalOccurrence.Unix(), fundingSchedule.LastOccurrence.Unix(), "last occurrence should match original") |
| 34 | + assert.Greater(t, fundingSchedule.NextOccurrence.Unix(), originalOccurrence.Unix(), "next occurrence should be in the future relative to the last occurrence") |
| 35 | + }) |
| 36 | + |
| 37 | + t.Run("would skip", func(t *testing.T) { |
| 38 | + rule, err := NewRule("FREQ=DAILY") |
| 39 | + require.NoError(t, err, "must be able to create a rule") |
| 40 | + |
| 41 | + originalOccurrence := time.Now().Add(1 * time.Minute) |
| 42 | + |
| 43 | + fundingSchedule := FundingSchedule{ |
| 44 | + AccountId: 1234, |
| 45 | + BankAccountId: 1234, |
| 46 | + Name: "Testing #123", |
| 47 | + Description: t.Name(), |
| 48 | + Rule: rule, |
| 49 | + LastOccurrence: nil, |
| 50 | + NextOccurrence: originalOccurrence, |
| 51 | + } |
| 52 | + |
| 53 | + assert.Nil(t, fundingSchedule.LastOccurrence, "last occurrence should still be nil") |
| 54 | + |
| 55 | + ok := fundingSchedule.CalculateNextOccurrence(context.Background(), time.Local) |
| 56 | + assert.False(t, ok, "next occurrence should not be calculated") |
| 57 | + assert.Equal(t, originalOccurrence.Unix(), fundingSchedule.NextOccurrence.Unix(), "next occurrence should not have changed") |
| 58 | + }) |
| 59 | +} |
0 commit comments