mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-28 15:10:32 +00:00
accounts: Move to instance methods, fix races and isolate tests (#1923)
* Bybit: Fix race in TestUpdateAccountInfo and TestWSHandleData * DriveBy rename TestWSHandleData * This doesn't address running with -race=2+ due to the singleton * Accounts: Add account.GetService() * exchange: Assertify TestSetupDefaults * Exchanges: Add account.Service override for testing * Exchanges: Remove duplicate IsWebsocketEnabled test from TestSetupDefaults * Dispatch: Replace nil checks with NilGuard * Engine: Remove deprecated printAccountHoldingsChangeSummary * Dispatcher: Add EnsureRunning method * Accounts: Move singleton accounts service to exchange Accounts * Move singleton accounts service to exchange Accounts This maintains the concept of a global store, whilst allowing exchanges to override it when needed, particularly for testing. APIServer: * Remove getAllActiveAccounts from apiserver Deprecated apiserver only thing using this, so remove it instead of updating it * Update comment for UpdateAccountBalances everywhere * Docs: Add punctuation to function comments * Bybit: Coverage for wsProcessWalletPushData Save
This commit is contained in:
@@ -9,8 +9,8 @@ import (
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/engine"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchange/accounts"
|
||||
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/deposit"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
@@ -130,16 +130,16 @@ func (e Exchange) CancelOrder(ctx context.Context, exch, orderID string, cp curr
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// AccountInformation returns account information (balance etc) for requested exchange
|
||||
func (e Exchange) AccountInformation(ctx context.Context, exch string, assetType asset.Item) (account.Holdings, error) {
|
||||
// AccountBalances returns account balances for requested exchange
|
||||
func (e Exchange) AccountBalances(ctx context.Context, exch string, assetType asset.Item) (accounts.SubAccounts, error) {
|
||||
ex, err := e.GetExchange(exch)
|
||||
if err != nil {
|
||||
return account.Holdings{}, err
|
||||
return accounts.SubAccounts{}, err
|
||||
}
|
||||
|
||||
accountInfo, err := ex.GetCachedAccountInfo(ctx, assetType)
|
||||
accountInfo, err := ex.GetCachedSubAccounts(ctx, assetType)
|
||||
if err != nil {
|
||||
return account.Holdings{}, err
|
||||
return accounts.SubAccounts{}, err
|
||||
}
|
||||
|
||||
return accountInfo, nil
|
||||
|
||||
@@ -119,11 +119,11 @@ func TestExchange_Pairs(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestExchange_AccountInformation(t *testing.T) {
|
||||
func TestExchange_AccountBalances(t *testing.T) {
|
||||
if !configureExchangeKeys() {
|
||||
t.Skip("no exchange configured test skipped")
|
||||
}
|
||||
_, err := exchangeTest.AccountInformation(t.Context(),
|
||||
_, err := exchangeTest.AccountBalances(t.Context(),
|
||||
exchName, asset.Spot)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -159,14 +159,14 @@ func TestExchangePairs(t *testing.T) {
|
||||
assert.ErrorIs(t, err, objects.ErrWrongNumArguments)
|
||||
}
|
||||
|
||||
func TestExchangeAccountInfo(t *testing.T) {
|
||||
func TestExchangeAccountBalances(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := gct.ExchangeAccountInfo()
|
||||
_, err := gct.ExchangeAccountBalances()
|
||||
require.ErrorIs(t, err, objects.ErrWrongNumArguments)
|
||||
obj, err := gct.ExchangeAccountInfo(ctx, exch, assetType)
|
||||
obj, err := gct.ExchangeAccountBalances(ctx, exch, assetType)
|
||||
require.NoError(t, err)
|
||||
rString, ok := objects.ToString(obj)
|
||||
require.True(t, ok, "ExchangeAccountInfo return value must return correctly from objects.ToString")
|
||||
require.True(t, ok, "ExchangeAccountBalances return value must return correctly from objects.ToString")
|
||||
require.Contains(t, rString, "Bitstamp REST or Websocket authentication support is not enabled")
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/core"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchange/accounts"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/deposit"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
@@ -184,32 +184,29 @@ func (w Wrapper) CancelOrder(_ context.Context, exch, orderid string, cp currenc
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// AccountInformation validator for test execution/scripts
|
||||
func (w Wrapper) AccountInformation(_ context.Context, exch string, assetType asset.Item) (account.Holdings, error) {
|
||||
// AccountBalances validator for test execution/scripts
|
||||
func (w Wrapper) AccountBalances(_ context.Context, exch string, assetType asset.Item) (accounts.SubAccounts, error) {
|
||||
if exch == exchError.String() {
|
||||
return account.Holdings{}, errTestFailed
|
||||
return nil, errTestFailed
|
||||
}
|
||||
|
||||
return account.Holdings{
|
||||
Exchange: exch,
|
||||
Accounts: []account.SubAccount{
|
||||
{
|
||||
ID: exch,
|
||||
AssetType: assetType,
|
||||
Currencies: []account.Balance{
|
||||
{
|
||||
Currency: currency.Code{
|
||||
Item: ¤cy.Item{
|
||||
ID: 0,
|
||||
FullName: "Bitcoin",
|
||||
Symbol: "BTC",
|
||||
Role: 1,
|
||||
AssocChain: "",
|
||||
},
|
||||
},
|
||||
Total: 100,
|
||||
Hold: 0,
|
||||
},
|
||||
c := currency.Code{
|
||||
Item: ¤cy.Item{
|
||||
ID: 0,
|
||||
FullName: "Bitcoin",
|
||||
Symbol: "BTC",
|
||||
Role: 1,
|
||||
AssocChain: "",
|
||||
},
|
||||
}
|
||||
return accounts.SubAccounts{
|
||||
{
|
||||
ID: "subacct1",
|
||||
AssetType: assetType,
|
||||
Balances: accounts.CurrencyBalances{
|
||||
c: accounts.Balance{
|
||||
Currency: c,
|
||||
Total: 100,
|
||||
Hold: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
@@ -60,20 +62,14 @@ func TestWrapper_IsEnabled(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapper_AccountInformation(t *testing.T) {
|
||||
func TestWrapperAccountBalances(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, err := testWrapper.AccountInformation(t.Context(),
|
||||
exchName, asset.Spot)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err := testWrapper.AccountBalances(t.Context(), exchName, asset.Spot)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = testWrapper.AccountInformation(t.Context(),
|
||||
exchError.String(), asset.Spot)
|
||||
if err == nil {
|
||||
t.Fatal("expected AccountInformation to return error on invalid name")
|
||||
}
|
||||
_, err = testWrapper.AccountBalances(t.Context(), exchError.String(), asset.Spot)
|
||||
assert.ErrorIs(t, err, errTestFailed)
|
||||
}
|
||||
|
||||
func TestWrapper_CancelOrder(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user