From 09a5cf9a996fa66e9f877e29a4a502b6e18a4410 Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Mon, 23 Oct 2017 21:14:19 -0400 Subject: [PATCH] testing: Test fetching all transactions --- internal/handlers/transactions.go | 5 +++ internal/handlers/transactions_test.go | 50 ++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/internal/handlers/transactions.go b/internal/handlers/transactions.go index c30aab2..86bc6a8 100644 --- a/internal/handlers/transactions.go +++ b/internal/handlers/transactions.go @@ -117,6 +117,11 @@ func (tl *TransactionList) Write(w http.ResponseWriter) error { 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 { enc := json.NewEncoder(w) return enc.Encode(atl) diff --git a/internal/handlers/transactions_test.go b/internal/handlers/transactions_test.go index d79f9f9..fac64eb 100644 --- a/internal/handlers/transactions_test.go +++ b/internal/handlers/transactions_test.go @@ -23,6 +23,15 @@ func getTransaction(client *http.Client, transactionid int64) (*handlers.Transac 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) { var s handlers.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) { RunWith(t, &data[0], func(t *testing.T, d *TestData) { for i := 0; i < len(data[0].transactions); i++ {