mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-14 07:26:47 +00:00
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:
@@ -53,8 +53,8 @@ func (c *CustomEx) IsEnabled() bool {
|
||||
func (c *CustomEx) SetEnabled(bool) {
|
||||
}
|
||||
|
||||
// ValidateCredentials is a mock method for CustomEx
|
||||
func (c *CustomEx) ValidateCredentials(_ context.Context, _ asset.Item) error {
|
||||
// ValidateAPICredentials is a mock method for CustomEx
|
||||
func (c *CustomEx) ValidateAPICredentials(_ context.Context, _ asset.Item) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package sharedtestvalues
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
|
||||
)
|
||||
|
||||
@@ -20,6 +23,11 @@ const (
|
||||
|
||||
MockTesting = "Mock testing framework in use for %s exchange on REST endpoints only"
|
||||
LiveTesting = "Mock testing bypassed; live testing of REST endpoints in use for %s exchange"
|
||||
|
||||
warningSkip = "Skipping test"
|
||||
warningKeys = "API test keys have not been set"
|
||||
warningManipulateOrders = "variable `canManipulateRealOrders` is false"
|
||||
warningHowTo = "these values can be set at the top of the test file."
|
||||
)
|
||||
|
||||
// GetWebsocketInterfaceChannelOverride returns a new interface based channel
|
||||
@@ -47,3 +55,51 @@ func NewTestWebsocket() *stream.Websocket {
|
||||
Match: stream.NewMatch(),
|
||||
}
|
||||
}
|
||||
|
||||
// SkipTestIfCredentialsUnset is a test helper function checking if the
|
||||
// authenticated function can perform the required test.
|
||||
func SkipTestIfCredentialsUnset(t *testing.T, exch exchange.IBotExchange, canManipulateOrders ...bool) {
|
||||
t.Helper()
|
||||
|
||||
if len(canManipulateOrders) > 1 {
|
||||
t.Fatal("more than one canManipulateOrders boolean value has been supplied, please remove")
|
||||
}
|
||||
|
||||
areTestAPICredentialsSet := AreAPICredentialsSet(exch)
|
||||
supportsManipulatingOrders := len(canManipulateOrders) > 0
|
||||
allowedToManipulateOrders := supportsManipulatingOrders && canManipulateOrders[0]
|
||||
|
||||
if (areTestAPICredentialsSet && !supportsManipulatingOrders) ||
|
||||
(areTestAPICredentialsSet && allowedToManipulateOrders) {
|
||||
return
|
||||
}
|
||||
|
||||
message := []string{warningSkip}
|
||||
if !areTestAPICredentialsSet {
|
||||
message = append(message, warningKeys)
|
||||
}
|
||||
|
||||
if supportsManipulatingOrders && !allowedToManipulateOrders {
|
||||
message = append(message, warningManipulateOrders)
|
||||
}
|
||||
message = append(message, warningHowTo)
|
||||
t.Skip(strings.Join(message, ", "))
|
||||
}
|
||||
|
||||
// SkipTestIfCannotManipulateOrders will only skip if the credentials are set
|
||||
// correctly and can manipulate orders is set to false. It will continue normal
|
||||
// operations if credentials are not set, giving better code coverage.
|
||||
func SkipTestIfCannotManipulateOrders(t *testing.T, exch exchange.IBotExchange, canManipulateOrders bool) {
|
||||
t.Helper()
|
||||
|
||||
if !AreAPICredentialsSet(exch) || canManipulateOrders {
|
||||
return
|
||||
}
|
||||
|
||||
t.Skip(warningSkip + ", " + warningManipulateOrders)
|
||||
}
|
||||
|
||||
// AreAPICredentialsSet returns if the API credentials are set.
|
||||
func AreAPICredentialsSet(exch exchange.IBotExchange) bool {
|
||||
return exch.VerifyAPICredentials(exch.GetDefaultCredentials()) == nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user