Fix bugs in calculateBalances and handle not found categories
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Jan Bader 2022-04-05 19:33:41 +00:00
parent cf39db52fb
commit 684efffbdf
2 changed files with 34 additions and 40 deletions

View File

@ -162,24 +162,9 @@ 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)
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 {
@ -199,11 +184,14 @@ func (*Handler) CalculateCategoryBalances(cat *postgres.GetCategoriesRow,
categoryWithBalance.Available = numeric.Zero()
}
if bal.Date.Before(firstOfNextMonth) {
if bal.Date.Year() == firstOfMonth.Year() && bal.Date.Month() == firstOfMonth.Month() {
categoryWithBalance.Activity = bal.Transactions
categoryWithBalance.Assigned = bal.Assignments
}
}
return categoryWithBalance
categoriesWithBalance = append(categoriesWithBalance, categoryWithBalance)
}
return categoriesWithBalance, moneyUsed
}

View File

@ -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")
}
}
}