sharedtestvalues: Add helper functions and implement throughout exchange tests (#1163)

* exchanges/sharedtestvalues: implement new functions to handle test skipping and announcements for standardising.

* exchanges: fin test impl.

* linter: fixes

* exchange_template: fix test

* allocate so it doesn't make a panic at the disco

* glorious: nits

* glorious: nits

* Update exchanges/sharedtestvalues/sharedtestvalues.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update exchanges/sharedtestvalues/sharedtestvalues.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious: nits

* linter: fix

* linter: shhhh

---------

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
Co-authored-by: Scott <gloriousCode@users.noreply.github.com>
This commit is contained in:
Ryan O'Hara-Reid
2023-04-28 13:05:42 +10:00
committed by GitHub
parent 492ea20f21
commit b20cf75d13
63 changed files with 2115 additions and 3161 deletions

View File

@@ -13,6 +13,7 @@ import (
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues"
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
)
@@ -23,7 +24,7 @@ const (
canManipulateRealOrders = false
)
var a Alphapoint
var a = &Alphapoint{}
func TestMain(m *testing.M) {
a.SetDefaults()
@@ -32,10 +33,6 @@ func TestMain(m *testing.M) {
os.Exit(m.Run())
}
func areTestAPIKeysSet() bool {
return a.ValidateAPICredentials(a.GetDefaultCredentials()) == nil
}
func TestGetTicker(t *testing.T) {
t.Parallel()
var ticker Ticker
@@ -274,9 +271,7 @@ func TestGetProducts(t *testing.T) {
func TestCreateAccount(t *testing.T) {
t.Parallel()
if !areTestAPIKeysSet() {
t.Skip("API keys not set, skipping")
}
sharedtestvalues.SkipTestIfCredentialsUnset(t, a)
err := a.CreateAccount(context.Background(),
"test", "account", "something@something.com", "0292383745", "lolcat123")
@@ -296,9 +291,7 @@ func TestCreateAccount(t *testing.T) {
func TestGetUserInfo(t *testing.T) {
t.Parallel()
if !areTestAPIKeysSet() {
t.Skip("API keys not set, skipping")
}
sharedtestvalues.SkipTestIfCredentialsUnset(t, a)
_, err := a.GetUserInfo(context.Background())
if err == nil {
@@ -308,9 +301,7 @@ func TestGetUserInfo(t *testing.T) {
func TestSetUserInfo(t *testing.T) {
t.Parallel()
if !areTestAPIKeysSet() {
t.Skip("API keys not set, skipping")
}
sharedtestvalues.SkipTestIfCredentialsUnset(t, a)
_, err := a.SetUserInfo(context.Background(),
"bla", "bla", "1", "meh", true, true)
@@ -321,9 +312,7 @@ func TestSetUserInfo(t *testing.T) {
func TestGetAccountInfo(t *testing.T) {
t.Parallel()
if !areTestAPIKeysSet() {
t.Skip("API keys not set, skipping")
}
sharedtestvalues.SkipTestIfCredentialsUnset(t, a)
_, err := a.UpdateAccountInfo(context.Background(), asset.Spot)
if err == nil {
@@ -333,9 +322,7 @@ func TestGetAccountInfo(t *testing.T) {
func TestGetAccountTrades(t *testing.T) {
t.Parallel()
if !areTestAPIKeysSet() {
t.Skip("API keys not set, skipping")
}
sharedtestvalues.SkipTestIfCredentialsUnset(t, a)
_, err := a.GetAccountTrades(context.Background(), "", 1, 2)
if err == nil {
@@ -345,9 +332,7 @@ func TestGetAccountTrades(t *testing.T) {
func TestGetDepositAddresses(t *testing.T) {
t.Parallel()
if !areTestAPIKeysSet() {
t.Skip("API keys not set, skipping")
}
sharedtestvalues.SkipTestIfCredentialsUnset(t, a)
_, err := a.GetDepositAddresses(context.Background())
if err == nil {
@@ -357,9 +342,7 @@ func TestGetDepositAddresses(t *testing.T) {
func TestWithdrawCoins(t *testing.T) {
t.Parallel()
if !areTestAPIKeysSet() {
t.Skip("API keys not set, skipping")
}
sharedtestvalues.SkipTestIfCredentialsUnset(t, a)
err := a.WithdrawCoins(context.Background(), "", "", "", 0.01)
if err == nil {
@@ -369,9 +352,7 @@ func TestWithdrawCoins(t *testing.T) {
func TestCreateOrder(t *testing.T) {
t.Parallel()
if !areTestAPIKeysSet() {
t.Skip("API keys not set, skipping")
}
sharedtestvalues.SkipTestIfCredentialsUnset(t, a)
_, err := a.CreateOrder(context.Background(),
"", "", order.Limit.String(), 0.01, 0)
@@ -382,9 +363,7 @@ func TestCreateOrder(t *testing.T) {
func TestModifyExistingOrder(t *testing.T) {
t.Parallel()
if !areTestAPIKeysSet() {
t.Skip("API keys not set, skipping")
}
sharedtestvalues.SkipTestIfCredentialsUnset(t, a)
_, err := a.ModifyExistingOrder(context.Background(), "", 1, 1)
if err == nil {
@@ -394,9 +373,7 @@ func TestModifyExistingOrder(t *testing.T) {
func TestCancelAllExistingOrders(t *testing.T) {
t.Parallel()
if !areTestAPIKeysSet() {
t.Skip("API keys not set, skipping")
}
sharedtestvalues.SkipTestIfCredentialsUnset(t, a)
err := a.CancelAllExistingOrders(context.Background(), "")
if err == nil {
@@ -406,9 +383,7 @@ func TestCancelAllExistingOrders(t *testing.T) {
func TestGetOrders(t *testing.T) {
t.Parallel()
if !areTestAPIKeysSet() {
t.Skip("API keys not set, skipping")
}
sharedtestvalues.SkipTestIfCredentialsUnset(t, a)
_, err := a.GetOrders(context.Background())
if err == nil {
@@ -418,9 +393,7 @@ func TestGetOrders(t *testing.T) {
func TestGetOrderFee(t *testing.T) {
t.Parallel()
if !areTestAPIKeysSet() {
t.Skip("API keys not set, skipping")
}
sharedtestvalues.SkipTestIfCredentialsUnset(t, a)
_, err := a.GetOrderFee(context.Background(), "", "", 1, 1)
if err == nil {
@@ -446,9 +419,9 @@ func TestGetActiveOrders(t *testing.T) {
}
_, err := a.GetActiveOrders(context.Background(), &getOrdersRequest)
if areTestAPIKeysSet() && err != nil {
if sharedtestvalues.AreAPICredentialsSet(a) && err != nil {
t.Errorf("Could not get open orders: %s", err)
} else if !areTestAPIKeysSet() && err == nil {
} else if !sharedtestvalues.AreAPICredentialsSet(a) && err == nil {
t.Error("Expecting an error when no keys are set")
}
}
@@ -462,9 +435,9 @@ func TestGetOrderHistory(t *testing.T) {
}
_, err := a.GetOrderHistory(context.Background(), &getOrdersRequest)
if areTestAPIKeysSet() && err != nil {
if sharedtestvalues.AreAPICredentialsSet(a) && err != nil {
t.Errorf("Could not get order history: %s", err)
} else if !areTestAPIKeysSet() && err == nil {
} else if !sharedtestvalues.AreAPICredentialsSet(a) && err == nil {
t.Error("Expecting an error when no keys are set")
}
}
@@ -474,9 +447,7 @@ func TestGetOrderHistory(t *testing.T) {
func TestSubmitOrder(t *testing.T) {
t.Parallel()
if areTestAPIKeysSet() && !canManipulateRealOrders {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
sharedtestvalues.SkipTestIfCannotManipulateOrders(t, a, canManipulateRealOrders)
var orderSubmission = &order.Submit{
Exchange: a.Name,
@@ -494,10 +465,10 @@ func TestSubmitOrder(t *testing.T) {
}
response, err := a.SubmitOrder(context.Background(), orderSubmission)
if !areTestAPIKeysSet() && err == nil {
if !sharedtestvalues.AreAPICredentialsSet(a) && err == nil {
t.Error("Expecting an error when no keys are set")
}
if areTestAPIKeysSet() && err != nil {
if sharedtestvalues.AreAPICredentialsSet(a) && err != nil {
t.Errorf("Withdraw failed to be placed: %v", err)
if response.Status != order.New {
@@ -508,9 +479,7 @@ func TestSubmitOrder(t *testing.T) {
func TestCancelExchangeOrder(t *testing.T) {
t.Parallel()
if areTestAPIKeysSet() && !canManipulateRealOrders {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
sharedtestvalues.SkipTestIfCannotManipulateOrders(t, a, canManipulateRealOrders)
currencyPair := currency.NewPair(currency.BTC, currency.LTC)
var orderCancellation = &order.Cancel{
@@ -522,19 +491,17 @@ func TestCancelExchangeOrder(t *testing.T) {
}
err := a.CancelOrder(context.Background(), orderCancellation)
if !areTestAPIKeysSet() && err == nil {
if !sharedtestvalues.AreAPICredentialsSet(a) && err == nil {
t.Error("Expecting an error when no keys are set")
}
if areTestAPIKeysSet() && err != nil {
if sharedtestvalues.AreAPICredentialsSet(a) && err != nil {
t.Errorf("Withdraw failed to be placed: %v", err)
}
}
func TestCancelAllExchangeOrders(t *testing.T) {
t.Parallel()
if areTestAPIKeysSet() && !canManipulateRealOrders {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
sharedtestvalues.SkipTestIfCannotManipulateOrders(t, a, canManipulateRealOrders)
currencyPair := currency.NewPair(currency.BTC, currency.LTC)
var orderCancellation = &order.Cancel{
@@ -546,10 +513,10 @@ func TestCancelAllExchangeOrders(t *testing.T) {
}
resp, err := a.CancelAllOrders(context.Background(), orderCancellation)
if !areTestAPIKeysSet() && err == nil {
if !sharedtestvalues.AreAPICredentialsSet(a) && err == nil {
t.Error("Expecting an error when no keys are set")
}
if areTestAPIKeysSet() && err != nil {
if sharedtestvalues.AreAPICredentialsSet(a) && err != nil {
t.Errorf("Withdraw failed to be placed: %v", err)
}
@@ -560,10 +527,8 @@ func TestCancelAllExchangeOrders(t *testing.T) {
func TestModifyOrder(t *testing.T) {
t.Parallel()
if areTestAPIKeysSet() && !canManipulateRealOrders {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
_, err := a.ModifyOrder(&order.Modify{AssetType: asset.Spot})
sharedtestvalues.SkipTestIfCredentialsUnset(t, a, canManipulateRealOrders)
_, err := a.ModifyOrder(context.Background(), &order.Modify{AssetType: asset.Spot})
if err == nil {
t.Error("ModifyOrder() Expected error")
}
@@ -580,9 +545,7 @@ func TestWithdraw(t *testing.T) {
func TestWithdrawFiat(t *testing.T) {
t.Parallel()
if areTestAPIKeysSet() && !canManipulateRealOrders {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
sharedtestvalues.SkipTestIfCredentialsUnset(t, a, canManipulateRealOrders)
_, err := a.WithdrawFiatFunds(context.Background(),
&withdraw.Request{})
@@ -593,11 +556,9 @@ func TestWithdrawFiat(t *testing.T) {
func TestWithdrawInternationalBank(t *testing.T) {
t.Parallel()
if areTestAPIKeysSet() && !canManipulateRealOrders {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
sharedtestvalues.SkipTestIfCredentialsUnset(t, a, canManipulateRealOrders)
_, err := a.WithdrawFiatFundsToInternationalBank(&withdraw.Request{})
_, err := a.WithdrawFiatFundsToInternationalBank(context.Background(), &withdraw.Request{})
if err != common.ErrNotYetImplemented {
t.Errorf("Expected '%v', received: '%v'", common.ErrNotYetImplemented, err)
}
@@ -609,7 +570,7 @@ func TestGetRecentTrades(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = a.GetRecentTrades(currencyPair, asset.Spot)
_, err = a.GetRecentTrades(context.Background(), currencyPair, asset.Spot)
if err != nil && err != common.ErrNotYetImplemented {
t.Error(err)
}

View File

@@ -4,6 +4,7 @@ import (
"context"
"errors"
"strconv"
"sync"
"time"
"github.com/thrasher-corp/gocryptotrader/common"
@@ -12,6 +13,8 @@ import (
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/deposit"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/protocol"
@@ -27,6 +30,11 @@ func (a *Alphapoint) GetDefaultConfig(_ context.Context) (*config.Exchange, erro
return nil, common.ErrFunctionNotSupported
}
// Start starts the Aplhapoint go routine
func (a *Alphapoint) Start(_ context.Context, _ *sync.WaitGroup) error {
return common.ErrNotYetImplemented
}
// SetDefaults sets current default settings
func (a *Alphapoint) SetDefaults() {
a.Name = "Alphapoint"
@@ -80,6 +88,11 @@ func (a *Alphapoint) SetDefaults() {
}
}
// Setup takes in the supplied exchange configuration details and sets params
func (a *Alphapoint) Setup(_ *config.Exchange) error {
return common.ErrFunctionNotSupported
}
// FetchTradablePairs returns a list of the exchanges tradable pairs
func (a *Alphapoint) FetchTradablePairs(_ context.Context, _ asset.Item) (currency.Pairs, error) {
return nil, common.ErrFunctionNotSupported
@@ -145,7 +158,7 @@ func (a *Alphapoint) FetchAccountInfo(ctx context.Context, assetType asset.Item)
}
// UpdateTickers updates the ticker for all currency pairs of a given asset type
func (a *Alphapoint) UpdateTickers(_ asset.Item) error {
func (a *Alphapoint) UpdateTickers(_ context.Context, _ asset.Item) error {
return common.ErrFunctionNotSupported
}
@@ -241,7 +254,7 @@ func (a *Alphapoint) GetWithdrawalsHistory(_ context.Context, _ currency.Code, _
}
// GetRecentTrades returns the most recent trades for a currency and asset
func (a *Alphapoint) GetRecentTrades(_ currency.Pair, _ asset.Item) ([]trade.Data, error) {
func (a *Alphapoint) GetRecentTrades(_ context.Context, _ currency.Pair, _ asset.Item) ([]trade.Data, error) {
return nil, common.ErrNotYetImplemented
}
@@ -276,8 +289,8 @@ func (a *Alphapoint) SubmitOrder(ctx context.Context, s *order.Submit) (*order.S
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (a *Alphapoint) ModifyOrder(_ *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrNotYetImplemented
func (a *Alphapoint) ModifyOrder(_ context.Context, _ *order.Modify) (*order.ModifyResponse, error) {
return nil, common.ErrNotYetImplemented
}
// CancelOrder cancels an order by its corresponding ID number
@@ -308,35 +321,25 @@ func (a *Alphapoint) CancelAllOrders(ctx context.Context, orderCancellation *ord
}
// GetOrderInfo returns order information based on order ID
func (a *Alphapoint) GetOrderInfo(ctx context.Context, orderID string, _ currency.Pair, _ asset.Item) (float64, error) {
orders, err := a.GetOrders(ctx)
if err != nil {
return 0, err
}
for x := range orders {
for y := range orders[x].OpenOrders {
if strconv.Itoa(orders[x].OpenOrders[y].ServerOrderID) == orderID {
return orders[x].OpenOrders[y].QtyRemaining, nil
}
}
}
return 0, errors.New("order not found")
func (a *Alphapoint) GetOrderInfo(_ context.Context, _ string, _ currency.Pair, _ asset.Item) (order.Detail, error) {
return order.Detail{}, common.ErrNotYetImplemented
}
// GetDepositAddress returns a deposit address for a specified currency
func (a *Alphapoint) GetDepositAddress(ctx context.Context, cryptocurrency currency.Code, _, _ string) (string, error) {
func (a *Alphapoint) GetDepositAddress(ctx context.Context, cryptocurrency currency.Code, _, _ string) (*deposit.Address, error) {
addresses, err := a.GetDepositAddresses(ctx)
if err != nil {
return "", err
return nil, err
}
for x := range addresses {
if addresses[x].Name == cryptocurrency.String() {
return addresses[x].DepositAddress, nil
return &deposit.Address{
Address: addresses[x].DepositAddress,
}, nil
}
}
return "", errors.New("associated currency address not found")
return nil, errors.New("associated currency address not found")
}
// WithdrawCryptocurrencyFunds returns a withdrawal ID when a withdrawal is
@@ -352,12 +355,12 @@ func (a *Alphapoint) WithdrawFiatFunds(_ context.Context, _ *withdraw.Request) (
// WithdrawFiatFundsToInternationalBank returns a withdrawal ID when a withdrawal is
// submitted
func (a *Alphapoint) WithdrawFiatFundsToInternationalBank(_ *withdraw.Request) (string, error) {
return "", common.ErrNotYetImplemented
func (a *Alphapoint) WithdrawFiatFundsToInternationalBank(_ context.Context, _ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
return nil, common.ErrNotYetImplemented
}
// GetFeeByType returns an estimate of fee based on type of transaction
func (a *Alphapoint) GetFeeByType(_ *exchange.FeeBuilder) (float64, error) {
func (a *Alphapoint) GetFeeByType(_ context.Context, _ *exchange.FeeBuilder) (float64, error) {
return 0, common.ErrFunctionNotSupported
}
@@ -439,9 +442,20 @@ func (a *Alphapoint) GetOrderHistory(ctx context.Context, req *order.GetOrdersRe
return req.Filter(a.Name, orders), nil
}
// ValidateCredentials validates current credentials used for wrapper
// ValidateAPICredentials validates current credentials used for wrapper
// functionality
func (a *Alphapoint) ValidateCredentials(ctx context.Context, assetType asset.Item) error {
func (a *Alphapoint) ValidateAPICredentials(ctx context.Context, assetType asset.Item) error {
_, err := a.UpdateAccountInfo(ctx, assetType)
return a.CheckTransientError(err)
}
// GetHistoricCandles returns candles between a time period for a set time interval
func (a *Alphapoint) GetHistoricCandles(_ context.Context, _ currency.Pair, _ asset.Item, _ kline.Interval, _, _ time.Time) (*kline.Item, error) {
return nil, common.ErrNotYetImplemented
}
// GetHistoricCandlesExtended returns candles between a time period for a set
// time interval
func (a *Alphapoint) GetHistoricCandlesExtended(_ context.Context, _ currency.Pair, _ asset.Item, _ kline.Interval, _, _ time.Time) (*kline.Item, error) {
return nil, common.ErrNotYetImplemented
}