mirror of
https://github.com/aclindsa/moneygo.git
synced 2024-12-26 07:33:21 -05:00
testing: More thoroughly test fetching account transactions
This commit is contained in:
parent
8ba6a3dbf2
commit
9721c04e52
@ -252,6 +252,25 @@ var data = []TestData{
|
||||
},
|
||||
},
|
||||
},
|
||||
handlers.Transaction{
|
||||
UserId: 0,
|
||||
Description: "weekly groceries",
|
||||
Date: time.Date(2017, time.October, 31, 19, 10, 14, 0, time.UTC),
|
||||
Splits: []*handlers.Split{
|
||||
&handlers.Split{
|
||||
Status: handlers.Reconciled,
|
||||
AccountId: 1,
|
||||
SecurityId: -1,
|
||||
Amount: "-81.59",
|
||||
},
|
||||
&handlers.Split{
|
||||
Status: handlers.Reconciled,
|
||||
AccountId: 3,
|
||||
SecurityId: -1,
|
||||
Amount: "81.59",
|
||||
},
|
||||
},
|
||||
},
|
||||
handlers.Transaction{
|
||||
UserId: 0,
|
||||
Description: "Cable",
|
||||
|
@ -351,16 +351,44 @@ func TestDeleteTransaction(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccountTransactions(t *testing.T) {
|
||||
RunWith(t, &data[0], func(t *testing.T, d *TestData) {
|
||||
for _, account := range d.accounts {
|
||||
func helperTestAccountTransactions(t *testing.T, d *TestData, account *handlers.Account, limit int64, sort string) {
|
||||
if account.UserId != d.users[0].UserId {
|
||||
continue
|
||||
return
|
||||
}
|
||||
atl, err := getAccountTransactions(d.clients[0], account.AccountId, 0, 0, "date-desc")
|
||||
|
||||
var transactions []handlers.Transaction
|
||||
var lastFetchCount int64
|
||||
|
||||
for page := int64(0); page == 0 || lastFetchCount > 0; page++ {
|
||||
atl, err := getAccountTransactions(d.clients[0], account.AccountId, page, limit, sort)
|
||||
if err != nil {
|
||||
t.Fatalf("Error fetching account transactions: %s\n", err)
|
||||
}
|
||||
if limit != 0 && atl.Transactions != nil && int64(len(*atl.Transactions)) > limit {
|
||||
t.Errorf("Exceeded limit of %d transactions (returned %d)\n", limit, len(*atl.Transactions))
|
||||
}
|
||||
if atl.Transactions != nil {
|
||||
for _, tran := range *atl.Transactions {
|
||||
transactions = append(transactions, tran)
|
||||
}
|
||||
lastFetchCount = int64(len(*atl.Transactions))
|
||||
} else {
|
||||
lastFetchCount = -1
|
||||
}
|
||||
}
|
||||
|
||||
var lastDate time.Time
|
||||
for _, tran := range transactions {
|
||||
if lastDate.IsZero() {
|
||||
lastDate = tran.Date
|
||||
continue
|
||||
} else if sort == "date-desc" && lastDate.Before(tran.Date) {
|
||||
t.Errorf("Sorted by date-desc, but later transaction has later date")
|
||||
} else if sort == "date-asc" && lastDate.After(tran.Date) {
|
||||
t.Errorf("Sorted by date-asc, but later transaction has earlier date")
|
||||
}
|
||||
lastDate = tran.Date
|
||||
}
|
||||
|
||||
numtransactions := 0
|
||||
foundIds := make(map[int64]bool)
|
||||
@ -387,8 +415,7 @@ func TestAccountTransactions(t *testing.T) {
|
||||
numtransactions += 1
|
||||
|
||||
found := false
|
||||
if atl.Transactions != nil {
|
||||
for _, tran := range *atl.Transactions {
|
||||
for _, tran := range transactions {
|
||||
if tran.TransactionId == curr.TransactionId {
|
||||
ensureTransactionsMatch(t, &curr, &tran, nil, true, true)
|
||||
if _, ok := foundIds[tran.TransactionId]; ok {
|
||||
@ -399,20 +426,26 @@ func TestAccountTransactions(t *testing.T) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Errorf("Unable to find matching transaction: %+v", curr)
|
||||
t.Errorf("Transactions: %+v\n", atl.Transactions)
|
||||
t.Errorf("Transactions: %+v\n", transactions)
|
||||
}
|
||||
}
|
||||
|
||||
if atl.Transactions == nil {
|
||||
if numtransactions != 0 {
|
||||
t.Fatalf("Expected %d transactions, received 0", numtransactions)
|
||||
}
|
||||
} else if numtransactions != len(*atl.Transactions) {
|
||||
t.Fatalf("Expected %d transactions, received %d", numtransactions, len(*atl.Transactions))
|
||||
if numtransactions != len(transactions) {
|
||||
t.Fatalf("Expected %d transactions, received %d", numtransactions, len(transactions))
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountTransactions(t *testing.T) {
|
||||
RunWith(t, &data[0], func(t *testing.T, d *TestData) {
|
||||
for _, account := range d.accounts {
|
||||
helperTestAccountTransactions(t, d, &account, 0, "date-desc")
|
||||
helperTestAccountTransactions(t, d, &account, 0, "date-asc")
|
||||
helperTestAccountTransactions(t, d, &account, 1, "date-desc")
|
||||
helperTestAccountTransactions(t, d, &account, 1, "date-asc")
|
||||
helperTestAccountTransactions(t, d, &account, 2, "date-desc")
|
||||
helperTestAccountTransactions(t, d, &account, 2, "date-asc")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user