testing: Test fetching all a user's accounts

This commit is contained in:
Aaron Lindsay 2017-10-12 22:19:53 -04:00
parent c26ce83aa3
commit 6726d9cb2f
4 changed files with 55 additions and 2 deletions

View File

@ -124,6 +124,11 @@ func (al *AccountList) Write(w http.ResponseWriter) error {
return enc.Encode(al)
}
func (al *AccountList) Read(json_str string) error {
dec := json.NewDecoder(strings.NewReader(json_str))
return dec.Decode(al)
}
func GetAccount(db *DB, accountid int64, userid int64) (*Account, error) {
var a Account

View File

@ -22,6 +22,15 @@ func getAccount(client *http.Client, accountid int64) (*handlers.Account, error)
return &a, nil
}
func getAccounts(client *http.Client) (*handlers.AccountList, error) {
var al handlers.AccountList
err := read(client, &al, "/account/", "accounts")
if err != nil {
return nil, err
}
return &al, nil
}
func updateAccount(client *http.Client, account *handlers.Account) (*handlers.Account, error) {
var a handlers.Account
err := update(client, account, &a, "/account/"+strconv.FormatInt(account.AccountId, 10), "account")
@ -81,6 +90,46 @@ func TestGetAccount(t *testing.T) {
})
}
func TestGetAccounts(t *testing.T) {
RunWith(t, &data[0], func(t *testing.T, d *TestData) {
al, err := getAccounts(d.clients[0])
if err != nil {
t.Fatalf("Error fetching accounts: %s\n", err)
}
numaccounts := 0
foundIds := make(map[int64]bool)
for i := 0; i < len(data[0].accounts); i++ {
orig := data[0].accounts[i]
curr := d.accounts[i]
if curr.UserId != d.users[0].UserId {
continue
}
numaccounts += 1
found := false
for _, a := range *al.Accounts {
if orig.Name == a.Name && orig.Type == a.Type && a.ExternalAccountId == orig.ExternalAccountId && d.securities[orig.SecurityId].SecurityId == a.SecurityId && ((orig.ParentAccountId == -1 && a.ParentAccountId == -1) || d.accounts[orig.ParentAccountId].AccountId == a.ParentAccountId) {
if _, ok := foundIds[a.AccountId]; ok {
continue
}
foundIds[a.AccountId] = true
found = true
break
}
}
if !found {
t.Errorf("Unable to find matching account: %+v", orig)
}
}
if numaccounts != len(*al.Accounts) {
t.Fatalf("Expected %d accounts, received %d", numaccounts, len(*al.Accounts))
}
})
}
func TestUpdateAccount(t *testing.T) {
RunWith(t, &data[0], func(t *testing.T, d *TestData) {
for i := 1; i < len(data[0].accounts); i++ {

View File

@ -158,7 +158,6 @@ func TestGetSecurities(t *testing.T) {
}
}
} else if numsecurities != len(*sl.Securities) {
t.Errorf("%+v\n", *sl.Securities)
t.Fatalf("Expected %d securities, received %d", numsecurities, len(*sl.Securities))
}
})

View File

@ -90,7 +90,7 @@ func (t *TestData) Initialize() (*TestData, error) {
for i, account := range t.accounts {
account.SecurityId = t2.securities[t.accounts[i].SecurityId].SecurityId
if account.ParentAccountId != -1 {
account.ParentAccountId = t2.accounts[t.accounts[i].AccountId].AccountId
account.ParentAccountId = t2.accounts[t.accounts[i].ParentAccountId].AccountId
}
a2, err := createAccount(t2.clients[account.UserId], &account)
if err != nil {