From 880ed731f83728cb4544d80f8bafa6923d6ee340 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Wed, 6 Apr 2022 19:43:11 +0000 Subject: [PATCH] Add test for account balances --- server/main_test.go | 71 ++++++++++++++++++++++++++++++++++++++++++++- testdata | 2 +- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/server/main_test.go b/server/main_test.go index cface7b..674402f 100644 --- a/server/main_test.go +++ b/server/main_test.go @@ -88,7 +88,73 @@ func TestMain(t *testing.T) { AssertCategoriesAndAvailableEqual(ctx, t, loc, handler, budget) - // check accounts + AssertAccountsEqual(ctx, t, handler, budget) +} + +func AssertAccountsEqual(ctx context.Context, t *testing.T, handler *Handler, budget *postgres.Budget) { + t.Helper() + + t.Run("account balances", func(t *testing.T) { + t.Parallel() + resultDir := "../testdata/production-export/results" + files, err := os.ReadDir(resultDir) + if err != nil { + t.Errorf("could not load results: %s", err) + return + } + + for _, file := range files { + if file.IsDir() { + continue + } + + name := file.Name()[0 : len(file.Name())-5] + if name != "accounts" { + continue + } + + testCaseFile := filepath.Join(resultDir, file.Name()) + handler.CheckAccountBalance(ctx, t, testCaseFile, budget) + } + }) +} + +func (h Handler) CheckAccountBalance(ctx context.Context, t *testing.T, testCaseFile string, budget *postgres.Budget) { + t.Helper() + + accounts, err := h.Service.GetAccountsWithBalance(ctx, budget.ID) + if err != nil { + t.Errorf("get accounts: %s", err) + return + } + + testDataFile, err := os.Open(testCaseFile) + if err != nil { + t.Errorf("could not load category test data: %s", err) + return + } + + var testData map[string]float64 + dec := json.NewDecoder(testDataFile) + err = dec.Decode(&testData) + if err != nil { + t.Errorf("could not decode category test data: %s", err) + return + } + + for accountName, accountBalance := range testData { + found := false + for _, account := range accounts { + if account.Name == accountName { + assertEqual(t, accountBalance, account.WorkingBalance.GetFloat64(), "balance for "+accountName) + found = true + } + } + + if !found { + t.Errorf("account " + accountName + " was not found in result") + } + } } func AssertCategoriesAndAvailableEqual(ctx context.Context, t *testing.T, loc *time.Location, handler *Handler, budget *postgres.Budget) { @@ -110,6 +176,9 @@ func AssertCategoriesAndAvailableEqual(ctx context.Context, t *testing.T, loc *t name := file.Name()[0 : len(file.Name())-5] parts := strings.Split(name, "-") + if len(parts) != 2 { + continue + } year, _ := strconv.Atoi(parts[0]) month, _ := strconv.Atoi(parts[1]) first := time.Date(year, time.Month(month), 1, 0, 0, 0, 0, loc) diff --git a/testdata b/testdata index b6eda76..6ca3adc 160000 --- a/testdata +++ b/testdata @@ -1 +1 @@ -Subproject commit b6eda7681f92c2c635555c392660cb69bec2a1ed +Subproject commit 6ca3adcee2713e8205133bec6c24b45aa8d730d9