moneygo/internal/handlers/transactions_test.go

253 lines
7.4 KiB
Go
Raw Normal View History

2017-10-16 05:39:41 -04:00
package handlers_test
import (
"github.com/aclindsa/moneygo/internal/handlers"
"net/http"
"strconv"
"testing"
"time"
2017-10-16 05:39:41 -04:00
)
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 ensureTransactionsMatch(t *testing.T, expected, tran *handlers.Transaction, accounts *[]handlers.Account, matchtransactionids, matchsplitids bool) {
t.Helper()
2017-10-16 05:39:41 -04:00
if tran.TransactionId == 0 {
t.Errorf("TransactionId is 0")
}
if matchtransactionids && tran.TransactionId != expected.TransactionId {
t.Errorf("TransactionId (%d) doesn't match what's expected (%d)", tran.TransactionId, expected.TransactionId)
}
if tran.Description != expected.Description {
t.Errorf("Description doesn't match")
}
if tran.Date != expected.Date {
t.Errorf("Date doesn't match")
}
if len(tran.Splits) != len(expected.Splits) {
t.Fatalf("Expected %d splits, received %d", len(expected.Splits), len(tran.Splits))
}
foundIds := make(map[int64]bool)
for j := 0; j < len(expected.Splits); j++ {
origsplit := expected.Splits[j]
if tran.Splits[j].TransactionId != tran.TransactionId {
t.Fatalf("Split TransactionId doesn't match transaction's")
}
found := false
for _, s := range tran.Splits {
if s.SplitId == 0 {
t.Errorf("Found SplitId that's 0")
}
accountid := origsplit.AccountId
if accounts != nil {
accountid = (*accounts)[accountid].AccountId
}
if origsplit.Status == s.Status &&
origsplit.ImportSplitType == s.ImportSplitType &&
s.AccountId == accountid &&
s.SecurityId == -1 &&
origsplit.RemoteId == origsplit.RemoteId &&
origsplit.Number == s.Number &&
origsplit.Memo == s.Memo &&
origsplit.Amount == s.Amount &&
(!matchsplitids || origsplit.SplitId == s.SplitId) {
if _, ok := foundIds[s.SplitId]; ok {
continue
}
foundIds[s.SplitId] = true
found = true
break
}
}
if !found {
t.Errorf("Unable to find matching split: %+v", origsplit)
}
}
}
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]
ensureTransactionsMatch(t, &orig, &transaction, &d.accounts, false, false)
}
2017-10-23 05:47:51 -04:00
// Don't allow imbalanced transactions
tran := handlers.Transaction{
UserId: d.users[0].UserId,
Description: "Imbalanced",
Date: time.Date(2017, time.September, 1, 0, 00, 00, 0, time.UTC),
Splits: []*handlers.Split{
&handlers.Split{
Status: handlers.Reconciled,
AccountId: d.accounts[1].AccountId,
SecurityId: -1,
Amount: "-39.98",
},
&handlers.Split{
Status: handlers.Entered,
AccountId: d.accounts[4].AccountId,
SecurityId: -1,
Amount: "39.99",
},
},
}
_, err := createTransaction(d.clients[0], &tran)
if err == nil {
t.Fatalf("Expected error creating imbalanced transaction")
}
if herr, ok := err.(*handlers.Error); ok {
if herr.ErrorId != 3 { // Invalid requeset
t.Fatalf("Unexpected API error creating imbalanced transaction: %s", herr)
}
} else {
t.Fatalf("Unexpected error creating imbalanced transaction")
}
// Don't allow transactions with 0 splits
tran.Splits = []*handlers.Split{}
_, err = createTransaction(d.clients[0], &tran)
if err == nil {
t.Fatalf("Expected error creating with zero splits")
}
if herr, ok := err.(*handlers.Error); ok {
if herr.ErrorId != 3 { // Invalid requeset
t.Fatalf("Unexpected API error creating with zero splits: %s", herr)
}
} else {
t.Fatalf("Unexpected error creating zero splits")
}
// Don't allow creating a transaction for another user
tran.UserId = d.users[1].UserId
_, err = createTransaction(d.clients[0], &tran)
if err == nil {
t.Fatalf("Expected error creating transaction for another user")
}
if herr, ok := err.(*handlers.Error); ok {
if herr.ErrorId != 3 { // Invalid request
t.Fatalf("Unexpected API error creating transction for another user: %s", herr)
}
} else {
t.Fatalf("Unexpected error creating transaction for another user")
}
})
}
func TestGetTransaction(t *testing.T) {
RunWith(t, &data[0], func(t *testing.T, d *TestData) {
for i := 0; i < len(data[0].transactions); i++ {
orig := data[0].transactions[i]
curr := d.transactions[i]
tran, err := getTransaction(d.clients[orig.UserId], curr.TransactionId)
if err != nil {
t.Fatalf("Error fetching transaction: %s\n", err)
}
ensureTransactionsMatch(t, &curr, tran, nil, true, true)
}
})
}
func TestUpdateTransaction(t *testing.T) {
RunWith(t, &data[0], func(t *testing.T, d *TestData) {
for i := 0; i < len(data[0].transactions); i++ {
orig := data[0].transactions[i]
curr := d.transactions[i]
curr.Description = "more money"
curr.Date = time.Date(2017, time.October, 18, 10, 41, 40, 0, time.UTC)
tran, err := updateTransaction(d.clients[orig.UserId], &curr)
if err != nil {
t.Fatalf("Error updating transaction: %s\n", err)
}
ensureTransactionsMatch(t, &curr, tran, nil, true, true)
tran.Splits = []*handlers.Split{}
for _, s := range curr.Splits {
var split handlers.Split
split = *s
tran.Splits = append(tran.Splits, &split)
}
2017-10-23 05:47:51 -04:00
// Don't allow updating transactions for other/invalid users
tran.UserId = tran.UserId + 1
tran2, err := updateTransaction(d.clients[orig.UserId], tran)
if tran2.UserId != curr.UserId {
t.Fatalf("Allowed updating transaction to have wrong UserId\n")
}
tran.UserId = curr.UserId
// Make sure we can't create an unbalanced transaction
tran.Splits[len(tran.Splits)-1].Amount = "42"
_, err = updateTransaction(d.clients[orig.UserId], tran)
if err == nil {
2017-10-23 05:47:51 -04:00
t.Fatalf("Expected error updating imbalanced transaction")
}
if herr, ok := err.(*handlers.Error); ok {
if herr.ErrorId != 3 { // Invalid requeset
2017-10-23 05:47:51 -04:00
t.Fatalf("Unexpected API error updating imbalanced transaction: %s", herr)
}
} else {
2017-10-23 05:47:51 -04:00
t.Fatalf("Unexpected error updating imbalanced transaction")
}
// Don't allow transactions with 0 splits
tran.Splits = []*handlers.Split{}
_, err = updateTransaction(d.clients[orig.UserId], tran)
if err == nil {
t.Fatalf("Expected error updating with zero splits")
}
if herr, ok := err.(*handlers.Error); ok {
if herr.ErrorId != 3 { // Invalid requeset
t.Fatalf("Unexpected API error updating with zero splits: %s", herr)
}
} else {
t.Fatalf("Unexpected error updating zero splits")
}
2017-10-16 05:39:41 -04:00
}
})
}