diff --git a/internal/handlers/accounts.go b/internal/handlers/accounts.go index fa9fce8..d278b6c 100644 --- a/internal/handlers/accounts.go +++ b/internal/handlers/accounts.go @@ -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 diff --git a/internal/handlers/accounts_test.go b/internal/handlers/accounts_test.go index 5b2f3da..0c0bc70 100644 --- a/internal/handlers/accounts_test.go +++ b/internal/handlers/accounts_test.go @@ -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++ { diff --git a/internal/handlers/securities_test.go b/internal/handlers/securities_test.go index de9c036..e4c65be 100644 --- a/internal/handlers/securities_test.go +++ b/internal/handlers/securities_test.go @@ -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)) } }) diff --git a/internal/handlers/testdata_test.go b/internal/handlers/testdata_test.go index cfc744a..0b877c2 100644 --- a/internal/handlers/testdata_test.go +++ b/internal/handlers/testdata_test.go @@ -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 {