From d656f15e841fbfb4451e442c60a899d9503c40f0 Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Sun, 26 Nov 2017 21:01:26 -0500 Subject: [PATCH] testing: Ensure account balance is correct after OFX import --- internal/handlers/common_test.go | 12 ++++++++++++ internal/handlers/gnucash_test.go | 22 +++++----------------- internal/handlers/ofx_test.go | 3 +++ 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/internal/handlers/common_test.go b/internal/handlers/common_test.go index a0141d0..5ff8c02 100644 --- a/internal/handlers/common_test.go +++ b/internal/handlers/common_test.go @@ -202,6 +202,18 @@ func uploadFile(client *http.Client, filename, urlsuffix string) error { return nil } +func accountBalanceHelper(t *testing.T, client *http.Client, account *handlers.Account, balance string) { + t.Helper() + transactions, err := getAccountTransactions(client, account.AccountId, 0, 0, "") + if err != nil { + t.Fatalf("Couldn't fetch account transactions for '%s': %s\n", account.Name, err) + } + + if transactions.EndingBalance != balance { + t.Errorf("Expected ending balance for '%s' to be '%s', but found %s\n", account.Name, balance, transactions.EndingBalance) + } +} + func RunWith(t *testing.T, d *TestData, fn TestDataFunc) { testdata, err := d.Initialize() if err != nil { diff --git a/internal/handlers/gnucash_test.go b/internal/handlers/gnucash_test.go index bf66e57..1cdf9d0 100644 --- a/internal/handlers/gnucash_test.go +++ b/internal/handlers/gnucash_test.go @@ -10,18 +10,6 @@ func importGnucash(client *http.Client, filename string) error { return uploadFile(client, filename, "/v1/imports/gnucash") } -func gnucashAccountBalanceHelper(t *testing.T, client *http.Client, account *handlers.Account, balance string) { - t.Helper() - transactions, err := getAccountTransactions(client, account.AccountId, 0, 0, "") - if err != nil { - t.Fatalf("Couldn't fetch account transactions for '%s': %s\n", account.Name, err) - } - - if transactions.EndingBalance != balance { - t.Errorf("Expected ending balance for '%s' to be '%s', but found %s\n", account.Name, balance, transactions.EndingBalance) - } -} - func TestImportGnucash(t *testing.T) { RunWith(t, &data[0], func(t *testing.T, d *TestData) { // Ensure there's only one USD currency @@ -100,11 +88,11 @@ func TestImportGnucash(t *testing.T) { t.Fatalf("Couldn't find 'Expenses/Cable' account") } - gnucashAccountBalanceHelper(t, d.clients[0], salary, "-998.34") - gnucashAccountBalanceHelper(t, d.clients[0], creditcard, "-272.03") - gnucashAccountBalanceHelper(t, d.clients[0], openingbalances, "-21014.33") - gnucashAccountBalanceHelper(t, d.clients[0], groceries, "287.56") // 87.19 from preexisting transactions and 200.37 from Gnucash - gnucashAccountBalanceHelper(t, d.clients[0], cable, "89.98") + accountBalanceHelper(t, d.clients[0], salary, "-998.34") + accountBalanceHelper(t, d.clients[0], creditcard, "-272.03") + accountBalanceHelper(t, d.clients[0], openingbalances, "-21014.33") + accountBalanceHelper(t, d.clients[0], groceries, "287.56") // 87.19 from preexisting transactions and 200.37 from Gnucash + accountBalanceHelper(t, d.clients[0], cable, "89.98") var ge *handlers.Security securities, err := getSecurities(d.clients[0]) diff --git a/internal/handlers/ofx_test.go b/internal/handlers/ofx_test.go index 74ce496..1e6a440 100644 --- a/internal/handlers/ofx_test.go +++ b/internal/handlers/ofx_test.go @@ -29,8 +29,11 @@ func TestImportOFX(t *testing.T) { if err = importOFX(d.clients[0], d.accounts[1].AccountId, "handlers_testdata/checking_20171126.ofx"); err != nil { t.Fatalf("Error importing OFX: %s\n", err) } + accountBalanceHelper(t, d.clients[0], &d.accounts[1], "2493.19") + if err = importOFX(d.clients[0], d.accounts[1].AccountId, "handlers_testdata/checking_20171129.ofx"); err != nil { t.Fatalf("Error importing OFX: %s\n", err) } + accountBalanceHelper(t, d.clients[0], &d.accounts[1], "5336.27") }) }