1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-12-26 23:42:29 -05:00

testing: Test fetching all transactions

This commit is contained in:
Aaron Lindsay 2017-10-23 21:14:19 -04:00
parent 45b6482b15
commit 09a5cf9a99
2 changed files with 55 additions and 0 deletions

View File

@ -117,6 +117,11 @@ func (tl *TransactionList) Write(w http.ResponseWriter) error {
return enc.Encode(tl) return enc.Encode(tl)
} }
func (tl *TransactionList) Read(json_str string) error {
dec := json.NewDecoder(strings.NewReader(json_str))
return dec.Decode(tl)
}
func (atl *AccountTransactionsList) Write(w http.ResponseWriter) error { func (atl *AccountTransactionsList) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w) enc := json.NewEncoder(w)
return enc.Encode(atl) return enc.Encode(atl)

View File

@ -23,6 +23,15 @@ func getTransaction(client *http.Client, transactionid int64) (*handlers.Transac
return &s, nil return &s, nil
} }
func getTransactions(client *http.Client) (*handlers.TransactionList, error) {
var tl handlers.TransactionList
err := read(client, &tl, "/transaction/", "transactions")
if err != nil {
return nil, err
}
return &tl, nil
}
func updateTransaction(client *http.Client, transaction *handlers.Transaction) (*handlers.Transaction, error) { func updateTransaction(client *http.Client, transaction *handlers.Transaction) (*handlers.Transaction, error) {
var s handlers.Transaction var s handlers.Transaction
err := update(client, transaction, &s, "/transaction/"+strconv.FormatInt(transaction.TransactionId, 10), "transaction") err := update(client, transaction, &s, "/transaction/"+strconv.FormatInt(transaction.TransactionId, 10), "transaction")
@ -188,6 +197,47 @@ func TestGetTransaction(t *testing.T) {
}) })
} }
func TestGetTransactions(t *testing.T) {
RunWith(t, &data[0], func(t *testing.T, d *TestData) {
tl, err := getTransactions(d.clients[0])
if err != nil {
t.Fatalf("Error fetching transactions: %s\n", err)
}
numtransactions := 0
foundIds := make(map[int64]bool)
for i := 0; i < len(data[0].transactions); i++ {
orig := data[0].transactions[i]
curr := d.transactions[i]
if curr.UserId != d.users[0].UserId {
continue
}
numtransactions += 1
found := false
for _, tran := range *tl.Transactions {
if tran.TransactionId == curr.TransactionId {
ensureTransactionsMatch(t, &curr, &tran, nil, true, true)
if _, ok := foundIds[tran.TransactionId]; ok {
continue
}
foundIds[tran.TransactionId] = true
found = true
break
}
}
if !found {
t.Errorf("Unable to find matching transaction: %+v", orig)
}
}
if numtransactions != len(*tl.Transactions) {
t.Fatalf("Expected %d transactions, received %d", numtransactions, len(*tl.Transactions))
}
})
}
func TestUpdateTransaction(t *testing.T) { func TestUpdateTransaction(t *testing.T) {
RunWith(t, &data[0], func(t *testing.T, d *TestData) { RunWith(t, &data[0], func(t *testing.T, d *TestData) {
for i := 0; i < len(data[0].transactions); i++ { for i := 0; i < len(data[0].transactions); i++ {