From 14b0839b41132658cb6fba50f83aedbd08792dd0 Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Mon, 9 Oct 2017 21:10:22 -0400 Subject: [PATCH] Add security tests --- internal/handlers/securities.go | 2 +- internal/handlers/securities_test.go | 173 +++++++++++++++++++++++++++ internal/handlers/testdata_test.go | 17 +++ 3 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 internal/handlers/securities_test.go diff --git a/internal/handlers/securities.go b/internal/handlers/securities.go index acbc18c..fba8363 100644 --- a/internal/handlers/securities.go +++ b/internal/handlers/securities.go @@ -208,7 +208,7 @@ func DeleteSecurity(db *DB, s *Security) error { // Remove all prices involving this security (either of this security, or // using it as a currency) - _, err = transaction.Exec("DELETE * FROM prices WHERE SecurityId=? OR CurrencyId=?", s.SecurityId, s.SecurityId) + _, err = transaction.Exec("DELETE FROM prices WHERE SecurityId=? OR CurrencyId=?", s.SecurityId, s.SecurityId) if err != nil { transaction.Rollback() return err diff --git a/internal/handlers/securities_test.go b/internal/handlers/securities_test.go new file mode 100644 index 0000000..aecdb6f --- /dev/null +++ b/internal/handlers/securities_test.go @@ -0,0 +1,173 @@ +package handlers_test + +import ( + "github.com/aclindsa/moneygo/internal/handlers" + "net/http" + "strconv" + "testing" +) + +func createSecurity(client *http.Client, security *handlers.Security) (*handlers.Security, error) { + var s handlers.Security + err := create(client, security, &s, "/security/", "security") + return &s, err +} + +func getSecurity(client *http.Client, securityid int64) (*handlers.Security, error) { + var s handlers.Security + err := read(client, &s, "/security/"+strconv.FormatInt(securityid, 10), "security") + if err != nil { + return nil, err + } + return &s, nil +} + +func updateSecurity(client *http.Client, security *handlers.Security) (*handlers.Security, error) { + var s handlers.Security + err := update(client, security, &s, "/security/"+strconv.FormatInt(security.SecurityId, 10), "security") + if err != nil { + return nil, err + } + return &s, nil +} + +func deleteSecurity(client *http.Client, s *handlers.Security) error { + err := remove(client, "/security/"+strconv.FormatInt(s.SecurityId, 10), "security") + if err != nil { + return err + } + return nil +} + +func TestCreateSecurity(t *testing.T) { + RunWith(t, &data[0], func(t *testing.T, d *TestData) { + for i := 1; i < len(data[0].securities); i++ { + orig := data[0].securities[i] + s := d.securities[i] + + if s.SecurityId == 0 { + t.Errorf("Unable to create security: %+v", s) + } + if s.Name != orig.Name { + t.Errorf("Name doesn't match") + } + if s.Description != orig.Description { + t.Errorf("Description doesn't match") + } + if s.Symbol != orig.Symbol { + t.Errorf("Symbol doesn't match") + } + if s.Precision != orig.Precision { + t.Errorf("Precision doesn't match") + } + if s.Type != orig.Type { + t.Errorf("Type doesn't match") + } + if s.AlternateId != orig.AlternateId { + t.Errorf("AlternateId doesn't match") + } + } + }) +} + +func TestGetSecurity(t *testing.T) { + RunWith(t, &data[0], func(t *testing.T, d *TestData) { + for i := 1; i < len(data[0].securities); i++ { + orig := data[0].securities[i] + curr := d.securities[i] + + s, err := getSecurity(d.clients[orig.UserId], curr.SecurityId) + if err != nil { + t.Fatalf("Error fetching security: %s\n", err) + } + if s.SecurityId != curr.SecurityId { + t.Errorf("SecurityId doesn't match") + } + if s.Name != orig.Name { + t.Errorf("Name doesn't match") + } + if s.Description != orig.Description { + t.Errorf("Description doesn't match") + } + if s.Symbol != orig.Symbol { + t.Errorf("Symbol doesn't match") + } + if s.Precision != orig.Precision { + t.Errorf("Precision doesn't match") + } + if s.Type != orig.Type { + t.Errorf("Type doesn't match") + } + if s.AlternateId != orig.AlternateId { + t.Errorf("AlternateId doesn't match") + } + } + }) +} + +func TestUpdateSecurity(t *testing.T) { + RunWith(t, &data[0], func(t *testing.T, d *TestData) { + for i := 1; i < len(data[0].securities); i++ { + orig := data[0].securities[i] + curr := d.securities[i] + + curr.Name = "EUR" + curr.Description = "Euro" + curr.Symbol = "€" + curr.AlternateId = "978" + + s, err := updateSecurity(d.clients[orig.UserId], &curr) + if err != nil { + t.Fatalf("Error updating security: %s\n", err) + } + + if s.SecurityId != curr.SecurityId { + t.Errorf("SecurityId doesn't match") + } + if s.Name != curr.Name { + t.Errorf("Name doesn't match") + } + if s.Description != curr.Description { + t.Errorf("Description doesn't match") + } + if s.Symbol != curr.Symbol { + t.Errorf("Symbol doesn't match") + } + if s.Precision != curr.Precision { + t.Errorf("Precision doesn't match") + } + if s.Type != curr.Type { + t.Errorf("Type doesn't match") + } + if s.AlternateId != curr.AlternateId { + t.Errorf("AlternateId doesn't match") + } + } + }) +} + +func TestDeleteSecurity(t *testing.T) { + RunWith(t, &data[0], func(t *testing.T, d *TestData) { + for i := 1; i < len(data[0].securities); i++ { + orig := data[0].securities[i] + curr := d.securities[i] + + err := deleteSecurity(d.clients[orig.UserId], &curr) + if err != nil { + t.Fatalf("Error deleting security: %s\n", err) + } + + _, err = getSecurity(d.clients[orig.UserId], curr.SecurityId) + if err == nil { + t.Fatalf("Expected error fetching deleted security") + } + if herr, ok := err.(*handlers.Error); ok { + if herr.ErrorId != 3 { + t.Fatalf("Unexpected API error fetching deleted security: %s", herr) + } + } else { + t.Fatalf("Unexpected error fetching deleted security") + } + } + }) +} diff --git a/internal/handlers/testdata_test.go b/internal/handlers/testdata_test.go index 0139086..bed50f2 100644 --- a/internal/handlers/testdata_test.go +++ b/internal/handlers/testdata_test.go @@ -79,6 +79,14 @@ func (t *TestData) Initialize() (*TestData, error) { } } + for _, security := range t.securities { + s2, err := createSecurity(t2.clients[security.UserId], &security) + if err != nil { + return nil, err + } + t2.securities = append(t2.securities, *s2) + } + t2.initialized = true return &t2, nil } @@ -117,6 +125,15 @@ var data = []TestData{ Type: handlers.Currency, AlternateId: "840", }, + handlers.Security{ + UserId: 0, + Name: "SPY", + Description: "SPDR S&P 500 ETF Trust", + Symbol: "SPY", + Precision: 5, + Type: handlers.Stock, + AlternateId: "78462F103", + }, }, accounts: []handlers.Account{ handlers.Account{