From 5f6bea4ee27df7fc370189e21bfbd00d258a435e Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Tue, 5 Apr 2022 19:00:40 +0000 Subject: [PATCH] Remove unused prop --- server/budgeting.go | 21 ++++++++------------- server/main_test.go | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/server/budgeting.go b/server/budgeting.go index 01e9b45..3f7cd42 100644 --- a/server/budgeting.go +++ b/server/budgeting.go @@ -25,19 +25,17 @@ func getFirstOfMonthTime(date time.Time) time.Time { type CategoryWithBalance struct { *postgres.GetCategoriesRow - Available numeric.Numeric - AvailableLastMonth numeric.Numeric - Activity numeric.Numeric - Assigned numeric.Numeric + Available numeric.Numeric + Activity numeric.Numeric + Assigned numeric.Numeric } func NewCategoryWithBalance(category *postgres.GetCategoriesRow) CategoryWithBalance { return CategoryWithBalance{ - GetCategoriesRow: category, - Available: numeric.Zero(), - AvailableLastMonth: numeric.Zero(), - Activity: numeric.Zero(), - Assigned: numeric.Zero(), + GetCategoriesRow: category, + Available: numeric.Zero(), + Activity: numeric.Zero(), + Assigned: numeric.Zero(), } } @@ -91,7 +89,6 @@ func (h *Handler) prepareBudgeting(ctx context.Context, budget postgres.Budget, } cat.Available = availableBalance - cat.AvailableLastMonth = availableBalance } data := BudgetingForMonthResponse{categoriesWithBalance, availableBalance} @@ -201,9 +198,7 @@ func (*Handler) CalculateCategoryBalances(cat *postgres.GetCategoriesRow, categoryWithBalance.Available = numeric.Zero() } - if bal.Date.Before(firstOfMonth) { - categoryWithBalance.AvailableLastMonth = categoryWithBalance.Available - } else if bal.Date.Before(firstOfNextMonth) { + if bal.Date.Before(firstOfNextMonth) { categoryWithBalance.Activity = bal.Transactions categoryWithBalance.Assigned = bal.Assignments } diff --git a/server/main_test.go b/server/main_test.go index 114e73c..055a535 100644 --- a/server/main_test.go +++ b/server/main_test.go @@ -2,6 +2,7 @@ package server import ( "context" + "encoding/json" "fmt" "io/fs" "log" @@ -69,15 +70,22 @@ func TestMain(t *testing.T) { handler.DoYNABImport(ctx, t, budget) + // check available balance for more dates handler.CheckAvailableBalance(ctx, t, budget) - // check available balance // check categories // check accounts } +type CategoryTestData struct { + Available float64 + Activity float64 + Assigned float64 +} + func (h Handler) CheckAvailableBalance(ctx context.Context, t *testing.T, budget *postgres.Budget) { + t.Helper() loc := time.Now().Location() first := time.Date(2022, 1, 1, 0, 0, 0, 0, loc) firstOfNextMonth := time.Date(2022, 2, 1, 0, 0, 0, 0, loc) @@ -91,6 +99,32 @@ func (h Handler) CheckAvailableBalance(ctx context.Context, t *testing.T, budget t.Errorf("available balance is wrong") return } + + categoryTestDataFile, err := os.Open("../testdata/production-export/results/categories-2021-12.json") + if err != nil { + t.Errorf("could not load category test data: %s", err) + return + } + + var categoryTestData map[string]CategoryTestData + dec := json.NewDecoder(categoryTestDataFile) + err = dec.Decode(&categoryTestData) + if err != nil { + t.Errorf("could not decode category test data: %s", err) + return + } + + for _, category := range data.Categories { + if category.Name == "Young & Home" { + if category.Available.GetFloat64() != 67.55 { + t.Errorf("available for category Young & Home is wrong") + } + if category.Activity.GetFloat64() != -107.78 { + t.Errorf("available for category Young & Home is wrong") + } + } + fmt.Printf("%s: %f %f\n", category.Name, category.Activity.GetFloat64(), category.Available.GetFloat64()) + } } func (h Handler) DoYNABImport(ctx context.Context, t *testing.T, budget *postgres.Budget) { @@ -103,14 +137,14 @@ func (h Handler) DoYNABImport(ctx context.Context, t *testing.T, budget *postgre return } - transactions, err := os.Open("../.vscode/Register.tsv") + transactions, err := os.Open("../testdata/production-export/Register.tsv") if err != nil { fmt.Println(err) t.Fail() return } - assignments, err := os.Open("../.vscode/Budget.tsv") + assignments, err := os.Open("../testdata/production-export/Budget.tsv") if err != nil { fmt.Println(err) t.Fail()