mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-02 07:26:53 +00:00
codebase: Cleanup various things (#1935)
* 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
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"math/rand"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -143,7 +144,7 @@ func TestKlineShort(t *testing.T) {
|
||||
|
||||
func TestDurationToWord(t *testing.T) {
|
||||
t.Parallel()
|
||||
testCases := []struct {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
interval Interval
|
||||
}{
|
||||
@@ -178,9 +179,11 @@ func TestDurationToWord(t *testing.T) {
|
||||
{"sixmonth", SixMonth},
|
||||
{"oneyear", OneYear},
|
||||
{"notfound", Interval(time.Hour * 1337)},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
require.Equal(t, tc.name, durationToWord(tc.interval))
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert.Equal(t, tc.name, strings.ToLower(tc.interval.Word()))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,125 +196,98 @@ func TestTotalCandlesPerInterval(t *testing.T) {
|
||||
start := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
end := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
for _, tc := range []struct {
|
||||
interval Interval
|
||||
expected uint64
|
||||
}{
|
||||
{
|
||||
"FifteenSecond",
|
||||
FifteenSecond,
|
||||
2102400,
|
||||
},
|
||||
{
|
||||
"OneMin",
|
||||
OneMin,
|
||||
525600,
|
||||
},
|
||||
{
|
||||
"ThreeMin",
|
||||
ThreeMin,
|
||||
175200,
|
||||
},
|
||||
{
|
||||
"FiveMin",
|
||||
FiveMin,
|
||||
105120,
|
||||
},
|
||||
{
|
||||
"TenMin",
|
||||
TenMin,
|
||||
52560,
|
||||
},
|
||||
{
|
||||
"FifteenMin",
|
||||
FifteenMin,
|
||||
35040,
|
||||
},
|
||||
{
|
||||
"ThirtyMin",
|
||||
ThirtyMin,
|
||||
17520,
|
||||
},
|
||||
{
|
||||
"OneHour",
|
||||
OneHour,
|
||||
8760,
|
||||
},
|
||||
{
|
||||
"TwoHour",
|
||||
TwoHour,
|
||||
4380,
|
||||
},
|
||||
{
|
||||
"FourHour",
|
||||
FourHour,
|
||||
2190,
|
||||
},
|
||||
{
|
||||
"SixHour",
|
||||
SixHour,
|
||||
1460,
|
||||
},
|
||||
{
|
||||
"EightHour",
|
||||
OneHour * 8,
|
||||
1095,
|
||||
},
|
||||
{
|
||||
"TwelveHour",
|
||||
TwelveHour,
|
||||
730,
|
||||
},
|
||||
{
|
||||
"OneDay",
|
||||
OneDay,
|
||||
365,
|
||||
},
|
||||
{
|
||||
"ThreeDay",
|
||||
ThreeDay,
|
||||
121,
|
||||
},
|
||||
{
|
||||
"FiveDay",
|
||||
FiveDay,
|
||||
73,
|
||||
},
|
||||
{
|
||||
"FifteenDay",
|
||||
FifteenDay,
|
||||
24,
|
||||
},
|
||||
{
|
||||
"OneWeek",
|
||||
OneWeek,
|
||||
52,
|
||||
},
|
||||
{
|
||||
"TwoWeek",
|
||||
TwoWeek,
|
||||
26,
|
||||
},
|
||||
{
|
||||
"OneMonth",
|
||||
OneMonth,
|
||||
12,
|
||||
},
|
||||
{
|
||||
"OneYear",
|
||||
OneYear,
|
||||
1,
|
||||
},
|
||||
}
|
||||
for x := range testCases {
|
||||
test := testCases[x]
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
} {
|
||||
t.Run(tc.interval.String(), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
v := TotalCandlesPerInterval(start, end, test.interval)
|
||||
if v != test.expected {
|
||||
t.Fatalf("%v: received %v expected %v", test.name, v, test.expected)
|
||||
}
|
||||
assert.Equal(t, tc.expected, TotalCandlesPerInterval(start, end, tc.interval))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -409,7 +385,7 @@ func setupTest(t *testing.T) {
|
||||
func TestStoreInDatabase(t *testing.T) {
|
||||
setupTest(t)
|
||||
|
||||
testCases := []struct {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
config *database.Config
|
||||
seedDB func(bool) error
|
||||
@@ -429,54 +405,31 @@ func TestStoreInDatabase(t *testing.T) {
|
||||
},
|
||||
seedDB: seedDB,
|
||||
},
|
||||
}
|
||||
|
||||
for x := range testCases {
|
||||
test := testCases[x]
|
||||
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if !testhelpers.CheckValidConfig(&test.config.ConnectionDetails) {
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if !testhelpers.CheckValidConfig(&tc.config.ConnectionDetails) {
|
||||
t.Skip("database not configured skipping test")
|
||||
}
|
||||
|
||||
dbConn, err := testhelpers.ConnectToDatabase(test.config)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dbConn, err := testhelpers.ConnectToDatabase(tc.config)
|
||||
require.NoError(t, err)
|
||||
|
||||
if test.seedDB != nil {
|
||||
err = test.seedDB(false)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if tc.seedDB != nil {
|
||||
require.NoError(t, tc.seedDB(false))
|
||||
}
|
||||
|
||||
_, ohlcvData, err := genOHCLVData()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
r, err := StoreInDatabase(&ohlcvData, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if r != 365 {
|
||||
t.Fatalf("unexpected number inserted: %v", r)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, uint64(365), r)
|
||||
|
||||
r, err = StoreInDatabase(&ohlcvData, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if r != 365 {
|
||||
t.Fatalf("unexpected number inserted: %v", r)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, uint64(365), r)
|
||||
|
||||
err = testhelpers.CloseDatabase(dbConn)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -489,7 +442,7 @@ func TestStoreInDatabase(t *testing.T) {
|
||||
func TestLoadFromDatabase(t *testing.T) {
|
||||
setupTest(t)
|
||||
|
||||
testCases := []struct {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
config *database.Config
|
||||
seedDB func(bool) error
|
||||
@@ -509,46 +462,26 @@ func TestLoadFromDatabase(t *testing.T) {
|
||||
},
|
||||
seedDB: seedDB,
|
||||
},
|
||||
}
|
||||
|
||||
for x := range testCases {
|
||||
test := testCases[x]
|
||||
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if !testhelpers.CheckValidConfig(&test.config.ConnectionDetails) {
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if !testhelpers.CheckValidConfig(&tc.config.ConnectionDetails) {
|
||||
t.Skip("database not configured skipping test")
|
||||
}
|
||||
|
||||
dbConn, err := testhelpers.ConnectToDatabase(test.config)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dbConn, err := testhelpers.ConnectToDatabase(tc.config)
|
||||
require.NoError(t, err)
|
||||
|
||||
if test.seedDB != nil {
|
||||
err = test.seedDB(true)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
p, err := currency.NewPairFromString("BTCUSDT")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
if tc.seedDB != nil {
|
||||
require.NoError(t, tc.seedDB(true))
|
||||
}
|
||||
start := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
end := start.AddDate(1, 0, 0)
|
||||
ret, err := LoadFromDatabase(testExchanges[0].Name, p, asset.Spot, OneDay, start, end)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ret.Exchange != testExchanges[0].Name {
|
||||
t.Fatalf("incorrect data returned: %v", ret.Exchange)
|
||||
}
|
||||
ret, err := LoadFromDatabase(testExchanges[0].Name, currency.NewBTCUSDT(), asset.Spot, OneDay, start, end)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, ret.Exchange, testExchanges[0].Name)
|
||||
|
||||
err = testhelpers.CloseDatabase(dbConn)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user