Skip to content

Commit

Permalink
Link up orderbook websocket code, improve exchange test coverage and …
Browse files Browse the repository at this point in the history
…various other fixes
  • Loading branch information
thrasher- committed Sep 14, 2017
1 parent 913c104 commit 87633c2
Show file tree
Hide file tree
Showing 34 changed files with 1,201 additions and 340 deletions.
37 changes: 25 additions & 12 deletions common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ func TestBase64Decode(t *testing.T) {
expectedOutput, actualResult, err),
)
}

_, err = Base64Decode("-")
if err == nil {
t.Error("Test failed. Bad base64 string failed returned nil error")
}
}

func TestBase64Encode(t *testing.T) {
Expand All @@ -226,7 +231,7 @@ func TestBase64Encode(t *testing.T) {
func TestStringSliceDifference(t *testing.T) {
t.Parallel()
originalInputOne := []string{"hello"}
originalInputTwo := []string{"moto"}
originalInputTwo := []string{"hello", "moto"}
expectedOutput := []string{"hello moto"}
actualResult := StringSliceDifference(originalInputOne, originalInputTwo)
if reflect.DeepEqual(expectedOutput, actualResult) {
Expand Down Expand Up @@ -334,14 +339,17 @@ func TestReplaceString(t *testing.T) {

func TestRoundFloat(t *testing.T) {
t.Parallel()
originalInput := float64(1.4545445445)
precisionInput := 2
expectedOutput := float64(1.45)
actualResult := RoundFloat(originalInput, precisionInput)
if expectedOutput != actualResult {
t.Error(fmt.Sprintf(
"Test failed. Expected '%f'. Actual '%f'.", expectedOutput, actualResult),
)
// mapping of input vs expected result
testTable := map[float64]float64{
2.3232323: 2.32,
-2.3232323: -2.32,
}
for testInput, expectedOutput := range testTable {
actualOutput := RoundFloat(testInput, 2)
if actualOutput != expectedOutput {
t.Error(fmt.Sprintf("Test failed. RoundFloat Expected '%f'. Actual '%f'.",
expectedOutput, actualOutput))
}
}
}

Expand Down Expand Up @@ -650,6 +658,11 @@ func TestWriteFile(t *testing.T) {
if err != nil {
t.Errorf("Test failed. Common WriteFile error: %s", err)
}

err = WriteFile("", nil)
if err == nil {
t.Error("Test failed. Common WriteFile allowed bad path")
}
}

func TestRemoveFile(t *testing.T) {
Expand All @@ -672,9 +685,9 @@ func TestGetURIPath(t *testing.T) {
t.Parallel()
// mapping of input vs expected result
testTable := map[string]string{
"https://api.gdax.com/accounts": "/accounts",
"https://api.gdax.com/accounts?a=1&b=2": "/accounts?a=1&b=2",
"ht:tp:/invalidurl": "",
"https://api.gdax.com/accounts": "/accounts",
"https://api.gdax.com/accounts?a=1&b=2": "/accounts?a=1&b=2",
"http://www.google.com/accounts?!@#$%;^^": "",
}
for testInput, expectedOutput := range testTable {
actualOutput := GetURIPath(testInput)
Expand Down
33 changes: 6 additions & 27 deletions currency/currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ const (
maxCurrencyPairsPerRequest = 350
yahooYQLURL = "https://query.yahooapis.com/v1/public/yql?"
yahooDatabase = "store://datatables.org/alltableswithkeys"
yahooEnabled = false
fixerAPI = "http://api.fixer.io/latest"
// DefaultCurrencies has the default minimum of FIAT values
DefaultCurrencies = "USD,AUD,EUR,CNY"
Expand All @@ -69,6 +68,7 @@ var (
ErrCurrencyNotFound = errors.New("unable to find specified currency")
ErrQueryingYahoo = errors.New("unable to query Yahoo currency values")
ErrQueryingYahooZeroCount = errors.New("yahoo returned zero currency data")
yahooEnabled = false
)

// IsDefaultCurrency checks if the currency passed in matches the default
Expand All @@ -91,6 +91,7 @@ func IsDefaultCryptocurrency(currency string) bool {
func IsFiatCurrency(currency string) bool {
if BaseCurrencies == "" {
log.Println("IsFiatCurrency: BaseCurrencies string variable not populated")
return false
}
return common.StringContains(BaseCurrencies, common.StringToUpper(currency))
}
Expand All @@ -101,6 +102,7 @@ func IsCryptocurrency(currency string) bool {
log.Println(
"IsCryptocurrency: CryptoCurrencies string variable not populated",
)
return false
}
return common.StringContains(CryptoCurrencies, common.StringToUpper(currency))
}
Expand Down Expand Up @@ -313,6 +315,8 @@ func FetchYahooCurrencyData(currencyPairs []string) error {
return err
}

log.Printf("Currency recv: %s", resp)

yahooResp := YahooJSONResponse{}
err = common.JSONDecode([]byte(resp), &yahooResp)
if err != nil {
Expand All @@ -338,30 +342,5 @@ func QueryYahooCurrencyValues(currencies string) error {
"%d fiat currency pairs generated. Fetching Yahoo currency data (this may take a minute)..\n",
len(currencyPairs),
)
var err error
var pairs []string
index := 0

if len(currencyPairs) > maxCurrencyPairsPerRequest {
for index < len(currencyPairs) {
if len(currencyPairs)-index > maxCurrencyPairsPerRequest {
pairs = currencyPairs[index : index+maxCurrencyPairsPerRequest]
index += maxCurrencyPairsPerRequest
} else {
pairs = currencyPairs[index:]
index += (len(currencyPairs) - index)
}
err = FetchYahooCurrencyData(pairs)
if err != nil {
return err
}
}
} else {
pairs = currencyPairs[index:]
err = FetchYahooCurrencyData(pairs)
if err != nil {
return err
}
}
return nil
return FetchYahooCurrencyData(currencyPairs)
}
98 changes: 74 additions & 24 deletions currency/currency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func TestIsDefaultCryptocurrency(t *testing.T) {
func TestIsFiatCurrency(t *testing.T) {
t.Parallel()

if IsFiatCurrency("") {
t.Error("Test failed. TestIsFiatCurrency returned true on an empty string")
}

BaseCurrencies = "USD,AUD"
var str1, str2, str3 string = "BTC", "USD", "birds123"

Expand All @@ -81,6 +85,10 @@ func TestIsFiatCurrency(t *testing.T) {
func TestIsCryptocurrency(t *testing.T) {
t.Parallel()

if IsCryptocurrency("") {
t.Error("Test failed. TestIsCryptocurrency returned true on an empty string")
}

CryptoCurrencies = "BTC,LTC,DASH"
var str1, str2, str3 string = "USD", "BTC", "pterodactyl123"

Expand Down Expand Up @@ -256,25 +264,25 @@ func TestCheckAndAddCurrency(t *testing.T) {
}

func TestSeedCurrencyData(t *testing.T) {
currencyRequestDefault := ""
currencyRequestUSDAUD := "USD,AUD"
currencyRequestObtuse := "WigWham"

err := SeedCurrencyData(currencyRequestDefault)
if err != nil {
t.Errorf(
"Test Failed. SeedCurrencyData: Error %s with currency as %s.",
err, currencyRequestDefault,
)
}
err2 := SeedCurrencyData(currencyRequestUSDAUD)
if err2 != nil {
t.Errorf(
"Test Failed. SeedCurrencyData: Error %s with currency as %s.",
err2, currencyRequestUSDAUD,
)
}
if yahooEnabled {
currencyRequestDefault := ""
currencyRequestUSDAUD := "USD,AUD"
currencyRequestObtuse := "WigWham"

err := SeedCurrencyData(currencyRequestDefault)
if err != nil {
t.Errorf(
"Test Failed. SeedCurrencyData: Error %s with currency as %s.",
err, currencyRequestDefault,
)
}
err2 := SeedCurrencyData(currencyRequestUSDAUD)
if err2 != nil {
t.Errorf(
"Test Failed. SeedCurrencyData: Error %s with currency as %s.",
err2, currencyRequestUSDAUD,
)
}
err3 := SeedCurrencyData(currencyRequestObtuse)
if err3 == nil {
t.Errorf(
Expand All @@ -283,6 +291,12 @@ func TestSeedCurrencyData(t *testing.T) {
)
}
}

yahooEnabled = false
err := SeedCurrencyData("")
if err != nil {
t.Errorf("Test failed. SeedCurrencyData via Fixer. Error: %s", err)
}
}

func TestMakecurrencyPairs(t *testing.T) {
Expand All @@ -299,12 +313,10 @@ func TestMakecurrencyPairs(t *testing.T) {
}

func TestConvertCurrency(t *testing.T) {
fiatCurrencies := DefaultCurrencies
for _, currencyFrom := range common.SplitStrings(fiatCurrencies, ",") {
for _, currencyTo := range common.SplitStrings(fiatCurrencies, ",") {
if currencyFrom == currencyTo {
continue
} else {
if yahooEnabled {
fiatCurrencies := DefaultCurrencies
for _, currencyFrom := range common.SplitStrings(fiatCurrencies, ",") {
for _, currencyTo := range common.SplitStrings(fiatCurrencies, ",") {
floatyMcfloat, err := ConvertCurrency(1000, currencyFrom, currencyTo)
if err != nil {
t.Errorf(
Expand All @@ -323,6 +335,44 @@ func TestConvertCurrency(t *testing.T) {
}
}
}

yahooEnabled = false
_, err := ConvertCurrency(1000, "USD", "AUD")
if err != nil {
t.Errorf("Test failed. ConvertCurrency USD -> AUD. Error %s", err)
}

_, err = ConvertCurrency(1000, "AUD", "USD")
if err != nil {
t.Errorf("Test failed. ConvertCurrency AUD -> AUD. Error %s", err)
}

_, err = ConvertCurrency(1000, "CNY", "AUD")
if err != nil {
t.Errorf("Test failed. ConvertCurrency USD -> AUD. Error %s", err)
}

// Test non-existant currencies

_, err = ConvertCurrency(1000, "ASDF", "USD")
if err == nil {
t.Errorf("Test failed. ConvertCurrency non-existant currency -> USD. Error %s", err)
}

_, err = ConvertCurrency(1000, "USD", "ASDF")
if err == nil {
t.Errorf("Test failed. ConvertCurrency USD -> non-existant currency. Error %s", err)
}

_, err = ConvertCurrency(1000, "CNY", "UAHF")
if err == nil {
t.Errorf("Test failed. ConvertCurrency non-USD currency CNY -> non-existant currency. Error %s", err)
}

_, err = ConvertCurrency(1000, "UASF", "UAHF")
if err == nil {
t.Errorf("Test failed. ConvertCurrency non-existant currency -> non-existant currency. Error %s", err)
}
}

func TestFetchFixerCurrencyData(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions currency/pair/pair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,20 @@ func TestNewCurrencyPairFromIndex(t *testing.T) {
actual, expected,
)
}

currency = "DOGEBTC"

pair = NewCurrencyPairFromIndex(currency, index)
pair.Delimiter = "-"
actual = pair.Pair()

expected = CurrencyItem("DOGE-BTC")
if actual != expected {
t.Errorf(
"Test failed. Pair(): %s was not equal to expected value: %s",
actual, expected,
)
}
}

func TestNewCurrencyPairFromString(t *testing.T) {
Expand Down
30 changes: 15 additions & 15 deletions exchanges/alphapoint/alphapoint_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (a *Alphapoint) GetExchangeAccountInfo() (exchange.AccountInfo, error) {
}

// UpdateTicker updates and returns the ticker for a currency pair
func (a *Alphapoint) UpdateTicker(p pair.CurrencyPair) (ticker.Price, error) {
func (a *Alphapoint) UpdateTicker(p pair.CurrencyPair, assetType string) (ticker.Price, error) {
var tickerPrice ticker.Price
tick, err := a.GetTicker(p.Pair().String())
if err != nil {
Expand All @@ -43,46 +43,46 @@ func (a *Alphapoint) UpdateTicker(p pair.CurrencyPair) (ticker.Price, error) {
tickerPrice.High = tick.High
tickerPrice.Volume = tick.Volume
tickerPrice.Last = tick.Last
ticker.ProcessTicker(a.GetName(), p, tickerPrice, ticker.Spot)
return ticker.GetTicker(a.Name, p, ticker.Spot)
ticker.ProcessTicker(a.GetName(), p, tickerPrice, assetType)
return ticker.GetTicker(a.Name, p, assetType)
}

// GetTickerPrice returns the ticker for a currency pair
func (a *Alphapoint) GetTickerPrice(p pair.CurrencyPair) (ticker.Price, error) {
tick, err := ticker.GetTicker(a.GetName(), p, ticker.Spot)
func (a *Alphapoint) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.Price, error) {
tick, err := ticker.GetTicker(a.GetName(), p, assetType)
if err != nil {
return a.UpdateTicker(p)
return a.UpdateTicker(p, assetType)
}
return tick, nil
}

// UpdateOrderbook updates and returns the orderbook for a currency pair
func (a *Alphapoint) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
var orderBook orderbook.OrderbookBase
func (a *Alphapoint) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := a.GetOrderbook(p.Pair().String())
if err != nil {
return orderBook, err
}

for x := range orderbookNew.Bids {
data := orderbookNew.Bids[x]
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Amount: data.Quantity, Price: data.Price})
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: data.Quantity, Price: data.Price})
}

for x := range orderbookNew.Asks {
data := orderbookNew.Asks[x]
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: data.Quantity, Price: data.Price})
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: data.Quantity, Price: data.Price})
}

orderbook.ProcessOrderbook(a.GetName(), p, orderBook)
return orderbook.GetOrderbook(a.Name, p)
orderbook.ProcessOrderbook(a.GetName(), p, orderBook, assetType)
return orderbook.GetOrderbook(a.Name, p, assetType)
}

// GetOrderbookEx returns the orderbook for a currency pair
func (a *Alphapoint) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
ob, err := orderbook.GetOrderbook(a.GetName(), p)
func (a *Alphapoint) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(a.GetName(), p, assetType)
if err == nil {
return a.UpdateOrderbook(p)
return a.UpdateOrderbook(p, assetType)
}
return ob, nil
}
10 changes: 5 additions & 5 deletions exchanges/anx/anx_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,17 @@ func (a *ANX) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.Pric
}

// GetOrderbookEx returns the orderbook for a currency pair
func (a *ANX) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
ob, err := orderbook.GetOrderbook(a.GetName(), p)
func (a *ANX) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(a.GetName(), p, assetType)
if err == nil {
return a.UpdateOrderbook(p)
return a.UpdateOrderbook(p, assetType)
}
return ob, nil
}

// UpdateOrderbook updates and returns the orderbook for a currency pair
func (a *ANX) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
var orderBook orderbook.OrderbookBase
func (a *ANX) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
return orderBook, nil
}

Expand Down
Loading

0 comments on commit 87633c2

Please sign in to comment.