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

@@ -18,6 +18,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues"
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
)
@@ -30,12 +31,10 @@ const (
testCurrency = "btc_usdt"
)
var z ZB
var wsSetupRan bool
func areTestAPIKeysSet() bool {
return z.ValidateAPICredentials(z.GetDefaultCredentials()) == nil
}
var (
z = &ZB{}
wsSetupRan bool
)
//nolint:gocritic // Only used as a testing helper function in this package
func setupWsAuth(t *testing.T) {
@@ -45,7 +44,7 @@ func setupWsAuth(t *testing.T) {
}
if !z.Websocket.IsEnabled() &&
!z.API.AuthenticatedWebsocketSupport ||
!areTestAPIKeysSet() ||
!sharedtestvalues.AreAPICredentialsSet(z) ||
!canManipulateRealOrders {
t.Skip(stream.WebsocketNotEnabled)
}
@@ -74,10 +73,7 @@ func TestStart(t *testing.T) {
func TestSpotNewOrder(t *testing.T) {
t.Parallel()
if !areTestAPIKeysSet() || !canManipulateRealOrders {
t.Skip()
}
sharedtestvalues.SkipTestIfCredentialsUnset(t, z, canManipulateRealOrders)
arg := SpotNewOrderRequestParams{
Symbol: testCurrency,
@@ -93,10 +89,7 @@ func TestSpotNewOrder(t *testing.T) {
func TestCancelExistingOrder(t *testing.T) {
t.Parallel()
if !areTestAPIKeysSet() || !canManipulateRealOrders {
t.Skip()
}
sharedtestvalues.SkipTestIfCredentialsUnset(t, z, canManipulateRealOrders)
err := z.CancelExistingOrder(context.Background(), 20180629145864850, testCurrency)
if err != nil {
@@ -165,7 +158,7 @@ func TestGetFeeByTypeOfflineTradeFee(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if !areTestAPIKeysSet() {
if !sharedtestvalues.AreAPICredentialsSet(z) {
if feeBuilder.FeeType != exchange.OfflineTradeFee {
t.Errorf("Expected %v, received %v", exchange.OfflineTradeFee, feeBuilder.FeeType)
}
@@ -237,6 +230,7 @@ func TestGetFee(t *testing.T) {
}
func TestFormatWithdrawPermissions(t *testing.T) {
t.Parallel()
expectedResult := exchange.AutoWithdrawCryptoText + " & " + exchange.NoFiatWithdrawalsText
withdrawPermissions := z.FormatWithdrawPermissions()
if withdrawPermissions != expectedResult {
@@ -245,6 +239,7 @@ func TestFormatWithdrawPermissions(t *testing.T) {
}
func TestGetActiveOrders(t *testing.T) {
t.Parallel()
if mockTests {
t.Skip("skipping authenticated function for mock testing")
}
@@ -256,14 +251,15 @@ func TestGetActiveOrders(t *testing.T) {
}
_, err := z.GetActiveOrders(context.Background(), &getOrdersRequest)
if areTestAPIKeysSet() && err != nil {
if sharedtestvalues.AreAPICredentialsSet(z) && err != nil {
t.Error(err)
} else if !areTestAPIKeysSet() && err == nil {
} else if !sharedtestvalues.AreAPICredentialsSet(z) && err == nil {
t.Error("expecting an error when no keys are set")
}
}
func TestGetOrderHistory(t *testing.T) {
t.Parallel()
if mockTests {
t.Skip("skipping authenticated function for mock testing")
}
@@ -275,9 +271,9 @@ func TestGetOrderHistory(t *testing.T) {
}
_, err := z.GetOrderHistory(context.Background(), &getOrdersRequest)
if areTestAPIKeysSet() && err != nil {
if sharedtestvalues.AreAPICredentialsSet(z) && err != nil {
t.Error(err)
} else if !areTestAPIKeysSet() && err == nil {
} else if !sharedtestvalues.AreAPICredentialsSet(z) && err == nil {
t.Error("expecting an error when no keys are set")
}
}
@@ -286,9 +282,9 @@ func TestGetOrderHistory(t *testing.T) {
// ----------------------------------------------------------------------------------------------------------------------------
func TestSubmitOrder(t *testing.T) {
if areTestAPIKeysSet() && !canManipulateRealOrders {
t.Skipf("Can place orders: %v", canManipulateRealOrders)
}
t.Parallel()
sharedtestvalues.SkipTestIfCannotManipulateOrders(t, z, canManipulateRealOrders)
if mockTests {
t.Skip("skipping authenticated function for mock testing")
}
@@ -308,20 +304,20 @@ func TestSubmitOrder(t *testing.T) {
AssetType: asset.Spot,
}
response, err := z.SubmitOrder(context.Background(), orderSubmission)
if areTestAPIKeysSet() && err != nil {
if sharedtestvalues.AreAPICredentialsSet(z) && err != nil {
t.Error(err)
} else if !areTestAPIKeysSet() && err == nil {
} else if !sharedtestvalues.AreAPICredentialsSet(z) && err == nil {
t.Error("expecting an error when no keys are set")
}
if areTestAPIKeysSet() && response.OrderID == "" {
if sharedtestvalues.AreAPICredentialsSet(z) && response.OrderID == "" {
t.Error("expected order id")
}
}
func TestCancelExchangeOrder(t *testing.T) {
if areTestAPIKeysSet() && !canManipulateRealOrders {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
t.Parallel()
sharedtestvalues.SkipTestIfCannotManipulateOrders(t, z, canManipulateRealOrders)
if mockTests {
t.Skip("skipping authenticated function for mock testing")
}
@@ -336,17 +332,17 @@ func TestCancelExchangeOrder(t *testing.T) {
}
err := z.CancelOrder(context.Background(), orderCancellation)
if areTestAPIKeysSet() && err != nil {
if sharedtestvalues.AreAPICredentialsSet(z) && err != nil {
t.Error(err)
} else if !areTestAPIKeysSet() && err == nil {
} else if !sharedtestvalues.AreAPICredentialsSet(z) && err == nil {
t.Error("expecting an error when no keys are set")
}
}
func TestCancelAllExchangeOrders(t *testing.T) {
if areTestAPIKeysSet() && !canManipulateRealOrders {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
t.Parallel()
sharedtestvalues.SkipTestIfCannotManipulateOrders(t, z, canManipulateRealOrders)
if mockTests {
t.Skip("skipping authenticated function for mock testing")
}
@@ -362,9 +358,9 @@ func TestCancelAllExchangeOrders(t *testing.T) {
resp, err := z.CancelAllOrders(context.Background(), orderCancellation)
if areTestAPIKeysSet() && err != nil {
if sharedtestvalues.AreAPICredentialsSet(z) && err != nil {
t.Error(err)
} else if !areTestAPIKeysSet() && err == nil {
} else if !sharedtestvalues.AreAPICredentialsSet(z) && err == nil {
t.Error("expecting an error when no keys are set")
}
if len(resp.Status) > 0 {
@@ -373,10 +369,11 @@ func TestCancelAllExchangeOrders(t *testing.T) {
}
func TestGetAccountInfo(t *testing.T) {
t.Parallel()
if mockTests {
t.Skip("skipping authenticated function for mock testing")
}
if areTestAPIKeysSet() {
if sharedtestvalues.AreAPICredentialsSet(z) {
_, err := z.UpdateAccountInfo(context.Background(), asset.Spot)
if err != nil {
t.Error("GetAccountInfo() error", err)
@@ -390,12 +387,11 @@ func TestGetAccountInfo(t *testing.T) {
}
func TestModifyOrder(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCannotManipulateOrders(t, z, canManipulateRealOrders)
if mockTests {
t.Skip("skipping authenticated function for mock testing")
}
if areTestAPIKeysSet() && !canManipulateRealOrders {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
_, err := z.ModifyOrder(context.Background(),
&order.Modify{AssetType: asset.Spot})
if err == nil {
@@ -404,12 +400,12 @@ func TestModifyOrder(t *testing.T) {
}
func TestWithdraw(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCannotManipulateOrders(t, z, canManipulateRealOrders)
if mockTests {
t.Skip("skipping authenticated function for mock testing")
}
if areTestAPIKeysSet() && !canManipulateRealOrders {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
withdrawCryptoRequest := withdraw.Request{
Exchange: z.Name,
@@ -424,20 +420,20 @@ func TestWithdraw(t *testing.T) {
_, err := z.WithdrawCryptocurrencyFunds(context.Background(),
&withdrawCryptoRequest)
if areTestAPIKeysSet() && err != nil {
if sharedtestvalues.AreAPICredentialsSet(z) && err != nil {
t.Error(err)
} else if !areTestAPIKeysSet() && err == nil {
} else if !sharedtestvalues.AreAPICredentialsSet(z) && err == nil {
t.Error("expecting an error when no keys are set")
}
}
func TestWithdrawFiat(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCannotManipulateOrders(t, z, canManipulateRealOrders)
if mockTests {
t.Skip("skipping authenticated function for mock testing")
}
if areTestAPIKeysSet() && !canManipulateRealOrders {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
var withdrawFiatRequest = withdraw.Request{}
_, err := z.WithdrawFiatFunds(context.Background(), &withdrawFiatRequest)
@@ -447,12 +443,12 @@ func TestWithdrawFiat(t *testing.T) {
}
func TestWithdrawInternationalBank(t *testing.T) {
t.Parallel()
sharedtestvalues.SkipTestIfCannotManipulateOrders(t, z, canManipulateRealOrders)
if mockTests {
t.Skip("skipping authenticated function for mock testing")
}
if areTestAPIKeysSet() && !canManipulateRealOrders {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
var withdrawFiatRequest = withdraw.Request{}
_, err := z.WithdrawFiatFundsToInternationalBank(context.Background(),
@@ -463,10 +459,11 @@ func TestWithdrawInternationalBank(t *testing.T) {
}
func TestGetDepositAddress(t *testing.T) {
t.Parallel()
if mockTests {
t.Skip("skipping authenticated function for mock testing")
}
if areTestAPIKeysSet() {
if sharedtestvalues.AreAPICredentialsSet(z) {
_, err := z.GetDepositAddress(context.Background(), currency.XRP, "", "")
if err != nil {
t.Error("GetDepositAddress() error PLEASE MAKE SURE YOU CREATE DEPOSIT ADDRESSES VIA ZB.COM",
@@ -481,10 +478,11 @@ func TestGetDepositAddress(t *testing.T) {
}
func TestGetMultiChainDepositAddress(t *testing.T) {
t.Parallel()
if mockTests {
t.Skip("skipping authenticated function for mock testing")
}
if areTestAPIKeysSet() {
if sharedtestvalues.AreAPICredentialsSet(z) {
_, err := z.GetMultiChainDepositAddress(context.Background(), currency.USDT)
if err != nil {
t.Error("GetDepositAddress() error PLEASE MAKE SURE YOU CREATE DEPOSIT ADDRESSES VIA ZB.COM",
@@ -1043,9 +1041,8 @@ func TestUpdateTickers(t *testing.T) {
func TestGetAvailableTransferChains(t *testing.T) {
t.Parallel()
if !areTestAPIKeysSet() {
t.Skip("api keys not set")
}
sharedtestvalues.SkipTestIfCredentialsUnset(t, z)
_, err := z.GetAvailableTransferChains(context.Background(), currency.BTC)
if err != nil {
t.Error(err)

View File

@@ -863,9 +863,9 @@ func (z *ZB) GetOrderHistory(ctx context.Context, req *order.GetOrdersRequest) (
return req.Filter(z.Name, orders), nil
}
// ValidateCredentials validates current credentials used for wrapper
// ValidateAPICredentials validates current credentials used for wrapper
// functionality
func (z *ZB) ValidateCredentials(ctx context.Context, assetType asset.Item) error {
func (z *ZB) ValidateAPICredentials(ctx context.Context, assetType asset.Item) error {
_, err := z.UpdateAccountInfo(ctx, assetType)
return z.CheckTransientError(err)
}