mirror of
https://github.com/aclindsa/moneygo.git
synced 2024-12-26 23:42:29 -05:00
testing: Add initial account transactions test
Update one SQL query to do an 'INNER JOIN' instead of an 'IN' with a subquery. This avoids error messages like: Error 1235: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
This commit is contained in:
parent
5056b2b2ec
commit
8ba6a3dbf2
@ -127,6 +127,11 @@ func (atl *AccountTransactionsList) Write(w http.ResponseWriter) error {
|
|||||||
return enc.Encode(atl)
|
return enc.Encode(atl)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (atl *AccountTransactionsList) Read(json_str string) error {
|
||||||
|
dec := json.NewDecoder(strings.NewReader(json_str))
|
||||||
|
return dec.Decode(atl)
|
||||||
|
}
|
||||||
|
|
||||||
func (t *Transaction) Valid() bool {
|
func (t *Transaction) Valid() bool {
|
||||||
for i := range t.Splits {
|
for i := range t.Splits {
|
||||||
if !t.Splits[i].Valid() {
|
if !t.Splits[i].Valid() {
|
||||||
@ -686,8 +691,8 @@ func GetAccountTransactions(tx *Tx, user *User, accountid int64, sort string, pa
|
|||||||
// Sum all the splits for all transaction splits for this account that
|
// Sum all the splits for all transaction splits for this account that
|
||||||
// occurred before the page we're returning
|
// occurred before the page we're returning
|
||||||
var amounts []string
|
var amounts []string
|
||||||
sql = "SELECT splits.Amount FROM splits WHERE splits.AccountId=? AND splits.TransactionId IN (SELECT DISTINCT transactions.TransactionId FROM transactions INNER JOIN splits ON transactions.TransactionId = splits.TransactionId WHERE transactions.UserId=? AND splits.AccountId=?" + sqlsort + balanceLimitOffset + ")"
|
sql = "SELECT s.Amount FROM splits AS s INNER JOIN (SELECT DISTINCT transactions.TransactionId FROM transactions INNER JOIN splits ON transactions.TransactionId = splits.TransactionId WHERE transactions.UserId=? AND splits.AccountId=?" + sqlsort + balanceLimitOffset + ") as t ON s.TransactionId = t.TransactionId WHERE s.AccountId=?"
|
||||||
_, err = tx.Select(&amounts, sql, accountid, user.UserId, accountid, balanceLimitOffsetArg)
|
_, err = tx.Select(&amounts, sql, user.UserId, accountid, balanceLimitOffsetArg, accountid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
package handlers_test
|
package handlers_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/aclindsa/moneygo/internal/handlers"
|
"github.com/aclindsa/moneygo/internal/handlers"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -32,6 +34,29 @@ func getTransactions(client *http.Client) (*handlers.TransactionList, error) {
|
|||||||
return &tl, nil
|
return &tl, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getAccountTransactions(client *http.Client, accountid, page, limit int64, sort string) (*handlers.AccountTransactionsList, error) {
|
||||||
|
var atl handlers.AccountTransactionsList
|
||||||
|
params := url.Values{}
|
||||||
|
|
||||||
|
query := fmt.Sprintf("/account/%d/transactions/", accountid)
|
||||||
|
if page != 0 {
|
||||||
|
params.Set("page", fmt.Sprintf("%d", page))
|
||||||
|
}
|
||||||
|
if limit != 0 {
|
||||||
|
params.Set("limit", fmt.Sprintf("%d", limit))
|
||||||
|
}
|
||||||
|
if len(sort) != 0 {
|
||||||
|
params.Set("sort", sort)
|
||||||
|
query += "?" + params.Encode()
|
||||||
|
}
|
||||||
|
|
||||||
|
err := read(client, &atl, query, "accounttransactions")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &atl, 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")
|
||||||
@ -325,3 +350,69 @@ 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 {
|
||||||
|
if account.UserId != d.users[0].UserId {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
atl, err := getAccountTransactions(d.clients[0], account.AccountId, 0, 0, "date-desc")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error fetching account transactions: %s\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
numtransactions := 0
|
||||||
|
foundIds := make(map[int64]bool)
|
||||||
|
for i := 0; i < len(d.transactions); i++ {
|
||||||
|
curr := d.transactions[i]
|
||||||
|
|
||||||
|
if curr.UserId != d.users[0].UserId {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Don't consider this transaction if we didn't find a split
|
||||||
|
// for the account we're considering
|
||||||
|
account_found := false
|
||||||
|
for _, s := range curr.Splits {
|
||||||
|
if s.AccountId == account.AccountId {
|
||||||
|
account_found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !account_found {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
numtransactions += 1
|
||||||
|
|
||||||
|
found := false
|
||||||
|
if atl.Transactions != nil {
|
||||||
|
for _, tran := range *atl.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", curr)
|
||||||
|
t.Errorf("Transactions: %+v\n", atl.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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user