mirror of
https://github.com/aclindsa/moneygo.git
synced 2024-12-26 23:42:29 -05:00
testing: Add transaction-creation test
This commit is contained in:
parent
af9371aeb9
commit
cdba839c66
@ -7,6 +7,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Needed because handlers.User doesn't allow Password to be written to JSON
|
// 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)
|
t2.securities = append(t2.securities, *s2)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, account := range t.accounts {
|
for _, account := range t.accounts {
|
||||||
account.SecurityId = t2.securities[t.accounts[i].SecurityId].SecurityId
|
account.SecurityId = t2.securities[account.SecurityId].SecurityId
|
||||||
if account.ParentAccountId != -1 {
|
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)
|
a2, err := createAccount(t2.clients[account.UserId], &account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -99,6 +100,22 @@ func (t *TestData) Initialize() (*TestData, error) {
|
|||||||
t2.accounts = append(t2.accounts, *a2)
|
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
|
t2.initialized = true
|
||||||
return &t2, nil
|
return &t2, nil
|
||||||
}
|
}
|
||||||
@ -177,5 +194,26 @@ var data = []TestData{
|
|||||||
Name: "Groceries",
|
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",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -410,15 +410,6 @@ func TransactionHandler(r *http.Request, tx *Tx) ResponseWriterWriter {
|
|||||||
transaction.TransactionId = -1
|
transaction.TransactionId = -1
|
||||||
transaction.UserId = user.UserId
|
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 {
|
for i := range transaction.Splits {
|
||||||
transaction.Splits[i].SplitId = -1
|
transaction.Splits[i].SplitId = -1
|
||||||
_, err := GetAccount(tx, transaction.Splits[i].AccountId, user.UserId)
|
_, 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)
|
err = InsertTransaction(tx, &transaction, user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if _, ok := err.(AccountMissingError); ok {
|
if _, ok := err.(AccountMissingError); ok {
|
||||||
|
58
internal/handlers/transactions_test.go
Normal file
58
internal/handlers/transactions_test.go
Normal file
@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user