Files
gocryptotrader/engine/exchange_manager_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

211 lines
4.6 KiB
Go

package engine
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/bitfinex"
"github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues"
)
type broken struct {
bitfinex.Exchange
}
func (b *broken) Shutdown() error { return errExpectedTestError }
func TestNewExchangeManager(t *testing.T) {
t.Parallel()
m := NewExchangeManager()
if m == nil { //nolint:staticcheck,nolintlint // SA5011 Ignore the nil warnings
t.Fatalf("unexpected response")
}
if m.exchanges == nil { //nolint:staticcheck,nolintlint // SA5011 Ignore the nil warnings
t.Error("unexpected response")
}
}
func TestExchangeManagerAdd(t *testing.T) {
t.Parallel()
var m *ExchangeManager
err := m.Add(nil)
require.ErrorIs(t, err, ErrNilSubsystem)
m = NewExchangeManager()
err = m.Add(nil)
require.ErrorIs(t, err, errExchangeIsNil)
b := new(bitfinex.Exchange)
b.SetDefaults()
err = m.Add(b)
require.NoError(t, err)
err = m.Add(b)
require.ErrorIs(t, err, ErrExchangeAlreadyLoaded)
exchanges, err := m.GetExchanges()
if err != nil {
t.Error("no exchange manager found")
}
if exchanges[0].GetName() != "Bitfinex" {
t.Error("unexpected exchange name")
}
}
func TestExchangeManagerGetExchanges(t *testing.T) {
t.Parallel()
var m *ExchangeManager
_, err := m.GetExchanges()
require.ErrorIs(t, err, ErrNilSubsystem)
m = NewExchangeManager()
exchanges, err := m.GetExchanges()
if err != nil {
t.Error("no exchange manager found")
}
if len(exchanges) != 0 {
t.Error("unexpected value")
}
b := new(bitfinex.Exchange)
b.SetDefaults()
err = m.Add(b)
require.NoError(t, err)
exchanges, err = m.GetExchanges()
if err != nil {
t.Error("no exchange manager found")
}
if exchanges[0].GetName() != "Bitfinex" {
t.Error("unexpected exchange name")
}
}
func TestExchangeManagerRemoveExchange(t *testing.T) {
t.Parallel()
var m *ExchangeManager
err := m.RemoveExchange("")
require.ErrorIs(t, err, ErrNilSubsystem)
m = NewExchangeManager()
err = m.RemoveExchange("")
require.ErrorIs(t, err, ErrExchangeNameIsEmpty)
err = m.RemoveExchange("Bitfinex")
require.ErrorIs(t, err, ErrExchangeNotFound)
b := new(bitfinex.Exchange)
b.SetDefaults()
err = m.Add(b)
require.NoError(t, err)
err = m.RemoveExchange("Bitstamp")
assert.ErrorIs(t, err, ErrExchangeNotFound)
err = m.RemoveExchange("BiTFiNeX")
require.NoError(t, err)
if len(m.exchanges) != 0 {
t.Error("exchange manager len should be 0")
}
brokenExch := &broken{}
brokenExch.SetDefaults()
err = m.Add(brokenExch)
require.NoError(t, err)
err = m.RemoveExchange("BiTFiNeX")
require.ErrorIs(t, err, errExpectedTestError)
}
func TestNewExchangeByName(t *testing.T) {
var m *ExchangeManager
_, err := m.NewExchangeByName("")
require.ErrorIs(t, err, ErrNilSubsystem)
m = NewExchangeManager()
_, err = m.NewExchangeByName("")
require.ErrorIs(t, err, ErrExchangeNameIsEmpty)
exchanges := exchange.Exchanges
exchanges = append(exchanges, "fake")
for i := range exchanges {
var exch exchange.IBotExchange
exch, err = m.NewExchangeByName(exchanges[i])
if err != nil && exchanges[i] != "fake" {
t.Fatal(err)
}
if err == nil {
exch.SetDefaults()
if !strings.EqualFold(exch.GetName(), exchanges[i]) {
t.Error("did not load expected exchange")
}
}
}
load := &bitfinex.Exchange{}
load.SetDefaults()
err = m.Add(load)
require.NoError(t, err)
_, err = m.NewExchangeByName("bitfinex")
require.ErrorIs(t, err, ErrExchangeAlreadyLoaded)
}
type ExchangeBuilder struct{}
func (n ExchangeBuilder) NewExchangeByName(name string) (exchange.IBotExchange, error) {
var exch exchange.IBotExchange
switch name {
case "customex":
exch = new(sharedtestvalues.CustomEx)
default:
return nil, fmt.Errorf("%s, %w", name, ErrExchangeNotFound)
}
return exch, nil
}
func TestNewCustomExchangeByName(t *testing.T) {
m := NewExchangeManager()
m.Builder = ExchangeBuilder{}
name := "customex"
exch, err := m.NewExchangeByName(name)
if err != nil {
t.Fatal(err)
}
if err == nil {
exch.SetDefaults()
if !strings.EqualFold(exch.GetName(), name) {
t.Error("did not load expected exchange")
}
}
}
func TestExchangeManagerShutdown(t *testing.T) {
t.Parallel()
var m *ExchangeManager
err := m.Shutdown(-1)
require.ErrorIs(t, err, ErrNilSubsystem)
m = NewExchangeManager()
err = m.Shutdown(-1)
require.NoError(t, err)
brokenExch := &broken{}
brokenExch.SetDefaults()
err = m.Add(brokenExch)
require.NoError(t, err)
err = m.Shutdown(-1)
require.NoError(t, err)
}