From 684efffbdfde6c62b1373fc02f5abda872633ff4 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Tue, 5 Apr 2022 19:33:41 +0000 Subject: [PATCH] Fix bugs in calculateBalances and handle not found categories --- server/budgeting.go | 64 ++++++++++++++++++--------------------------- server/main_test.go | 10 +++++-- 2 files changed, 34 insertions(+), 40 deletions(-) diff --git a/server/budgeting.go b/server/budgeting.go index dca6c71..05020f6 100644 --- a/server/budgeting.go +++ b/server/budgeting.go @@ -162,48 +162,36 @@ func (h *Handler) calculateBalances(budget postgres.Budget, ) ([]CategoryWithBalance, numeric.Numeric) { categoriesWithBalance := []CategoryWithBalance{} - moneyUsed2 := numeric.Zero() - moneyUsed := &moneyUsed2 + moneyUsed := numeric.Zero() for i := range categories { cat := &categories[i] - // do not show hidden categories - categoryWithBalance := h.CalculateCategoryBalances(cat, cumultativeBalances, - firstOfNextMonth, moneyUsed, firstOfMonth, budget) + categoryWithBalance := NewCategoryWithBalance(cat) + for _, bal := range cumultativeBalances { + if bal.CategoryID != cat.ID { + continue + } + + // skip everything in the future + if !bal.Date.Before(firstOfNextMonth) { + continue + } + + moneyUsed.SubI(bal.Assignments) + categoryWithBalance.Available.AddI(bal.Assignments) + categoryWithBalance.Available.AddI(bal.Transactions) + if !categoryWithBalance.Available.IsPositive() && bal.Date.Before(firstOfMonth) { + moneyUsed.AddI(categoryWithBalance.Available) + categoryWithBalance.Available = numeric.Zero() + } + + if bal.Date.Year() == firstOfMonth.Year() && bal.Date.Month() == firstOfMonth.Month() { + categoryWithBalance.Activity = bal.Transactions + categoryWithBalance.Assigned = bal.Assignments + } + } categoriesWithBalance = append(categoriesWithBalance, categoryWithBalance) } - return categoriesWithBalance, *moneyUsed -} - -func (*Handler) CalculateCategoryBalances(cat *postgres.GetCategoriesRow, - cumultativeBalances []postgres.GetCumultativeBalancesRow, firstOfNextMonth time.Time, - moneyUsed *numeric.Numeric, firstOfMonth time.Time, budget postgres.Budget, -) CategoryWithBalance { - categoryWithBalance := NewCategoryWithBalance(cat) - for _, bal := range cumultativeBalances { - if bal.CategoryID != cat.ID { - continue - } - - // skip everything in the future - if !bal.Date.Before(firstOfNextMonth) { - continue - } - - moneyUsed.SubI(bal.Assignments) - categoryWithBalance.Available.AddI(bal.Assignments) - categoryWithBalance.Available.AddI(bal.Transactions) - if !categoryWithBalance.Available.IsPositive() && bal.Date.Before(firstOfMonth) { - moneyUsed.AddI(categoryWithBalance.Available) - categoryWithBalance.Available = numeric.Zero() - } - - if bal.Date.Before(firstOfNextMonth) { - categoryWithBalance.Activity = bal.Transactions - categoryWithBalance.Assigned = bal.Assignments - } - } - - return categoryWithBalance + return categoriesWithBalance, moneyUsed } diff --git a/server/main_test.go b/server/main_test.go index 8f5a36b..7b2eae7 100644 --- a/server/main_test.go +++ b/server/main_test.go @@ -95,7 +95,8 @@ func (h Handler) CheckAvailableBalance(ctx context.Context, t *testing.T, budget return } - assert_equal(t, -115170.56, data.AvailableBalance.GetFloat64(), "available balance") + // 2022-01 assert_equal(t, -115170.56, data.AvailableBalance.GetFloat64(), "available balance") + assert_equal(t, -110181.600, data.AvailableBalance.GetFloat64(), "available balance") categoryTestDataFile, err := os.Open("../testdata/production-export/results/categories-2021-12.json") if err != nil { @@ -112,6 +113,7 @@ func (h Handler) CheckAvailableBalance(ctx context.Context, t *testing.T, budget } for categoryName, categoryTestData := range categoryTestData { + found := false for _, category := range data.Categories { name := category.Group + " : " + category.Name @@ -119,8 +121,12 @@ func (h Handler) CheckAvailableBalance(ctx context.Context, t *testing.T, budget assert_equal(t, categoryTestData.Available, category.Available.GetFloat64(), "available for "+categoryName) assert_equal(t, categoryTestData.Activity, category.Activity.GetFloat64(), "activity for "+categoryName) assert_equal(t, categoryTestData.Assigned, category.Assigned.GetFloat64(), "assigned for "+categoryName) + found = true } - fmt.Printf("%s: %f %f\n", category.Name, category.Activity.GetFloat64(), category.Available.GetFloat64()) + } + + if !found { + t.Errorf("category " + categoryName + " was not found in result") } } }