forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exchange_manager_test.go
129 lines (117 loc) · 3.3 KB
/
exchange_manager_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package engine
import (
"errors"
"fmt"
"strings"
"testing"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/bitfinex"
"github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues"
)
func TestSetupExchangeManager(t *testing.T) {
t.Parallel()
m := SetupExchangeManager()
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()
m := SetupExchangeManager()
b := new(bitfinex.Bitfinex)
b.SetDefaults()
m.Add(b)
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()
m := SetupExchangeManager()
exchanges, err := m.GetExchanges()
if err != nil {
t.Error("no exchange manager found")
}
if len(exchanges) != 0 {
t.Error("unexpected value")
}
b := new(bitfinex.Bitfinex)
b.SetDefaults()
m.Add(b)
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()
m := SetupExchangeManager()
if err := m.RemoveExchange("Bitfinex"); err != ErrNoExchangesLoaded {
t.Error("no exchanges should be loaded")
}
b := new(bitfinex.Bitfinex)
b.SetDefaults()
m.Add(b)
err := m.RemoveExchange("Bitstamp")
if !errors.Is(err, ErrExchangeNotFound) {
t.Errorf("received: %v but expected: %v", err, ErrExchangeNotFound)
}
if err := m.RemoveExchange("BiTFiNeX"); err != nil {
t.Error("exchange should have been removed")
}
if m.Len() != 0 {
t.Error("exchange manager len should be 0")
}
}
func TestNewExchangeByName(t *testing.T) {
m := SetupExchangeManager()
exchanges := []string{"binanceus", "binance", "bitfinex", "bitflyer", "bithumb", "bitmex", "bitstamp", "bittrex", "btc markets", "btse", "bybit", "coinut", "exmo", "coinbasepro", "gateio", "gemini", "hitbtc", "huobi", "itbit", "kraken", "lbank", "localbitcoins", "okcoin international", "okx", "poloniex", "yobit", "zb", "fake"}
for i := range exchanges {
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")
}
}
}
}
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 := SetupExchangeManager()
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")
}
}
}