golangci-lint: Enable usetesting and unused linters (#1893)

* golangci-lint: Enable usetesting and unused linters

* tests: Improve assertions in various test cases for clarity and accuracy

* tests: Enhance error assertions in TestExecuteStrategyFromFile for improved clarity

* tests: Update assertions for improved clarity and accuracy

* tests: Replace assert with require for task count checks

* config/versions/v7: Replace context.Background() with t.Context()

* Bithumb: Centralise and consoliate testPair, relax UpdateTickers check

with some glorious Doom Eternal music

* Bithumb: Use UpdatePairsOnce and update remaining pair string

* Bithumb: Add UpdatePairsOnce to TestUpdateTickers
This commit is contained in:
Adrian Gallagher
2025-05-01 14:44:29 +10:00
committed by GitHub
parent c2d876d8b0
commit bea16af380
69 changed files with 3253 additions and 3681 deletions

View File

@@ -1,7 +1,6 @@
package engine
import (
"context"
"errors"
"os"
"slices"
@@ -380,7 +379,7 @@ func TestGetDefaultConfigurations(t *testing.T) {
t.Skipf("skipping %s unsupported", name)
}
defaultCfg, err := exchange.GetDefaultConfig(context.Background(), exch)
defaultCfg, err := exchange.GetDefaultConfig(t.Context(), exch)
require.NoError(t, err, "GetDefaultConfig must not error")
require.NotNil(t, defaultCfg)
assert.NotEmpty(t, defaultCfg.Name, "Name should not be empty")
@@ -480,7 +479,7 @@ func TestSetupExchanges(t *testing.T) {
exchLoader := func(exch exchange.IBotExchange) {
exch.SetDefaults()
exch.GetBase().Features.Supports.RESTCapabilities.AutoPairUpdates = false
cfg, err := exchange.GetDefaultConfig(context.Background(), exch)
cfg, err := exchange.GetDefaultConfig(t.Context(), exch)
require.NoError(t, err)
e.Config.Exchanges = append(e.Config.Exchanges, *cfg)
}

View File

@@ -1,7 +1,6 @@
package engine
import (
"context"
"errors"
"sync/atomic"
"testing"
@@ -277,7 +276,7 @@ func TestCheckEventCondition(t *testing.T) {
exch, err := em.NewExchangeByName(testExchange)
require.NoError(t, err, "NewExchangeByName must not error")
conf, err := exchange.GetDefaultConfig(context.Background(), exch)
conf, err := exchange.GetDefaultConfig(t.Context(), exch)
require.NoError(t, err, "GetDefaultConfig must not error")
require.NoError(t, exch.Setup(conf), "Setup must not error")
@@ -293,7 +292,7 @@ func TestCheckEventCondition(t *testing.T) {
assert.ErrorIs(t, err, ticker.ErrTickerNotFound)
m.m.Unlock()
_, err = exch.UpdateTicker(context.Background(), currency.NewPair(currency.BTC, currency.USD), asset.Spot)
_, err = exch.UpdateTicker(t.Context(), currency.NewPair(currency.BTC, currency.USD), asset.Spot)
require.NoError(t, err, "UpdateTicker must not error")
m.m.Lock()
@@ -311,7 +310,7 @@ func TestCheckEventCondition(t *testing.T) {
assert.ErrorIs(t, err, orderbook.ErrOrderbookNotFound)
m.m.Unlock()
_, err = exch.UpdateOrderbook(context.Background(), currency.NewPair(currency.BTC, currency.USD), asset.Spot)
_, err = exch.UpdateOrderbook(t.Context(), currency.NewPair(currency.BTC, currency.USD), asset.Spot)
require.NoError(t, err, "UpdateOrderbook must not error")
m.m.Lock()

View File

@@ -19,6 +19,7 @@ import (
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/thrasher-corp/gocryptotrader/common/convert"
"github.com/thrasher-corp/gocryptotrader/common/file"
"github.com/thrasher-corp/gocryptotrader/communications"
@@ -1021,27 +1022,20 @@ func TestGetCryptocurrencyDepositAddressesByExchange(t *testing.T) {
func TestGetExchangeCryptocurrencyDepositAddress(t *testing.T) {
t.Parallel()
e := createDepositEngine(&fakeDepositExchangeOpts{SupportsAuth: true, SupportsMultiChain: true})
_, err := e.GetExchangeCryptocurrencyDepositAddress(t.Context(), "non-existent", "", "", currency.BTC, false)
assert.ErrorIs(t, err, ErrExchangeNotFound)
const exchName = "fake"
if _, err := e.GetExchangeCryptocurrencyDepositAddress(context.Background(), "non-existent", "", "", currency.BTC, false); !errors.Is(err, ErrExchangeNotFound) {
t.Errorf("received %s, expected: %s", err, ErrExchangeNotFound)
}
r, err := e.GetExchangeCryptocurrencyDepositAddress(context.Background(), exchName, "", "", currency.BTC, false)
if err != nil {
t.Error(err)
}
if r.Address != "fakeaddr" {
t.Error("unexpected address")
}
r, err := e.GetExchangeCryptocurrencyDepositAddress(t.Context(), exchName, "", "", currency.BTC, false)
require.NoError(t, err, "GetExchangeCryptocurrencyDepositAddress must not error")
assert.Equal(t, "fakeaddr", r.Address, "Should return the correct r.Address")
e.DepositAddressManager = SetupDepositAddressManager()
if err := e.DepositAddressManager.Sync(e.GetAllExchangeCryptocurrencyDepositAddresses()); err != nil {
t.Fatal(err)
}
if _, err := e.GetExchangeCryptocurrencyDepositAddress(context.Background(), "meow", "", "", currency.BTC, false); !errors.Is(err, ErrExchangeNotFound) {
t.Errorf("received %s, expected: %s", err, ErrExchangeNotFound)
}
if _, err := e.GetExchangeCryptocurrencyDepositAddress(context.Background(), exchName, "", "", currency.BTC, false); err != nil {
t.Error(err)
}
err = e.DepositAddressManager.Sync(e.GetAllExchangeCryptocurrencyDepositAddresses())
assert.NoError(t, err, "Sync should not error")
_, err = e.GetExchangeCryptocurrencyDepositAddress(t.Context(), "meow", "", "", currency.BTC, false)
assert.ErrorIs(t, err, ErrExchangeNotFound)
_, err = e.GetExchangeCryptocurrencyDepositAddress(t.Context(), exchName, "", "", currency.BTC, false)
assert.NoError(t, err, "GetExchangeCryptocurrencyDepositAddress should not error")
}
func TestGetAllExchangeCryptocurrencyDepositAddresses(t *testing.T) {
@@ -1281,7 +1275,7 @@ func TestNewSupportedExchangeByName(t *testing.T) {
func TestNewExchangeByNameWithDefaults(t *testing.T) {
t.Parallel()
_, err := NewExchangeByNameWithDefaults(context.Background(), "moarunlikelymeow")
_, err := NewExchangeByNameWithDefaults(t.Context(), "moarunlikelymeow")
assert.ErrorIs(t, err, ErrExchangeNotFound, "Invalid exchange name should error")
for x := range exchange.Exchanges {
name := exchange.Exchanges[x]
@@ -1293,7 +1287,7 @@ func TestNewExchangeByNameWithDefaults(t *testing.T) {
if slices.Contains(unsupportedDefaultConfigExchanges, name) {
t.Skipf("skipping %s unsupported", name)
}
exch, err := NewExchangeByNameWithDefaults(context.Background(), name)
exch, err := NewExchangeByNameWithDefaults(t.Context(), name)
if assert.NoError(t, err, "NewExchangeByNameWithDefaults should not error") {
assert.Equal(t, name, strings.ToLower(exch.GetName()), "Should get correct exchange name")
}

View File

@@ -261,7 +261,7 @@ func OrdersSetup(t *testing.T) *OrderManager {
t.Fatal(err)
}
cfg, err := exchange.GetDefaultConfig(context.Background(), exch)
cfg, err := exchange.GetDefaultConfig(t.Context(), exch)
if err != nil {
t.Fatal(err)
}
@@ -420,31 +420,31 @@ func TestStore_modifyOrder(t *testing.T) {
func TestCancelOrder(t *testing.T) {
m := OrdersSetup(t)
err := m.Cancel(context.Background(), nil)
err := m.Cancel(t.Context(), nil)
if err == nil {
t.Error("Expected error due to empty order")
}
err = m.Cancel(context.Background(), &order.Cancel{})
err = m.Cancel(t.Context(), &order.Cancel{})
if err == nil {
t.Error("Expected error due to empty order")
}
err = m.Cancel(context.Background(), &order.Cancel{
err = m.Cancel(t.Context(), &order.Cancel{
Exchange: testExchange,
})
if err == nil {
t.Error("Expected error due to no order ID")
}
err = m.Cancel(context.Background(), &order.Cancel{
err = m.Cancel(t.Context(), &order.Cancel{
OrderID: "ID",
})
if err == nil {
t.Error("Expected error due to no Exchange")
}
err = m.Cancel(context.Background(), &order.Cancel{
err = m.Cancel(t.Context(), &order.Cancel{
OrderID: "ID",
Exchange: testExchange,
AssetType: asset.Binary,
@@ -463,7 +463,7 @@ func TestCancelOrder(t *testing.T) {
t.Error(err)
}
err = m.Cancel(context.Background(), &order.Cancel{
err = m.Cancel(t.Context(), &order.Cancel{
OrderID: "Unknown",
Exchange: testExchange,
AssetType: asset.Spot,
@@ -479,7 +479,7 @@ func TestCancelOrder(t *testing.T) {
AssetType: asset.Spot,
Pair: btcusdPair,
}
err = m.Cancel(context.Background(), cancel)
err = m.Cancel(t.Context(), cancel)
if !errors.Is(err, nil) {
t.Errorf("error '%v', expected '%v'", err, nil)
}
@@ -491,13 +491,13 @@ func TestCancelOrder(t *testing.T) {
func TestGetOrderInfo(t *testing.T) {
m := OrdersSetup(t)
_, err := m.GetOrderInfo(context.Background(), "", "", currency.EMPTYPAIR, asset.Empty)
_, err := m.GetOrderInfo(t.Context(), "", "", currency.EMPTYPAIR, asset.Empty)
if err == nil {
t.Error("Expected error due to empty order")
}
var result order.Detail
result, err = m.GetOrderInfo(context.Background(),
result, err = m.GetOrderInfo(t.Context(),
testExchange, "1337", currency.EMPTYPAIR, asset.Empty)
if err != nil {
t.Error(err)
@@ -506,7 +506,7 @@ func TestGetOrderInfo(t *testing.T) {
t.Error("unexpected order returned")
}
result, err = m.GetOrderInfo(context.Background(),
result, err = m.GetOrderInfo(t.Context(),
testExchange, "1337", currency.EMPTYPAIR, asset.Empty)
if err != nil {
t.Error(err)
@@ -527,7 +527,7 @@ func TestCancelAllOrders(t *testing.T) {
t.Error(err)
}
m.CancelAllOrders(context.Background(), []exchange.IBotExchange{})
m.CancelAllOrders(t.Context(), []exchange.IBotExchange{})
checkDeets, err := m.orderStore.getByExchangeAndID(testExchange, "TestCancelAllOrders")
if err != nil {
t.Fatal(err)
@@ -541,7 +541,7 @@ func TestCancelAllOrders(t *testing.T) {
t.Fatal(err)
}
m.CancelAllOrders(context.Background(), []exchange.IBotExchange{exch})
m.CancelAllOrders(t.Context(), []exchange.IBotExchange{exch})
checkDeets, err = m.orderStore.getByExchangeAndID(testExchange, "TestCancelAllOrders")
if err != nil {
t.Fatal(err)
@@ -554,17 +554,17 @@ func TestCancelAllOrders(t *testing.T) {
func TestSubmit(t *testing.T) {
m := OrdersSetup(t)
_, err := m.Submit(context.Background(), nil)
_, err := m.Submit(t.Context(), nil)
require.ErrorIs(t, err, errNilOrder)
o := &order.Submit{Type: order.Market}
_, err = m.Submit(context.Background(), o)
_, err = m.Submit(t.Context(), o)
if err == nil {
t.Error("Expected error from empty exchange")
}
o.Exchange = testExchange
_, err = m.Submit(context.Background(), o)
_, err = m.Submit(t.Context(), o)
if err == nil {
t.Error("Expected error from validation")
}
@@ -576,20 +576,20 @@ func TestSubmit(t *testing.T) {
o.Side = order.Buy
o.Amount = 1
o.Price = 1
_, err = m.Submit(context.Background(), o)
_, err = m.Submit(t.Context(), o)
if err == nil {
t.Error("Expected fail due to order market type is not allowed")
}
m.cfg.AllowMarketOrders = true
m.cfg.LimitAmount = 1
o.Amount = 2
_, err = m.Submit(context.Background(), o)
_, err = m.Submit(t.Context(), o)
if err == nil {
t.Error("Expected fail due to order limit exceeds allowed limit")
}
m.cfg.LimitAmount = 0
m.cfg.AllowedExchanges = []string{"fake"}
_, err = m.Submit(context.Background(), o)
_, err = m.Submit(t.Context(), o)
if err == nil {
t.Error("Expected fail due to order exchange not found in allowed list")
}
@@ -601,13 +601,13 @@ func TestSubmit(t *testing.T) {
m.cfg.AllowedExchanges = nil
m.cfg.AllowedPairs = currency.Pairs{failPair}
_, err = m.Submit(context.Background(), o)
_, err = m.Submit(t.Context(), o)
if err == nil {
t.Error("Expected fail due to order pair not found in allowed list")
}
m.cfg.AllowedPairs = nil
_, err = m.Submit(context.Background(), o)
_, err = m.Submit(t.Context(), o)
if !errors.Is(err, exchange.ErrAuthenticationSupportNotEnabled) {
t.Errorf("received: %v but expected: %v", err, exchange.ErrAuthenticationSupportNotEnabled)
}
@@ -682,7 +682,7 @@ func TestOrderManager_Modify(t *testing.T) {
t.Error(err)
}
resp, err := m.Modify(context.Background(), &mod)
resp, err := m.Modify(t.Context(), &mod)
if expectError {
if err == nil {
t.Fatal("Expected error")

File diff suppressed because it is too large Load Diff

View File

@@ -1,7 +1,6 @@
package engine
import (
"context"
"errors"
"sync"
"testing"
@@ -24,7 +23,7 @@ func withdrawManagerTestHelper(t *testing.T) (*ExchangeManager, *portfolioManage
t.Helper()
em := NewExchangeManager()
b := new(okx.Okx)
cfg, err := exchange.GetDefaultConfig(context.Background(), b)
cfg, err := exchange.GetDefaultConfig(t.Context(), b)
if err != nil {
t.Fatal(err)
}
@@ -80,7 +79,7 @@ func TestSubmitWithdrawal(t *testing.T) {
Bank: bank,
},
}
_, err = m.SubmitWithdrawal(context.Background(), req)
_, err = m.SubmitWithdrawal(t.Context(), req)
if !errors.Is(err, common.ErrFunctionNotSupported) {
t.Errorf("received %v, expected %v", err, common.ErrFunctionNotSupported)
}
@@ -88,7 +87,7 @@ func TestSubmitWithdrawal(t *testing.T) {
req.Type = withdraw.Crypto
req.Currency = currency.BTC
req.Crypto.Address = "1337"
_, err = m.SubmitWithdrawal(context.Background(), req)
_, err = m.SubmitWithdrawal(t.Context(), req)
if !errors.Is(err, withdraw.ErrStrAddressNotWhiteListed) {
t.Errorf("received %v, expected %v", err, withdraw.ErrStrAddressNotWhiteListed)
}
@@ -106,24 +105,24 @@ func TestSubmitWithdrawal(t *testing.T) {
if !errors.Is(err, nil) {
t.Errorf("received %v, expected %v", err, nil)
}
_, err = m.SubmitWithdrawal(context.Background(), req)
_, err = m.SubmitWithdrawal(t.Context(), req)
if !errors.Is(err, withdraw.ErrStrExchangeNotSupportedByAddress) {
t.Errorf("received %v, expected %v", err, withdraw.ErrStrExchangeNotSupportedByAddress)
}
adds[0].SupportedExchanges = withdrawManagerTestExchangeName
_, err = m.SubmitWithdrawal(context.Background(), req)
_, err = m.SubmitWithdrawal(t.Context(), req)
if !errors.Is(err, exchange.ErrAuthenticationSupportNotEnabled) {
t.Errorf("received '%v', expected '%v'", err, exchange.ErrAuthenticationSupportNotEnabled)
}
_, err = m.SubmitWithdrawal(context.Background(), nil)
_, err = m.SubmitWithdrawal(t.Context(), nil)
if !errors.Is(err, withdraw.ErrRequestCannotBeNil) {
t.Errorf("received %v, expected %v", err, withdraw.ErrRequestCannotBeNil)
}
m.isDryRun = true
_, err = m.SubmitWithdrawal(context.Background(), req)
_, err = m.SubmitWithdrawal(t.Context(), req)
if !errors.Is(err, nil) {
t.Errorf("received %v, expected %v", err, nil)
}