forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexchange_test.go
216 lines (179 loc) · 4.82 KB
/
exchange_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package engine
import (
"testing"
"github.com/thrasher-corp/gocryptotrader/exchanges/bitfinex"
)
var testSetup = false
func SetupTest(t *testing.T) {
if !testSetup {
var err error
Bot, err = New()
if err != nil {
t.Fatal(err)
}
testSetup = true
}
if GetExchangeByName(testExchange) != nil {
return
}
err := LoadExchange(testExchange, false, nil)
if err != nil {
t.Errorf("SetupTest: Failed to load exchange: %s", err)
}
}
func CleanupTest(t *testing.T) {
if GetExchangeByName(testExchange) == nil {
return
}
err := UnloadExchange(testExchange)
if err != nil {
t.Fatalf("CleanupTest: Failed to unload exchange: %s",
err)
}
}
func TestExchangeManagerAdd(t *testing.T) {
t.Parallel()
var e exchangeManager
bitfinex := new(bitfinex.Bitfinex)
bitfinex.SetDefaults()
e.add(bitfinex)
if exch := e.getExchanges(); exch[0].GetName() != "Bitfinex" {
t.Error("unexpected exchange name")
}
}
func TestExchangeManagerGetExchanges(t *testing.T) {
t.Parallel()
var e exchangeManager
if exchanges := e.getExchanges(); exchanges != nil {
t.Error("unexpected value")
}
bitfinex := new(bitfinex.Bitfinex)
bitfinex.SetDefaults()
e.add(bitfinex)
if exch := e.getExchanges(); exch[0].GetName() != "Bitfinex" {
t.Error("unexpected exchange name")
}
}
func TestExchangeManagerRemoveExchange(t *testing.T) {
t.Parallel()
var e exchangeManager
if err := e.removeExchange("Bitfinex"); err != ErrNoExchangesLoaded {
t.Error("no exchanges should be loaded")
}
bitfinex := new(bitfinex.Bitfinex)
bitfinex.SetDefaults()
e.add(bitfinex)
if err := e.removeExchange(testExchange); err != ErrExchangeNotFound {
t.Error("Bitstamp exchange should return an error")
}
if err := e.removeExchange("BiTFiNeX"); err != nil {
t.Error("exchange should have been removed")
}
if e.Len() != 0 {
t.Error("exchange manager len should be 0")
}
}
func TestCheckExchangeExists(t *testing.T) {
SetupTest(t)
if GetExchangeByName(testExchange) == nil {
t.Errorf("TestGetExchangeExists: Unable to find exchange")
}
if GetExchangeByName("Asdsad") != nil {
t.Errorf("TestGetExchangeExists: Non-existent exchange found")
}
CleanupTest(t)
}
func TestGetExchangeByName(t *testing.T) {
SetupTest(t)
exch := GetExchangeByName(testExchange)
if exch == nil {
t.Errorf("TestGetExchangeByName: Failed to get exchange")
}
if !exch.IsEnabled() {
t.Errorf("TestGetExchangeByName: Unexpected result")
}
exch.SetEnabled(false)
bfx := GetExchangeByName(testExchange)
if bfx.IsEnabled() {
t.Errorf("TestGetExchangeByName: Unexpected result")
}
if exch.GetName() != testExchange {
t.Errorf("TestGetExchangeByName: Unexpected result")
}
exch = GetExchangeByName("Asdasd")
if exch != nil {
t.Errorf("TestGetExchangeByName: Non-existent exchange found")
}
CleanupTest(t)
}
func TestUnloadExchange(t *testing.T) {
SetupTest(t)
err := UnloadExchange("asdf")
if err.Error() != "exchange asdf not found" {
t.Errorf("TestUnloadExchange: Incorrect result: %s",
err)
}
err = UnloadExchange(testExchange)
if err != nil {
t.Errorf("TestUnloadExchange: Failed to get exchange. %s",
err)
}
err = UnloadExchange(testExchange)
if err != ErrNoExchangesLoaded {
t.Errorf("TestUnloadExchange: Incorrect result: %s",
err)
}
CleanupTest(t)
}
func TestDryRunParamInteraction(t *testing.T) {
SetupTest(t)
// Load bot as per normal, dry run and verbose for Bitfinex should be
// disabled
exchCfg, err := Bot.Config.GetExchangeConfig(testExchange)
if err != nil {
t.Error(err)
}
if Bot.Settings.EnableDryRun ||
exchCfg.Verbose {
t.Error("dryrun and verbose should have been disabled")
}
// Simulate overiding default settings and ensure that enabling exchange
// verbose mode will be set on Bitfinex
if err = UnloadExchange(testExchange); err != nil {
t.Error(err)
}
Bot.Settings.CheckParamInteraction = true
Bot.Settings.EnableExchangeVerbose = true
if err = LoadExchange(testExchange, false, nil); err != nil {
t.Error(err)
}
exchCfg, err = Bot.Config.GetExchangeConfig(testExchange)
if err != nil {
t.Error(err)
}
if !Bot.Settings.EnableDryRun ||
!exchCfg.Verbose {
t.Error("dryrun and verbose should have been enabled")
}
if err = UnloadExchange(testExchange); err != nil {
t.Error(err)
}
// Now set dryrun mode to false (via flagset and the previously enabled
// setting), enable exchange verbose mode and verify that verbose mode
// will be set on Bitfinex
Bot.Settings.EnableDryRun = false
Bot.Settings.CheckParamInteraction = true
Bot.Settings.EnableExchangeVerbose = true
flagSet["dryrun"] = true
if err = LoadExchange(testExchange, false, nil); err != nil {
t.Error(err)
}
exchCfg, err = Bot.Config.GetExchangeConfig(testExchange)
if err != nil {
t.Error(err)
}
if Bot.Settings.EnableDryRun ||
!exchCfg.Verbose {
t.Error("dryrun should be false and verbose should be true")
}
}