Files
gocryptotrader/exchanges/gateio/gateio_websocket_request_spot_test.go
Samuael A. 3f534a15f1 cmd/exchange_template, exchanges: Update templates and propogate to exchanges (#1777)
* Added TimeInForce type and updated related files

* Linter issue fix and minor coinbasepro type update

* Bitrex consts update

* added unit test and minor changes in bittrex

* Unit tests update

* Fix minor linter issues

* Update TestStringToTimeInForce unit test

* Exchange test template change

* A different approach

* fix conflict with gateio timeInForce

* minor exchange template update

* Minor fix to test_files template

* Update order tests

* Complete updating the order unit tests

* Updating exchange wrapper and test template files

* update kucoin and deribit wrapper to match the time in force change

* minor comment update

* fix time-in-force related test errors

* linter issue fix

* ADD_NEW_EXCHANGE documentation update

* time in force constants, functions and unit tests update

* shift tif policies to TimeInForce

* Update time-in-force, related functions, and unit tests

* fix linter issue and time-in-force processing

* added a good till crossing tif value

* order type fix and fix related tim-in-force entries

* update time-in-force unmarshaling and unit test

* consistency guideline added

* fix time-in-force error in gateio

* linter issue fix

* update based on review comments

* add unit test and fix missing issues

* minor fix and added benchmark unit test

* change GTT to GTC for limit

* fix linter issue

* added time-in-force value to place order param

* fix minor issues based on review comment and move tif code to separate files

* update on exchanges linked to time-in-force

* resolve missing review comments

* minor linter issues fix

* added time-in-force handler and update timeInForce parametered endpoint

* minor fixes based on review

* nits fix

* update based on review

* linter fix

* rm getTimeInForce func and minor change to time-in-force

* minor change

* update based on review comments

* wrappers and time-in-force calling approach

* minor change

* update gateio string to timeInForce conversion and unit test

* update exchange template

* update wrapper template file

* policy comments, and template files update

* rename all exchange types name to Exchange

* update on template files and template generation

* templates and generation code and other updates

* linter issue fix

* added subscriptions and websocket templates

* update ADD_NEW_EXCHANGE.md with recent binance functions and implementations

* rename template files and update unit tests

* minor template and unit test fix

* rename templates and fix on unit tests

* update on template files and documentation

* removed unnecessary tag fix and update templates

* fix Add_NEW_EXCHANGE.md doc file

* formatting, comments, and error checks update on template files

* rename exchange receivers to e and ex for consistency

* rename unit test exchange receiver and minor updates

* linter issues fix

* fix deribit issue and minor style update

* fix test issues caused by receiver change

* raname local variables exchange declaration variables

* update templates comments

* update templates and related comments

* renamed ex to e

* update template comments

* toggle WS to false to improve coverage

* template comments update

* added test coverage to Ws enabled and minor changes

---------

Co-authored-by: Samuel Reid <43227667+cranktakular@users.noreply.github.com>
2025-07-17 10:46:36 +10:00

237 lines
8.0 KiB
Go

package gateio
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues"
testexch "github.com/thrasher-corp/gocryptotrader/internal/testing/exchange"
)
func TestWebsocketLogin(t *testing.T) {
t.Parallel()
err := e.websocketLogin(t.Context(), nil, "")
require.ErrorIs(t, err, common.ErrNilPointer)
sharedtestvalues.SkipTestIfCredentialsUnset(t, e, canManipulateRealOrders)
g := newExchangeWithWebsocket(t, asset.Spot)
c, err := g.Websocket.GetConnection(asset.Spot)
require.NoError(t, err)
err = g.websocketLogin(t.Context(), c, "")
require.ErrorIs(t, err, errChannelEmpty)
err = g.websocketLogin(t.Context(), c, "spot.login")
require.NoError(t, err)
}
func TestWebsocketSpotSubmitOrder(t *testing.T) {
t.Parallel()
_, err := e.WebsocketSpotSubmitOrder(t.Context(), &CreateOrderRequest{})
require.ErrorIs(t, err, currency.ErrCurrencyPairEmpty)
out := &CreateOrderRequest{CurrencyPair: currency.NewPair(currency.NewCode("GT"), currency.USDT).Format(currency.PairFormat{Uppercase: true, Delimiter: "_"})}
_, err = e.WebsocketSpotSubmitOrder(t.Context(), out)
require.ErrorIs(t, err, order.ErrSideIsInvalid)
out.Side = strings.ToLower(order.Sell.String())
_, err = e.WebsocketSpotSubmitOrder(t.Context(), out)
require.ErrorIs(t, err, errInvalidAmount)
out.Amount = 1
out.Type = "limit"
_, err = e.WebsocketSpotSubmitOrder(t.Context(), out)
require.ErrorIs(t, err, errInvalidPrice)
out.Price = 100
sharedtestvalues.SkipTestIfCredentialsUnset(t, e, canManipulateRealOrders)
g := newExchangeWithWebsocket(t, asset.Spot)
got, err := g.WebsocketSpotSubmitOrder(t.Context(), out)
require.NoError(t, err)
require.NotEmpty(t, got)
}
func TestWebsocketSpotSubmitOrders(t *testing.T) {
t.Parallel()
_, err := e.WebsocketSpotSubmitOrders(t.Context())
require.ErrorIs(t, err, errOrdersEmpty)
out := &CreateOrderRequest{}
_, err = e.WebsocketSpotSubmitOrders(t.Context(), out)
require.ErrorIs(t, err, currency.ErrCurrencyPairEmpty)
out.CurrencyPair = currency.NewBTCUSDT()
_, err = e.WebsocketSpotSubmitOrders(t.Context(), out)
require.ErrorIs(t, err, order.ErrSideIsInvalid)
out.Side = strings.ToLower(order.Buy.String())
_, err = e.WebsocketSpotSubmitOrders(t.Context(), out)
require.ErrorIs(t, err, errInvalidAmount)
out.Amount = 0.0003
out.Type = "limit"
_, err = e.WebsocketSpotSubmitOrders(t.Context(), out)
require.ErrorIs(t, err, errInvalidPrice)
out.Price = 20000
sharedtestvalues.SkipTestIfCredentialsUnset(t, e, canManipulateRealOrders)
g := newExchangeWithWebsocket(t, asset.Spot)
// test single order
got, err := g.WebsocketSpotSubmitOrders(t.Context(), out)
require.NoError(t, err)
require.NotEmpty(t, got)
// test batch orders
got, err = g.WebsocketSpotSubmitOrders(t.Context(), out, out)
require.NoError(t, err)
require.NotEmpty(t, got)
}
func TestWebsocketSpotCancelOrder(t *testing.T) {
t.Parallel()
_, err := e.WebsocketSpotCancelOrder(t.Context(), "", currency.EMPTYPAIR, "")
require.ErrorIs(t, err, order.ErrOrderIDNotSet)
_, err = e.WebsocketSpotCancelOrder(t.Context(), "1337", currency.EMPTYPAIR, "")
require.ErrorIs(t, err, currency.ErrCurrencyPairEmpty)
sharedtestvalues.SkipTestIfCredentialsUnset(t, e, canManipulateRealOrders)
g := newExchangeWithWebsocket(t, asset.Spot)
got, err := g.WebsocketSpotCancelOrder(t.Context(), "644913098758", BTCUSDT, "")
require.NoError(t, err)
require.NotEmpty(t, got)
}
func TestWebsocketSpotCancelAllOrdersByIDs(t *testing.T) {
t.Parallel()
_, err := e.WebsocketSpotCancelAllOrdersByIDs(t.Context(), []WebsocketOrderBatchRequest{})
require.ErrorIs(t, err, errNoOrdersToCancel)
out := WebsocketOrderBatchRequest{}
_, err = e.WebsocketSpotCancelAllOrdersByIDs(t.Context(), []WebsocketOrderBatchRequest{out})
require.ErrorIs(t, err, order.ErrOrderIDNotSet)
out.OrderID = "1337"
_, err = e.WebsocketSpotCancelAllOrdersByIDs(t.Context(), []WebsocketOrderBatchRequest{out})
require.ErrorIs(t, err, currency.ErrCurrencyPairEmpty)
out.Pair = BTCUSDT
sharedtestvalues.SkipTestIfCredentialsUnset(t, e, canManipulateRealOrders)
g := newExchangeWithWebsocket(t, asset.Spot)
out.OrderID = "644913101755"
got, err := g.WebsocketSpotCancelAllOrdersByIDs(t.Context(), []WebsocketOrderBatchRequest{out})
require.NoError(t, err)
require.NotEmpty(t, got)
}
func TestWebsocketSpotCancelAllOrdersByPair(t *testing.T) {
t.Parallel()
_, err := e.WebsocketSpotCancelAllOrdersByPair(t.Context(), currency.NewPairWithDelimiter("LTC", "USDT", "_"), 0, "")
require.ErrorIs(t, err, order.ErrSideIsInvalid)
sharedtestvalues.SkipTestIfCredentialsUnset(t, e, canManipulateRealOrders)
g := newExchangeWithWebsocket(t, asset.Spot)
got, err := g.WebsocketSpotCancelAllOrdersByPair(t.Context(), currency.EMPTYPAIR, order.Buy, "")
require.NoError(t, err)
require.NotEmpty(t, got)
}
func TestWebsocketSpotAmendOrder(t *testing.T) {
t.Parallel()
_, err := e.WebsocketSpotAmendOrder(t.Context(), nil)
require.ErrorIs(t, err, common.ErrNilPointer)
amend := &WebsocketAmendOrder{}
_, err = e.WebsocketSpotAmendOrder(t.Context(), amend)
require.ErrorIs(t, err, order.ErrOrderIDNotSet)
amend.OrderID = "1337"
_, err = e.WebsocketSpotAmendOrder(t.Context(), amend)
require.ErrorIs(t, err, currency.ErrCurrencyPairEmpty)
amend.Pair = BTCUSDT
_, err = e.WebsocketSpotAmendOrder(t.Context(), amend)
require.ErrorIs(t, err, errInvalidAmount)
amend.Amount = "0.0004"
sharedtestvalues.SkipTestIfCredentialsUnset(t, e, canManipulateRealOrders)
g := newExchangeWithWebsocket(t, asset.Spot)
amend.OrderID = "645029162673"
got, err := g.WebsocketSpotAmendOrder(t.Context(), amend)
require.NoError(t, err)
require.NotEmpty(t, got)
}
func TestWebsocketSpotGetOrderStatus(t *testing.T) {
t.Parallel()
_, err := e.WebsocketSpotGetOrderStatus(t.Context(), "", currency.EMPTYPAIR, "")
require.ErrorIs(t, err, order.ErrOrderIDNotSet)
_, err = e.WebsocketSpotGetOrderStatus(t.Context(), "1337", currency.EMPTYPAIR, "")
require.ErrorIs(t, err, currency.ErrCurrencyPairEmpty)
sharedtestvalues.SkipTestIfCredentialsUnset(t, e, canManipulateRealOrders)
testexch.UpdatePairsOnce(t, e)
g := newExchangeWithWebsocket(t, asset.Spot)
got, err := g.WebsocketSpotGetOrderStatus(t.Context(), "644999650452", BTCUSDT, "")
require.NoError(t, err)
require.NotEmpty(t, got)
}
// getWebsocketInstance returns a websocket instance copy for testing.
// This restricts the pairs to a single pair per asset type to reduce test time.
func newExchangeWithWebsocket(t *testing.T, a asset.Item) *Exchange {
t.Helper()
if apiKey == "" || apiSecret == "" {
t.Skip()
}
e := new(Exchange) //nolint:govet // Intentional shadow
require.NoError(t, testexch.Setup(e), "Test instance Setup must not error")
testexch.UpdatePairsOnce(t, e)
e.API.AuthenticatedSupport = true
e.API.AuthenticatedWebsocketSupport = true
e.SetCredentials(apiKey, apiSecret, "", "", "", "")
e.Websocket.SetCanUseAuthenticatedEndpoints(true)
switch a {
case asset.Spot:
avail, err := e.GetAvailablePairs(a)
require.NoError(t, err)
if len(avail) > 1 { // reduce pairs to 1 to speed up tests
avail = avail[:1]
}
require.NoError(t, e.SetPairs(avail, a, true))
case asset.Futures:
avail, err := e.GetAvailablePairs(a)
require.NoError(t, err)
usdtPairs, err := avail.GetPairsByQuote(currency.USDT) // Get USDT margin pairs
require.NoError(t, err)
btcPairs, err := avail.GetPairsByQuote(currency.USD) // Get BTC margin pairs
require.NoError(t, err)
// below makes sure there is both a USDT and BTC pair available
// so that allows two connections to be made.
avail[0] = usdtPairs[0]
avail[1] = btcPairs[0]
avail = avail[:2]
require.NoError(t, e.SetPairs(avail, a, true))
default:
require.NoError(t, e.CurrencyPairs.SetAssetEnabled(a, false))
}
require.NoError(t, e.Websocket.Connect())
return e
}