2017-12-09 19:55:34 -05:00
|
|
|
package integration_test
|
2017-11-10 20:13:49 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/aclindsa/moneygo/internal/handlers"
|
2017-12-04 21:05:17 -05:00
|
|
|
"github.com/aclindsa/moneygo/internal/models"
|
2017-11-10 20:13:49 -05:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-12-04 21:05:17 -05:00
|
|
|
func createPrice(client *http.Client, price *models.Price) (*models.Price, error) {
|
|
|
|
var p models.Price
|
2017-11-16 19:17:51 -05:00
|
|
|
err := create(client, price, &p, "/v1/securities/"+strconv.FormatInt(price.SecurityId, 10)+"/prices/")
|
2017-11-10 20:13:49 -05:00
|
|
|
return &p, err
|
|
|
|
}
|
|
|
|
|
2017-12-04 21:05:17 -05:00
|
|
|
func getPrice(client *http.Client, priceid, securityid int64) (*models.Price, error) {
|
|
|
|
var p models.Price
|
2017-11-16 19:17:51 -05:00
|
|
|
err := read(client, &p, "/v1/securities/"+strconv.FormatInt(securityid, 10)+"/prices/"+strconv.FormatInt(priceid, 10))
|
2017-11-10 20:13:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &p, nil
|
|
|
|
}
|
|
|
|
|
2017-12-04 21:05:17 -05:00
|
|
|
func getPrices(client *http.Client, securityid int64) (*models.PriceList, error) {
|
|
|
|
var pl models.PriceList
|
2017-11-16 19:17:51 -05:00
|
|
|
err := read(client, &pl, "/v1/securities/"+strconv.FormatInt(securityid, 10)+"/prices/")
|
2017-11-10 20:13:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &pl, nil
|
|
|
|
}
|
|
|
|
|
2017-12-04 21:05:17 -05:00
|
|
|
func updatePrice(client *http.Client, price *models.Price) (*models.Price, error) {
|
|
|
|
var p models.Price
|
2017-11-16 19:17:51 -05:00
|
|
|
err := update(client, price, &p, "/v1/securities/"+strconv.FormatInt(price.SecurityId, 10)+"/prices/"+strconv.FormatInt(price.PriceId, 10))
|
2017-11-10 20:13:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &p, nil
|
|
|
|
}
|
|
|
|
|
2017-12-04 21:05:17 -05:00
|
|
|
func deletePrice(client *http.Client, p *models.Price) error {
|
2017-11-16 19:17:51 -05:00
|
|
|
err := remove(client, "/v1/securities/"+strconv.FormatInt(p.SecurityId, 10)+"/prices/"+strconv.FormatInt(p.PriceId, 10))
|
2017-11-10 20:13:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreatePrice(t *testing.T) {
|
|
|
|
RunWith(t, &data[0], func(t *testing.T, d *TestData) {
|
|
|
|
for i := 0; i < len(data[0].prices); i++ {
|
|
|
|
orig := data[0].prices[i]
|
|
|
|
p := d.prices[i]
|
|
|
|
|
|
|
|
if p.PriceId == 0 {
|
|
|
|
t.Errorf("Unable to create price: %+v", p)
|
|
|
|
}
|
|
|
|
if p.SecurityId != d.securities[orig.SecurityId].SecurityId {
|
|
|
|
t.Errorf("SecurityId doesn't match")
|
|
|
|
}
|
|
|
|
if p.CurrencyId != d.securities[orig.CurrencyId].SecurityId {
|
|
|
|
t.Errorf("CurrencyId doesn't match")
|
|
|
|
}
|
2017-11-17 05:19:27 -05:00
|
|
|
if !p.Date.Equal(orig.Date) {
|
2017-11-10 20:13:49 -05:00
|
|
|
t.Errorf("Date doesn't match")
|
|
|
|
}
|
|
|
|
if p.Value != orig.Value {
|
|
|
|
t.Errorf("Value doesn't match")
|
|
|
|
}
|
|
|
|
if p.RemoteId != orig.RemoteId {
|
|
|
|
t.Errorf("RemoteId doesn't match")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPrice(t *testing.T) {
|
|
|
|
RunWith(t, &data[0], func(t *testing.T, d *TestData) {
|
|
|
|
for i := 0; i < len(data[0].prices); i++ {
|
|
|
|
orig := data[0].prices[i]
|
|
|
|
curr := d.prices[i]
|
|
|
|
|
|
|
|
userid := data[0].securities[orig.SecurityId].UserId
|
2017-11-16 19:17:51 -05:00
|
|
|
p, err := getPrice(d.clients[userid], curr.PriceId, curr.SecurityId)
|
2017-11-10 20:13:49 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error fetching price: %s\n", err)
|
|
|
|
}
|
|
|
|
if p.SecurityId != d.securities[orig.SecurityId].SecurityId {
|
|
|
|
t.Errorf("SecurityId doesn't match")
|
|
|
|
}
|
|
|
|
if p.CurrencyId != d.securities[orig.CurrencyId].SecurityId {
|
|
|
|
t.Errorf("CurrencyId doesn't match")
|
|
|
|
}
|
2017-11-17 05:19:27 -05:00
|
|
|
if !p.Date.Equal(orig.Date) {
|
2017-11-10 20:13:49 -05:00
|
|
|
t.Errorf("Date doesn't match")
|
|
|
|
}
|
|
|
|
if p.Value != orig.Value {
|
|
|
|
t.Errorf("Value doesn't match")
|
|
|
|
}
|
|
|
|
if p.RemoteId != orig.RemoteId {
|
|
|
|
t.Errorf("RemoteId doesn't match")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPrices(t *testing.T) {
|
|
|
|
RunWith(t, &data[0], func(t *testing.T, d *TestData) {
|
2017-11-16 19:17:51 -05:00
|
|
|
for origsecurityid, security := range d.securities {
|
|
|
|
if data[0].securities[origsecurityid].UserId != 0 {
|
2017-11-10 20:13:49 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-11-16 19:17:51 -05:00
|
|
|
pl, err := getPrices(d.clients[0], security.SecurityId)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error fetching prices: %s\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
numprices := 0
|
|
|
|
foundIds := make(map[int64]bool)
|
|
|
|
for i := 0; i < len(data[0].prices); i++ {
|
|
|
|
orig := data[0].prices[i]
|
|
|
|
|
|
|
|
if orig.SecurityId != int64(origsecurityid) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
numprices += 1
|
|
|
|
|
|
|
|
found := false
|
|
|
|
for _, p := range *pl.Prices {
|
2017-11-17 05:19:27 -05:00
|
|
|
if p.SecurityId == d.securities[orig.SecurityId].SecurityId && p.CurrencyId == d.securities[orig.CurrencyId].SecurityId && p.Date.Equal(orig.Date) && p.Value == orig.Value && p.RemoteId == orig.RemoteId {
|
2017-11-16 19:17:51 -05:00
|
|
|
if _, ok := foundIds[p.PriceId]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
foundIds[p.PriceId] = true
|
|
|
|
found = true
|
|
|
|
break
|
2017-11-10 20:13:49 -05:00
|
|
|
}
|
2017-11-16 19:17:51 -05:00
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Errorf("Unable to find matching price: %+v", orig)
|
2017-11-10 20:13:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-16 19:17:51 -05:00
|
|
|
if numprices != len(*pl.Prices) {
|
|
|
|
t.Fatalf("Expected %d prices, received %d", numprices, len(*pl.Prices))
|
|
|
|
}
|
2017-11-10 20:13:49 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdatePrice(t *testing.T) {
|
|
|
|
RunWith(t, &data[0], func(t *testing.T, d *TestData) {
|
|
|
|
for i := 0; i < len(data[0].prices); i++ {
|
|
|
|
orig := data[0].prices[i]
|
|
|
|
curr := d.prices[i]
|
|
|
|
|
|
|
|
tmp := curr.SecurityId
|
|
|
|
curr.SecurityId = curr.CurrencyId
|
|
|
|
curr.CurrencyId = tmp
|
|
|
|
curr.Value = "5.55"
|
|
|
|
curr.Date = time.Date(2019, time.June, 5, 12, 5, 6, 7, time.UTC)
|
|
|
|
curr.RemoteId = "something"
|
|
|
|
|
|
|
|
userid := data[0].securities[orig.SecurityId].UserId
|
|
|
|
p, err := updatePrice(d.clients[userid], &curr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error updating price: %s\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.SecurityId != curr.SecurityId {
|
|
|
|
t.Errorf("SecurityId doesn't match")
|
|
|
|
}
|
|
|
|
if p.CurrencyId != curr.CurrencyId {
|
|
|
|
t.Errorf("CurrencyId doesn't match")
|
|
|
|
}
|
2017-11-17 05:19:27 -05:00
|
|
|
if !p.Date.Equal(curr.Date) {
|
2017-11-10 20:13:49 -05:00
|
|
|
t.Errorf("Date doesn't match")
|
|
|
|
}
|
|
|
|
if p.Value != curr.Value {
|
|
|
|
t.Errorf("Value doesn't match")
|
|
|
|
}
|
|
|
|
if p.RemoteId != curr.RemoteId {
|
|
|
|
t.Errorf("RemoteId doesn't match")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeletePrice(t *testing.T) {
|
|
|
|
RunWith(t, &data[0], func(t *testing.T, d *TestData) {
|
|
|
|
for i := 0; i < len(data[0].prices); i++ {
|
|
|
|
orig := data[0].prices[i]
|
|
|
|
curr := d.prices[i]
|
|
|
|
|
|
|
|
userid := data[0].securities[orig.SecurityId].UserId
|
|
|
|
err := deletePrice(d.clients[userid], &curr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error deleting price: %s\n", err)
|
|
|
|
}
|
|
|
|
|
2017-11-16 19:17:51 -05:00
|
|
|
_, err = getPrice(d.clients[userid], curr.PriceId, curr.SecurityId)
|
2017-11-10 20:13:49 -05:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error fetching deleted price")
|
|
|
|
}
|
|
|
|
if herr, ok := err.(*handlers.Error); ok {
|
|
|
|
if herr.ErrorId != 3 { // Invalid requeset
|
|
|
|
t.Fatalf("Unexpected API error fetching deleted price: %s", herr)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Fatalf("Unexpected error fetching deleted price")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|