github.com/shoshinnikita/budget-manager@v0.7.1-0.20220131195411-8c46ff1c6778/internal/app/app_test.go (about)

     1  package app
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestCalculateTimeToNextMonthInit(t *testing.T) {
    11  	t.Parallel()
    12  
    13  	tests := []struct {
    14  		now  time.Time
    15  		want time.Duration
    16  	}{
    17  		{
    18  			now:  time.Date(2021, time.January, 1, 0, 0, 0, 0, time.UTC),
    19  			want: 31 * 24 * time.Hour,
    20  		},
    21  		{
    22  			now:  time.Date(2021, time.January, 31, 22, 50, 5, 0, time.UTC),
    23  			want: time.Hour + 9*time.Minute + 55*time.Second,
    24  		},
    25  		{
    26  			now:  time.Date(2021, time.January, 31, 22, 50, 5, 0, time.UTC),
    27  			want: time.Hour + 9*time.Minute + 55*time.Second,
    28  		},
    29  		{
    30  			now:  time.Date(2021, time.April, 1, 0, 0, 45, 0, time.UTC),
    31  			want: 29*24*time.Hour + 23*time.Hour + 59*time.Minute + 15*time.Second,
    32  		},
    33  		{
    34  			now:  time.Date(2021, time.April, 30, 23, 59, 59, 0, time.UTC),
    35  			want: time.Second,
    36  		},
    37  	}
    38  	for _, tt := range tests {
    39  		tt := tt
    40  		t.Run("", func(t *testing.T) {
    41  			got := calculateTimeToNextMonthInit(tt.now)
    42  			require.Equal(t, tt.want, got)
    43  			require.Equal(t, tt.now.Month()+1, tt.now.Add(got).Month())
    44  		})
    45  	}
    46  }