From a2280e50ecfbb64f9fd587b5eb06487b435c78e2 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Thu, 7 Apr 2022 07:53:25 +0000 Subject: [PATCH] Fix test without available property --- server/budgeting.go | 29 +++++++++++++++-------------- server/main_test.go | 4 +++- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/server/budgeting.go b/server/budgeting.go index 5960a87..9e0a7d1 100644 --- a/server/budgeting.go +++ b/server/budgeting.go @@ -13,17 +13,17 @@ import ( type CategoryWithBalance struct { *postgres.GetCategoriesRow - Available numeric.Numeric - Activity numeric.Numeric - Assigned numeric.Numeric + AvailableLastMonth numeric.Numeric + Activity numeric.Numeric + Assigned numeric.Numeric } func NewCategoryWithBalance(category *postgres.GetCategoriesRow) CategoryWithBalance { return CategoryWithBalance{ - GetCategoriesRow: category, - Available: numeric.Zero(), - Activity: numeric.Zero(), - Assigned: numeric.Zero(), + GetCategoriesRow: category, + AvailableLastMonth: numeric.Zero(), + Activity: numeric.Zero(), + Assigned: numeric.Zero(), } } @@ -157,16 +157,17 @@ func (h *Handler) calculateBalances(budget postgres.Budget, month Month, } moneyUsed.SubI(bal.Assignments) - categoryWithBalance.Available.AddI(bal.Assignments) - categoryWithBalance.Available.AddI(bal.Transactions) - if !categoryWithBalance.Available.IsPositive() && month.InPast(bal.Date) { - moneyUsed.AddI(categoryWithBalance.Available) - categoryWithBalance.Available = numeric.Zero() - } - if month.InPresent(bal.Date) { categoryWithBalance.Activity = bal.Transactions categoryWithBalance.Assigned = bal.Assignments + continue + } + + categoryWithBalance.AvailableLastMonth.AddI(bal.Assignments) + categoryWithBalance.AvailableLastMonth.AddI(bal.Transactions) + if !categoryWithBalance.AvailableLastMonth.IsPositive() { + moneyUsed.AddI(categoryWithBalance.AvailableLastMonth) + categoryWithBalance.AvailableLastMonth = numeric.Zero() } } diff --git a/server/main_test.go b/server/main_test.go index 00b31fc..772bbe0 100644 --- a/server/main_test.go +++ b/server/main_test.go @@ -231,9 +231,11 @@ func (h Handler) CheckAvailableBalance(ctx context.Context, t *testing.T, testCa name := category.Group + " : " + category.Name if name == categoryName { - assertEqual(t, categoryTestData.Available, category.Available.GetFloat64(), "available for "+categoryName) assertEqual(t, categoryTestData.Activity, category.Activity.GetFloat64(), "activity for "+categoryName) assertEqual(t, categoryTestData.Assigned, category.Assigned.GetFloat64(), "assigned for "+categoryName) + available := category.AvailableLastMonth + available.AddI(category.Activity).AddI(category.Assigned) + assertEqual(t, categoryTestData.Available, available.GetFloat64(), "available for "+categoryName) found = true } }