forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents_test.go
327 lines (283 loc) · 7.42 KB
/
events_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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package engine
import (
"testing"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
)
const (
testExchange = "Bitstamp"
)
func addValidEvent() (int64, error) {
return Add(testExchange,
ItemPrice,
EventConditionParams{Condition: ConditionGreaterThan, Price: 1},
currency.NewPair(currency.BTC, currency.USD),
asset.Spot,
"SMS,test")
}
func TestAdd(t *testing.T) {
SetupTestHelpers(t)
_, err := Add("", "", EventConditionParams{}, currency.Pair{}, "", "")
if err == nil {
t.Error("should err on invalid params")
}
_, err = addValidEvent()
if err != nil {
t.Error("unexpected result", err)
}
_, err = addValidEvent()
if err != nil {
t.Error("unexpected result", err)
}
if len(Events) != 2 {
t.Error("2 events should be stored")
}
}
func TestRemove(t *testing.T) {
SetupTestHelpers(t)
id, err := addValidEvent()
if err != nil {
t.Error("unexpected result", err)
}
if s := Remove(id); !s {
t.Error("unexpected result")
}
if s := Remove(id); s {
t.Error("unexpected result")
}
}
func TestGetEventCounter(t *testing.T) {
SetupTestHelpers(t)
_, err := addValidEvent()
if err != nil {
t.Error("unexpected result", err)
}
n, e := GetEventCounter()
if n == 0 || e > 0 {
t.Error("unexpected result")
}
Events[0].Executed = true
n, e = GetEventCounter()
if n == 0 || e == 0 {
t.Error("unexpected result")
}
}
func TestExecuteAction(t *testing.T) {
t.Parallel()
if Bot == nil {
Bot = new(Engine)
}
var e Event
if r := e.ExecuteAction(); !r {
t.Error("unexpected result")
}
e.Action = "SMS,test"
if r := e.ExecuteAction(); !r {
t.Error("unexpected result")
}
e.Action = "SMS,ALL"
if r := e.ExecuteAction(); !r {
t.Error("unexpected result")
}
}
func TestString(t *testing.T) {
t.Parallel()
e := Event{
Exchange: testExchange,
Item: ItemPrice,
Condition: EventConditionParams{
Condition: ConditionGreaterThan,
Price: 1,
},
Pair: currency.NewPair(currency.BTC, currency.USD),
Asset: asset.Spot,
Action: "SMS,ALL",
}
if r := e.String(); r != "If the BTCUSD [SPOT] PRICE on Bitstamp meets the following {> 1 false false 0} then SMS,ALL." {
t.Error("unexpected result")
}
}
func TestProcessTicker(t *testing.T) {
if Bot == nil {
Bot = new(Engine)
}
e := Event{
Exchange: testExchange,
Pair: currency.NewPair(currency.BTC, currency.USD),
Asset: asset.Spot,
Condition: EventConditionParams{
Condition: ConditionGreaterThan,
Price: 1,
},
}
// now populate it with a 0 entry
tick := ticker.Price{
Pair: currency.NewPair(currency.BTC, currency.USD),
ExchangeName: e.Exchange,
AssetType: e.Asset,
}
if err := ticker.ProcessTicker(&tick); err != nil {
t.Fatal("unexpected result:", err)
}
if r := e.processTicker(); r {
t.Error("unexpected result")
}
// now populate it with a number > 0
tick.Last = 1337
if err := ticker.ProcessTicker(&tick); err != nil {
t.Fatal("unexpected result:", err)
}
if r := e.processTicker(); !r {
t.Error("unexpected result")
}
}
func TestProcessCondition(t *testing.T) {
t.Parallel()
var e Event
tester := []struct {
Condition string
Actual float64
Threshold float64
ExpectedResult bool
}{
{ConditionGreaterThan, 1, 2, false},
{ConditionGreaterThan, 2, 1, true},
{ConditionGreaterThanOrEqual, 1, 2, false},
{ConditionGreaterThanOrEqual, 2, 1, true},
{ConditionIsEqual, 1, 1, true},
{ConditionIsEqual, 1, 2, false},
{ConditionLessThan, 1, 2, true},
{ConditionLessThan, 2, 1, false},
{ConditionLessThanOrEqual, 1, 2, true},
{ConditionLessThanOrEqual, 2, 1, false},
}
for x := range tester {
e.Condition.Condition = tester[x].Condition
if r := e.processCondition(tester[x].Actual, tester[x].Threshold); r != tester[x].ExpectedResult {
t.Error("unexpected result")
}
}
}
func TestProcessOrderbook(t *testing.T) {
if Bot == nil {
Bot = new(Engine)
}
e := Event{
Exchange: testExchange,
Pair: currency.NewPair(currency.BTC, currency.USD),
Asset: asset.Spot,
Condition: EventConditionParams{
Condition: ConditionGreaterThan,
CheckBidsAndAsks: true,
OrderbookAmount: 100,
},
}
// now populate it with a 0 entry
o := orderbook.Base{
Pair: currency.NewPair(currency.BTC, currency.USD),
Bids: []orderbook.Item{{Amount: 24, Price: 23}},
Asks: []orderbook.Item{{Amount: 24, Price: 23}},
ExchangeName: e.Exchange,
AssetType: e.Asset,
}
if err := o.Process(); err != nil {
t.Fatal("unexpected result:", err)
}
if r := e.processOrderbook(); !r {
t.Error("unexpected result")
}
}
func TestCheckEventCondition(t *testing.T) {
t.Parallel()
if Bot == nil {
Bot = new(Engine)
}
e := Event{
Item: ItemPrice,
}
if r := e.CheckEventCondition(); r {
t.Error("unexpected result")
}
e.Item = ItemOrderbook
if r := e.CheckEventCondition(); r {
t.Error("unexpected result")
}
}
func TestIsValidEvent(t *testing.T) {
SetupTestHelpers(t)
// invalid exchange name
if err := IsValidEvent("meow", "", EventConditionParams{}, ""); err != errExchangeDisabled {
t.Error("unexpected result:", err)
}
// invalid item
if err := IsValidEvent(testExchange, "", EventConditionParams{}, ""); err != errInvalidItem {
t.Error("unexpected result:", err)
}
// invalid condition
if err := IsValidEvent(testExchange, ItemPrice, EventConditionParams{}, ""); err != errInvalidCondition {
t.Error("unexpected result:", err)
}
// valid condition but empty price which will still throw an errInvalidCondition
c := EventConditionParams{
Condition: ConditionGreaterThan,
}
if err := IsValidEvent(testExchange, ItemPrice, c, ""); err != errInvalidCondition {
t.Error("unexpected result:", err)
}
// valid condition but empty orderbook amount will still still throw an errInvalidCondition
if err := IsValidEvent(testExchange, ItemOrderbook, c, ""); err != errInvalidCondition {
t.Error("unexpected result:", err)
}
// test action splitting, but invalid
c.OrderbookAmount = 1337
if err := IsValidEvent(testExchange, ItemOrderbook, c, "a,meow"); err != errInvalidAction {
t.Error("unexpected result:", err)
}
// check for invalid action without splitting
if err := IsValidEvent(testExchange, ItemOrderbook, c, "hi"); err != errInvalidAction {
t.Error("unexpected result:", err)
}
// valid event
if err := IsValidEvent(testExchange, ItemOrderbook, c, "SMS,test"); err != nil {
t.Error("unexpected result:", err)
}
}
func TestIsValidExchange(t *testing.T) {
t.Parallel()
if s := IsValidExchange("invalidexchangerino"); s {
t.Error("unexpected result")
}
SetupTestHelpers(t)
if s := IsValidExchange(testExchange); !s {
t.Error("unexpected result")
}
}
func TestIsValidCondition(t *testing.T) {
t.Parallel()
if s := IsValidCondition("invalidconditionerino"); s {
t.Error("unexpected result")
}
if s := IsValidCondition(ConditionGreaterThan); !s {
t.Error("unexpected result")
}
}
func TestIsValidAction(t *testing.T) {
t.Parallel()
if s := IsValidAction("invalidactionerino"); s {
t.Error("unexpected result")
}
if s := IsValidAction(ActionSMSNotify); !s {
t.Error("unexpected result")
}
}
func TestIsValidItem(t *testing.T) {
t.Parallel()
if s := IsValidItem("invaliditemerino"); s {
t.Error("unexpected result")
}
if s := IsValidItem(ItemPrice); !s {
t.Error("unexpected result")
}
}