From f0993fd9c392238b5183a7b623c2256e228b052e Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Tue, 5 Apr 2022 19:08:59 +0000 Subject: [PATCH] Read tests from file --- server/main_test.go | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/server/main_test.go b/server/main_test.go index 055a535..8f5a36b 100644 --- a/server/main_test.go +++ b/server/main_test.go @@ -87,18 +87,15 @@ type CategoryTestData struct { 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) + first := time.Date(2021, 12, 1, 0, 0, 0, 0, loc) + firstOfNextMonth := time.Date(2022, 1, 1, 0, 0, 0, 0, loc) data, err := h.prepareBudgeting(ctx, *budget, firstOfNextMonth, first) if err != nil { t.Errorf("prepare budgeting: %s", err) return } - if data.AvailableBalance.GetFloat64() != -115170.56 { - t.Errorf("available balance is wrong") - return - } + assert_equal(t, -115170.56, data.AvailableBalance.GetFloat64(), "available balance") categoryTestDataFile, err := os.Open("../testdata/production-export/results/categories-2021-12.json") if err != nil { @@ -114,19 +111,28 @@ func (h Handler) CheckAvailableBalance(ctx context.Context, t *testing.T, budget 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") + for categoryName, categoryTestData := range categoryTestData { + for _, category := range data.Categories { + name := category.Group + " : " + category.Name + + if name == categoryName { + 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) } + fmt.Printf("%s: %f %f\n", category.Name, category.Activity.GetFloat64(), category.Available.GetFloat64()) } - fmt.Printf("%s: %f %f\n", category.Name, category.Activity.GetFloat64(), category.Available.GetFloat64()) } } +func assert_equal(t *testing.T, expected, actual float64, message string) { + if expected == actual { + return + } + + t.Errorf("%s: expected %f, got %f", message, expected, actual) +} + func (h Handler) DoYNABImport(ctx context.Context, t *testing.T, budget *postgres.Budget) { t.Helper() budgetID := budget.ID