github.com/shoshinnikita/budget-manager@v0.7.1-0.20220131195411-8c46ff1c6778/internal/db/base/month_test.go (about)

     1  package base
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/ShoshinNikita/budget-manager/internal/pkg/money"
     9  )
    10  
    11  func TestRecomputeMonth(t *testing.T) {
    12  	t.Parallel()
    13  
    14  	toMoney := func(m int64) money.Money { //nolint:gocritic
    15  		return money.FromInt(m)
    16  	}
    17  
    18  	tests := []struct {
    19  		desc  string
    20  		input Month
    21  		want  Month
    22  	}{
    23  		{
    24  			desc: "usual month",
    25  			input: Month{
    26  				Incomes: []Income{
    27  					{Income: toMoney(700)},
    28  					{Income: toMoney(150)},
    29  					{Income: toMoney(150)},
    30  				},
    31  				MonthlyPayments: []MonthlyPayment{
    32  					{Cost: toMoney(175)},
    33  					{Cost: toMoney(25)},
    34  				},
    35  				Days: []Day{
    36  					{},
    37  					{
    38  						Spends: []Spend{{Cost: toMoney(99)}, {Cost: toMoney(1)}},
    39  					},
    40  					{
    41  						Spends: []Spend{{Cost: toMoney(12)}},
    42  					},
    43  					{},
    44  				},
    45  			},
    46  			want: Month{
    47  				MonthOverview: MonthOverview{
    48  					DailyBudget: toMoney(200),
    49  					TotalIncome: toMoney(1000),
    50  					TotalSpend:  toMoney(-312),
    51  					Result:      toMoney(688),
    52  				},
    53  				Incomes: []Income{
    54  					{Income: toMoney(700)},
    55  					{Income: toMoney(150)},
    56  					{Income: toMoney(150)},
    57  				},
    58  				MonthlyPayments: []MonthlyPayment{
    59  					{Cost: toMoney(175)},
    60  					{Cost: toMoney(25)},
    61  				},
    62  				Days: []Day{
    63  					{
    64  						Saldo: toMoney(200),
    65  					},
    66  					{
    67  						Spends: []Spend{{Cost: toMoney(99)}, {Cost: toMoney(1)}},
    68  						Saldo:  toMoney(300),
    69  					},
    70  					{
    71  						Spends: []Spend{{Cost: toMoney(12)}},
    72  						Saldo:  toMoney(488),
    73  					},
    74  					{
    75  						Saldo: toMoney(688),
    76  					},
    77  				},
    78  			},
    79  		},
    80  		{
    81  			desc: "negative spends (cashback)",
    82  			input: Month{
    83  				Incomes:         []Income{{Income: toMoney(1000)}},
    84  				MonthlyPayments: nil,
    85  				//
    86  				Days: []Day{
    87  					{},
    88  					{
    89  						Spends: []Spend{{Cost: toMoney(99)}, {Cost: toMoney(-99)}},
    90  					},
    91  					{
    92  						Spends: []Spend{{Cost: toMoney(120)}},
    93  					},
    94  					{},
    95  				},
    96  			},
    97  			want: Month{
    98  				MonthOverview: MonthOverview{
    99  					DailyBudget: toMoney(250),
   100  					TotalIncome: toMoney(1000),
   101  					TotalSpend:  toMoney(-120),
   102  					Result:      toMoney(880),
   103  				},
   104  				Incomes:         []Income{{Income: toMoney(1000)}},
   105  				MonthlyPayments: nil,
   106  				Days: []Day{
   107  					{
   108  						Saldo: toMoney(250),
   109  					},
   110  					{
   111  						Spends: []Spend{{Cost: toMoney(99)}, {Cost: toMoney(-99)}},
   112  						Saldo:  toMoney(500),
   113  					},
   114  					{
   115  						Spends: []Spend{{Cost: toMoney(120)}},
   116  						Saldo:  toMoney(630),
   117  					},
   118  					{
   119  						Saldo: toMoney(880),
   120  					},
   121  				},
   122  			},
   123  		},
   124  	}
   125  	for _, tt := range tests {
   126  		tt := tt
   127  		t.Run(tt.desc, func(t *testing.T) {
   128  			got := recomputeMonth(tt.input)
   129  			require.Equal(t, tt.want, got)
   130  		})
   131  	}
   132  }