mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-17 23:16:52 +00:00
* codebase: Rid base64/hex to string common funcs * codebase: Rid local scope variable usage and other improvements * codebase: Refactor currency pair usage across multiple exchanges - Updated HitBTC tests to use the new currency pair format. - Modified Kraken futures types to use currency.Pair instead of string for Symbol. - Adjusted Kraken wrapper methods to handle currency pairs correctly. - Refined OKX tests and types to utilize currency.Pair for instrument IDs. - Enhanced Poloniex tests to consistently use predefined currency pairs. - Streamlined order and orderbook tests to replace string pairs with currency.NewBTCUSD(). - Improved Yobit tests to utilize a standardized currency pair format. - Updated validator wrapper to use currency pairs directly instead of string conversions. * codebase: Use types.Number where possible * refactor: update PayoutFee type to types.Number for consistency * Refactor: Remove crypto functions to use standard library and other minor changes - Removed custom crypto functions for SHA256, SHA512, and MD5 from the common/crypto package. - Replaced usages of removed functions with standard library implementations in various files including: - cmd/websocket_client/main.go - engine/apiserver.go - exchanges/kraken/kraken.go - exchanges/lbank/lbank.go - exchanges/okx/okx_business_websocket.go - exchanges/kucoin/kucoin_websocket.go - gctscript/vm/vm.go - Updated tests to reflect changes in the crypto functions. - Renamed several functions for clarity, particularly in the context of order book updates across multiple exchanges. * refactor: replace assert with require for consistency in test assertions * refactor: Improve Binance futures candlestick test, standardise orderbook update function names and improve test parallelism * refactor: Replace require.Len with require.Equal for better output in TestGetFuturesKlineData
242 lines
4.5 KiB
Go
242 lines
4.5 KiB
Go
package common
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
gctorder "github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
|
"github.com/thrasher-corp/gocryptotrader/log"
|
|
)
|
|
|
|
func TestCanTransact(t *testing.T) {
|
|
t.Parallel()
|
|
for _, ti := range []struct {
|
|
side gctorder.Side
|
|
expected bool
|
|
}{
|
|
{
|
|
side: gctorder.UnknownSide,
|
|
expected: false,
|
|
},
|
|
{
|
|
side: gctorder.Buy,
|
|
expected: true,
|
|
},
|
|
{
|
|
side: gctorder.Sell,
|
|
expected: true,
|
|
},
|
|
{
|
|
side: gctorder.Bid,
|
|
expected: true,
|
|
},
|
|
{
|
|
side: gctorder.Ask,
|
|
expected: true,
|
|
},
|
|
{
|
|
// while anyside can work in GCT, it's a no for the backtester
|
|
side: gctorder.AnySide,
|
|
expected: false,
|
|
},
|
|
{
|
|
side: gctorder.Long,
|
|
expected: true,
|
|
},
|
|
{
|
|
side: gctorder.Short,
|
|
expected: true,
|
|
},
|
|
{
|
|
side: gctorder.ClosePosition,
|
|
expected: true,
|
|
},
|
|
{
|
|
side: gctorder.DoNothing,
|
|
expected: false,
|
|
},
|
|
{
|
|
side: gctorder.TransferredFunds,
|
|
expected: false,
|
|
},
|
|
{
|
|
side: gctorder.CouldNotBuy,
|
|
expected: false,
|
|
},
|
|
{
|
|
side: gctorder.CouldNotSell,
|
|
expected: false,
|
|
},
|
|
{
|
|
side: gctorder.CouldNotShort,
|
|
expected: false,
|
|
},
|
|
{
|
|
side: gctorder.CouldNotLong,
|
|
expected: false,
|
|
},
|
|
{
|
|
side: gctorder.CouldNotCloseShort,
|
|
expected: false,
|
|
},
|
|
{
|
|
side: gctorder.CouldNotCloseLong,
|
|
expected: false,
|
|
},
|
|
{
|
|
side: gctorder.MissingData,
|
|
expected: false,
|
|
},
|
|
} {
|
|
t.Run(ti.side.String(), func(t *testing.T) {
|
|
t.Parallel()
|
|
if CanTransact(ti.side) != ti.expected {
|
|
t.Errorf("received '%v' expected '%v'", ti.side, ti.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDataTypeConversion(t *testing.T) {
|
|
t.Parallel()
|
|
for _, ti := range []struct {
|
|
title string
|
|
dataType string
|
|
want int64
|
|
expectErr bool
|
|
}{
|
|
{
|
|
title: "Candle data type",
|
|
dataType: CandleStr,
|
|
want: DataCandle,
|
|
},
|
|
{
|
|
title: "Trade data type",
|
|
dataType: TradeStr,
|
|
want: DataTrade,
|
|
},
|
|
{
|
|
title: "Unknown data type",
|
|
dataType: "unknown",
|
|
want: 0,
|
|
expectErr: true,
|
|
},
|
|
} {
|
|
t.Run(ti.title, func(t *testing.T) {
|
|
t.Parallel()
|
|
got, err := DataTypeToInt(ti.dataType)
|
|
if ti.expectErr {
|
|
if err == nil {
|
|
t.Error("expected error")
|
|
}
|
|
} else {
|
|
if err != nil || got != ti.want {
|
|
t.Error(fmt.Errorf("%s: expected %d, got %d, err: %v", ti.dataType, ti.want, got, err))
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFitStringToLimit(t *testing.T) {
|
|
t.Parallel()
|
|
for _, tc := range []struct {
|
|
str string
|
|
sep string
|
|
limit int
|
|
expected string
|
|
upper bool
|
|
}{
|
|
{
|
|
str: "good",
|
|
sep: " ",
|
|
limit: 5,
|
|
expected: "GOOD ",
|
|
upper: true,
|
|
},
|
|
{
|
|
str: "negative limit",
|
|
sep: " ",
|
|
limit: -1,
|
|
expected: "negative limit",
|
|
},
|
|
{
|
|
str: "long spacer",
|
|
sep: "--",
|
|
limit: 14,
|
|
expected: "long spacer---",
|
|
},
|
|
{
|
|
str: "zero limit",
|
|
sep: "--",
|
|
limit: 0,
|
|
expected: "",
|
|
},
|
|
{
|
|
str: "over limit",
|
|
sep: "--",
|
|
limit: 6,
|
|
expected: "ove...",
|
|
},
|
|
{
|
|
str: "hi",
|
|
sep: " ",
|
|
limit: 1,
|
|
expected: "h",
|
|
},
|
|
} {
|
|
t.Run(tc.str, func(t *testing.T) {
|
|
t.Parallel()
|
|
assert.Equal(t, tc.expected, FitStringToLimit(tc.str, tc.sep, tc.limit, tc.upper))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLogo(t *testing.T) {
|
|
colourLogo := Logo()
|
|
if colourLogo == "" {
|
|
t.Error("expected a logo")
|
|
}
|
|
PurgeColours()
|
|
if len(colourLogo) == len(Logo()) {
|
|
t.Error("expected logo with colours removed")
|
|
}
|
|
}
|
|
|
|
func TestPurgeColours(t *testing.T) {
|
|
PurgeColours()
|
|
if CMDColours.Success != "" {
|
|
t.Error("expected purged colour")
|
|
}
|
|
}
|
|
|
|
func TestGenerateFileName(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := GenerateFileName("", "")
|
|
assert.ErrorIs(t, err, errCannotGenerateFileName)
|
|
|
|
_, err = GenerateFileName("hello", "")
|
|
assert.ErrorIs(t, err, errCannotGenerateFileName)
|
|
|
|
_, err = GenerateFileName("", "moto")
|
|
assert.ErrorIs(t, err, errCannotGenerateFileName)
|
|
|
|
_, err = GenerateFileName("hello", "moto")
|
|
assert.NoError(t, err)
|
|
|
|
name, err := GenerateFileName("......HELL0. + _", "moto.")
|
|
require.NoError(t, err, "GenerateFileName must not error")
|
|
assert.Equal(t, "hell0_.moto", name)
|
|
}
|
|
|
|
func TestRegisterBacktesterSubLoggers(t *testing.T) {
|
|
t.Parallel()
|
|
err := RegisterBacktesterSubLoggers()
|
|
assert.NoError(t, err)
|
|
|
|
err = RegisterBacktesterSubLoggers()
|
|
assert.ErrorIs(t, err, log.ErrSubLoggerAlreadyRegistered)
|
|
}
|