github.com/shoshinnikita/budget-manager@v0.7.1-0.20220131195411-8c46ff1c6778/internal/web/pages/statistics/spent_by_day_test.go (about)

     1  package statistics
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/ShoshinNikita/budget-manager/internal/db"
    10  	"github.com/ShoshinNikita/budget-manager/internal/pkg/money"
    11  )
    12  
    13  func TestCalculateSpentByDay(t *testing.T) {
    14  	t.Parallel()
    15  
    16  	date := func(y int, m time.Month, d int) time.Time {
    17  		return time.Date(y, m, d, 0, 0, 0, 0, time.UTC)
    18  	}
    19  
    20  	tests := []struct {
    21  		spends    []db.Spend
    22  		startDate time.Time
    23  		endDate   time.Time
    24  		//
    25  		want SpentByDayDataset
    26  	}{
    27  		{
    28  			spends: []db.Spend{
    29  				{Year: 2020, Month: 10, Day: 25, Cost: money.FromInt(100)},
    30  				{Year: 2020, Month: 10, Day: 25, Cost: money.FromInt(150)},
    31  				{Year: 2020, Month: 10, Day: 27, Cost: money.FromInt(1)},
    32  				{Year: 2020, Month: 10, Day: 30, Cost: money.FromInt(33)},
    33  			},
    34  			startDate: date(2020, 10, 25),
    35  			endDate:   date(2020, 10, 30),
    36  			//
    37  			want: SpentByDayDataset{
    38  				{Year: 2020, Month: 10, Day: 25, Spent: money.FromInt(250)},
    39  				{Year: 2020, Month: 10, Day: 26, Spent: 0},
    40  				{Year: 2020, Month: 10, Day: 27, Spent: money.FromInt(1)},
    41  				{Year: 2020, Month: 10, Day: 28, Spent: 0},
    42  				{Year: 2020, Month: 10, Day: 29, Spent: 0},
    43  				{Year: 2020, Month: 10, Day: 30, Spent: money.FromInt(33)},
    44  			},
    45  		},
    46  		{
    47  			spends: []db.Spend{
    48  				{Year: 2020, Month: 10, Day: 30, Cost: money.FromInt(33)},
    49  				{Year: 2020, Month: 11, Day: 3, Cost: money.FromInt(55)},
    50  			},
    51  			startDate: date(2020, 10, 28),
    52  			endDate:   date(2020, 11, 4),
    53  			//
    54  			want: SpentByDayDataset{
    55  				{Year: 2020, Month: 10, Day: 28, Spent: 0},
    56  				{Year: 2020, Month: 10, Day: 29, Spent: 0},
    57  				{Year: 2020, Month: 10, Day: 30, Spent: money.FromInt(33)},
    58  				{Year: 2020, Month: 10, Day: 31, Spent: 0},
    59  				{Year: 2020, Month: 11, Day: 1, Spent: 0},
    60  				{Year: 2020, Month: 11, Day: 2, Spent: 0},
    61  				{Year: 2020, Month: 11, Day: 3, Spent: money.FromInt(55)},
    62  				{Year: 2020, Month: 11, Day: 4, Spent: 0},
    63  			},
    64  		},
    65  		{
    66  			spends: []db.Spend{
    67  				{Year: 2020, Month: 10, Day: 30, Cost: money.FromInt(33)},
    68  				{Year: 2020, Month: 11, Day: 3, Cost: money.FromInt(55)},
    69  			},
    70  			//
    71  			want: SpentByDayDataset{
    72  				{Year: 2020, Month: 10, Day: 30, Spent: money.FromInt(33)},
    73  				{Year: 2020, Month: 10, Day: 31},
    74  				{Year: 2020, Month: 11, Day: 1},
    75  				{Year: 2020, Month: 11, Day: 2},
    76  				{Year: 2020, Month: 11, Day: 3, Spent: money.FromInt(55)},
    77  			},
    78  		},
    79  		{
    80  			spends: []db.Spend{
    81  				{Year: 2019, Month: 12, Day: 30, Cost: money.FromInt(1000)},
    82  				{Year: 2020, Month: 1, Day: 3, Cost: money.FromInt(99)},
    83  			},
    84  			//
    85  			want: SpentByDayDataset{
    86  				{Year: 2019, Month: 12, Day: 30, Spent: money.FromInt(1000)},
    87  				{Year: 2019, Month: 12, Day: 31},
    88  				{Year: 2020, Month: 1, Day: 1},
    89  				{Year: 2020, Month: 1, Day: 2},
    90  				{Year: 2020, Month: 1, Day: 3, Spent: money.FromInt(99)},
    91  			},
    92  		},
    93  	}
    94  	for _, tt := range tests {
    95  		tt := tt
    96  		t.Run("", func(t *testing.T) {
    97  			got := CalculateSpentByDay(tt.spends, tt.startDate, tt.endDate)
    98  			require.Equal(t, tt.want, got)
    99  		})
   100  	}
   101  }