From cdba839c66da0053ea805ec03f7d4a79e1150bba Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Mon, 16 Oct 2017 05:39:41 -0400 Subject: [PATCH] testing: Add transaction-creation test --- internal/handlers/testdata_test.go | 44 +++++++++++++++++-- internal/handlers/transactions.go | 17 ++++---- internal/handlers/transactions_test.go | 58 ++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 12 deletions(-) create mode 100644 internal/handlers/transactions_test.go diff --git a/internal/handlers/testdata_test.go b/internal/handlers/testdata_test.go index 0b877c2..d320220 100644 --- a/internal/handlers/testdata_test.go +++ b/internal/handlers/testdata_test.go @@ -7,6 +7,7 @@ import ( "net/http" "strings" "testing" + "time" ) // Needed because handlers.User doesn't allow Password to be written to JSON @@ -87,10 +88,10 @@ func (t *TestData) Initialize() (*TestData, error) { t2.securities = append(t2.securities, *s2) } - for i, account := range t.accounts { - account.SecurityId = t2.securities[t.accounts[i].SecurityId].SecurityId + for _, account := range t.accounts { + account.SecurityId = t2.securities[account.SecurityId].SecurityId if account.ParentAccountId != -1 { - account.ParentAccountId = t2.accounts[t.accounts[i].ParentAccountId].AccountId + account.ParentAccountId = t2.accounts[account.ParentAccountId].AccountId } a2, err := createAccount(t2.clients[account.UserId], &account) if err != nil { @@ -99,6 +100,22 @@ func (t *TestData) Initialize() (*TestData, error) { t2.accounts = append(t2.accounts, *a2) } + for i, transaction := range t.transactions { + transaction.Splits = []*handlers.Split{} + for _, s := range t.transactions[i].Splits { + // Make a copy of the split since Splits is a slice of pointers so + // copying the transaction doesn't + split := *s + split.AccountId = t2.accounts[split.AccountId].AccountId + transaction.Splits = append(transaction.Splits, &split) + } + tt2, err := createTransaction(t2.clients[transaction.UserId], &transaction) + if err != nil { + return nil, err + } + t2.transactions = append(t2.transactions, *tt2) + } + t2.initialized = true return &t2, nil } @@ -177,5 +194,26 @@ var data = []TestData{ Name: "Groceries", }, }, + transactions: []handlers.Transaction{ + handlers.Transaction{ + UserId: 0, + Description: "blah", + Date: time.Date(2017, time.October, 15, 1, 16, 59, 0, time.UTC), + Splits: []*handlers.Split{ + &handlers.Split{ + Status: handlers.Reconciled, + AccountId: 1, + SecurityId: -1, + Amount: "-5.6", + }, + &handlers.Split{ + Status: handlers.Reconciled, + AccountId: 3, + SecurityId: -1, + Amount: "5.6", + }, + }, + }, + }, }, } diff --git a/internal/handlers/transactions.go b/internal/handlers/transactions.go index 1af7c26..e77d7b8 100644 --- a/internal/handlers/transactions.go +++ b/internal/handlers/transactions.go @@ -410,15 +410,6 @@ func TransactionHandler(r *http.Request, tx *Tx) ResponseWriterWriter { transaction.TransactionId = -1 transaction.UserId = user.UserId - balanced, err := transaction.Balanced(tx) - if err != nil { - log.Print(err) - return NewError(999 /*Internal Error*/) - } - if !transaction.Valid() || !balanced { - return NewError(3 /*Invalid Request*/) - } - for i := range transaction.Splits { transaction.Splits[i].SplitId = -1 _, err := GetAccount(tx, transaction.Splits[i].AccountId, user.UserId) @@ -427,6 +418,14 @@ func TransactionHandler(r *http.Request, tx *Tx) ResponseWriterWriter { } } + balanced, err := transaction.Balanced(tx) + if err != nil { + return NewError(999 /*Internal Error*/) + } + if !transaction.Valid() || !balanced { + return NewError(3 /*Invalid Request*/) + } + err = InsertTransaction(tx, &transaction, user) if err != nil { if _, ok := err.(AccountMissingError); ok { diff --git a/internal/handlers/transactions_test.go b/internal/handlers/transactions_test.go new file mode 100644 index 0000000..1ce723e --- /dev/null +++ b/internal/handlers/transactions_test.go @@ -0,0 +1,58 @@ +package handlers_test + +import ( + "github.com/aclindsa/moneygo/internal/handlers" + "net/http" + "strconv" + "testing" +) + +func createTransaction(client *http.Client, transaction *handlers.Transaction) (*handlers.Transaction, error) { + var s handlers.Transaction + err := create(client, transaction, &s, "/transaction/", "transaction") + return &s, err +} + +func getTransaction(client *http.Client, transactionid int64) (*handlers.Transaction, error) { + var s handlers.Transaction + err := read(client, &s, "/transaction/"+strconv.FormatInt(transactionid, 10), "transaction") + if err != nil { + return nil, err + } + return &s, nil +} + +func updateTransaction(client *http.Client, transaction *handlers.Transaction) (*handlers.Transaction, error) { + var s handlers.Transaction + err := update(client, transaction, &s, "/transaction/"+strconv.FormatInt(transaction.TransactionId, 10), "transaction") + if err != nil { + return nil, err + } + return &s, nil +} + +func deleteTransaction(client *http.Client, s *handlers.Transaction) error { + err := remove(client, "/transaction/"+strconv.FormatInt(s.TransactionId, 10), "transaction") + if err != nil { + return err + } + return nil +} + +func TestCreateTransaction(t *testing.T) { + RunWith(t, &data[0], func(t *testing.T, d *TestData) { + for i, orig := range data[0].transactions { + transaction := d.transactions[i] + + if transaction.TransactionId == 0 { + t.Errorf("Unable to create transaction: %+v", transaction) + } + if transaction.Description != orig.Description { + t.Errorf("Description doesn't match") + } + if transaction.Date != orig.Date { + t.Errorf("Date doesn't match") + } + } + }) +}