-
Notifications
You must be signed in to change notification settings - Fork 9
/
schedule_test.go
324 lines (245 loc) · 10.3 KB
/
schedule_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
package main
import (
"errors"
"fmt"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/sberserker/dcagdax/exchanges"
"github.com/sberserker/dcagdax/mocks"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest"
)
func loggerStub(t *testing.T) *zap.Logger {
return zaptest.NewLogger(t, zaptest.WrapOptions(zap.Hooks(func(e zapcore.Entry) error {
return nil
})))
}
func TestCalcLimitOrder(t *testing.T) {
type test struct {
askPrice float64
fiatAmout float64
fee float64
spread float64
outOrderPrise float64
outOrderSize float64
}
tests := []test{
{askPrice: 1, fiatAmout: 10, fee: 1, spread: 1, outOrderPrise: 1.01, outOrderSize: 9.80198019},
}
for _, tc := range tests {
s := gdaxSchedule{}
s.req = syncRequest{fee: tc.fee, orderSpread: tc.spread}
s.logger = loggerStub(t).Sugar()
orderPrice, orderSize := s.calcLimitOrder(decimal.NewFromFloat(tc.askPrice), decimal.NewFromFloat(tc.fiatAmout))
orderPricef, _ := orderPrice.Float64()
orderSizef, _ := orderSize.Float64()
assert.Equal(t, tc.outOrderPrise, orderPricef)
assert.Equal(t, tc.outOrderSize, orderSizef)
}
}
func TestSyncWhenNotAGoodTime(t *testing.T) {
type test struct {
until time.Time
after time.Time
err string
message string
}
afterTomorrow := time.Now().AddDate(0, 0, 1)
afterTomrrowMesage := fmt.Sprintf("Configured to start after %v, not taking any action", afterTomorrow)
tests := []test{
{until: time.Time{}, after: afterTomorrow, err: afterTomrrowMesage, message: "not yet time to run"},
{until: time.Now().AddDate(0, 0, -1), after: time.Time{}, err: "Deadline has passed, not taking any action", message: "time to run passed"},
}
for _, tc := range tests {
s := gdaxSchedule{}
s.logger = loggerStub(t).Sugar()
s.req = syncRequest{until: tc.until, after: tc.after}
err := s.Sync()
assert.Equal(t, tc.err, err.Error(), tc.message)
}
}
func TestWhenRecentPurchase(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
m := mocks.NewMockExchange(ctrl)
s := gdaxSchedule{}
s.logger = loggerStub(t).Sugar()
s.req = syncRequest{every: 24 * time.Hour, currency: "USD"} // setup run every 24 hrs
s.markerCoin = "BTC"
s.exchange = m
t.Run("when recent purchase", func(t *testing.T) {
//but last run was 12 hours ago
lastPurchaseTime := time.Now().Add(-12 * time.Hour)
m.EXPECT().LastPurchaseTime("BTC", "USD", gomock.Any()).Return(&lastPurchaseTime, nil)
err := s.Sync()
assert.Equal(t, "Detected a recent purchase, waiting for next purchase window", err.Error())
})
t.Run("when recent purchase falsed", func(t *testing.T) {
//but last run was 12 hours ago
lastPurchaseTime := time.Now().Add(-12 * time.Hour)
m.EXPECT().LastPurchaseTime("BTC", "USD", gomock.Any()).Return(&lastPurchaseTime, errors.New("some error"))
err := s.Sync()
assert.Equal(t, "some error", err.Error())
})
}
func TestTimeToPurchase(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
m := mocks.NewMockExchange(ctrl)
s := gdaxSchedule{}
s.logger = loggerStub(t).Sugar()
s.req = syncRequest{every: 24 * time.Hour, currency: "USD"} // setup run every 24 hrs
s.markerCoin = "BTC"
s.exchange = m
t.Run("when recent purchase", func(t *testing.T) {
lastPurchaseTime := time.Now().Add(-12 * time.Hour) //last purchase time 12 hrs ago
m.EXPECT().LastPurchaseTime("BTC", "USD", gomock.Any()).Return(&lastPurchaseTime, nil)
result, err := s.timeToPurchase(time.Now().Add(-24 * time.Hour))
assert.False(t, result)
assert.Nil(t, err)
})
t.Run("when no recent purchase", func(t *testing.T) {
lastPurchaseTime := time.Now().Add(-48 * time.Hour) //last purchase time 2 days ago
m.EXPECT().LastPurchaseTime("BTC", "USD", gomock.Any()).Return(&lastPurchaseTime, nil)
result, err := s.timeToPurchase(time.Now().Add(-24 * time.Hour))
assert.True(t, result)
assert.Nil(t, err)
})
}
func TestNewSchedule(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
m := mocks.NewMockExchange(ctrl)
t.Run("when success", func(t *testing.T) {
req := syncRequest{every: 24 * time.Hour, orderType: exchanges.Market, autoFund: true, currency: "USD", usd: 50, coins: []string{"BTC:50", "ETH:50"}} // setup run every 24 hrs
m.EXPECT().GetTickerSymbol("BTC", "USD").Return("BTC:USD")
m.EXPECT().GetProduct("BTC:USD").Return(&exchanges.Product{BaseMinSize: 0.001}, nil)
m.EXPECT().GetTicker("BTC:USD").Return(&exchanges.Ticker{Price: 1000}, nil)
m.EXPECT().GetTickerSymbol("ETH", "USD").Return("ETH:USD")
m.EXPECT().GetProduct("ETH:USD").Return(&exchanges.Product{BaseMinSize: 0.5}, nil)
m.EXPECT().GetTicker("ETH:USD").Return(&exchanges.Ticker{Price: 10}, nil)
s, err := newGdaxSchedule(m, loggerStub(t).Sugar(), false, req)
assert.Nil(t, err)
assert.NotNil(t, s)
assert.Equal(t, 25.0, s.coins["BTC"].amount)
assert.Equal(t, "BTC:USD", s.coins["BTC"].symbol)
assert.Equal(t, 25.0, s.coins["ETH"].amount)
assert.Equal(t, "ETH:USD", s.coins["ETH"].symbol)
})
t.Run("when unbalanced coins request", func(t *testing.T) {
req := syncRequest{every: 24 * time.Hour, orderType: exchanges.Market, autoFund: true, currency: "USD", usd: 50, coins: []string{"BTC:50", "ETH:49"}} // setup run every 24 hrs
m.EXPECT().GetTickerSymbol("BTC", "USD").Return("BTC:USD")
m.EXPECT().GetProduct("BTC:USD").Return(&exchanges.Product{BaseMinSize: 0.001}, nil)
m.EXPECT().GetTicker("BTC:USD").Return(&exchanges.Ticker{Price: 1000}, nil)
m.EXPECT().GetTickerSymbol("ETH", "USD").Return("ETH:USD")
m.EXPECT().GetProduct("ETH:USD").Return(&exchanges.Product{BaseMinSize: 0.5}, nil)
m.EXPECT().GetTicker("ETH:USD").Return(&exchanges.Ticker{Price: 10}, nil)
s, err := newGdaxSchedule(m, loggerStub(t).Sugar(), false, req)
assert.NotNil(t, err)
assert.Nil(t, s)
assert.Equal(t, "Total percentages must be exactly 100, provided 99", err.Error())
})
}
func TestNewScheduleWhenBelowMinSize(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
m := mocks.NewMockExchange(ctrl)
req := syncRequest{every: 24 * time.Hour, orderType: exchanges.Market, autoFund: true, currency: "USD", usd: 50, coins: []string{"BTC:50"}} // setup run every 24 hrs
m.EXPECT().GetTickerSymbol("BTC", "USD").Return("BTC:USD")
m.EXPECT().GetProduct("BTC:USD").Return(&exchanges.Product{BaseMinSize: 0.01}, nil)
m.EXPECT().GetTicker("BTC:USD").Return(&exchanges.Ticker{Price: 10000}, nil)
s, err := newGdaxSchedule(m, loggerStub(t).Sugar(), false, req)
assert.Nil(t, s)
assert.NotNil(t, err)
assert.Equal(t, "Coinbase minimum BTC trade amount is $100.00, but you're trying to purchase $25.00", err.Error())
}
func TestSyncWhenSuccessful(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
m := mocks.NewMockExchange(ctrl)
s := gdaxSchedule{}
s.logger = loggerStub(t).Sugar()
s.req = syncRequest{every: 24 * time.Hour, orderType: exchanges.Market, autoFund: true, currency: "USD", usd: 50} // setup run every 24 hrs
s.coins = map[string]orderDetails{"BTC": {symbol: "btcusd", amount: 50}}
s.markerCoin = "BTC"
s.sleepFunc = func(d time.Duration) {}
s.exchange = m
now := time.Now()
result := exchanges.Order{OrderID: "1"}
m.EXPECT().LastPurchaseTime("BTC", "USD", gomock.Any()).Return(nil, nil)
m.EXPECT().GetFiatAccount("USD").Return(&exchanges.Account{Available: 25}, nil)
m.EXPECT().GetPendingTransfers("USD").Return([]exchanges.PendingTransfer{}, nil)
m.EXPECT().Deposit("USD", 25.0).Return(&now, nil)
m.EXPECT().CreateOrder("btcusd", 50.0, exchanges.Market, gomock.Any()).Return(&result, nil)
err := s.Sync()
assert.Nil(t, err)
}
func TestSyncWhenNotSufficientBalanceAndAutoFundIsOff(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
m := mocks.NewMockExchange(ctrl)
s := gdaxSchedule{}
s.logger = loggerStub(t).Sugar()
s.req = syncRequest{every: 24 * time.Hour, orderType: exchanges.Market, autoFund: false, currency: "USD", usd: 50} // setup run every 24 hrs
s.coins = map[string]orderDetails{"BTC": {symbol: "btcusd", amount: 50}}
s.markerCoin = "BTC"
s.sleepFunc = func(d time.Duration) {}
s.exchange = m
m.EXPECT().LastPurchaseTime("BTC", "USD", gomock.Any()).Return(nil, nil)
m.EXPECT().GetFiatAccount("USD").Return(&exchanges.Account{Available: 25}, nil)
m.EXPECT().GetPendingTransfers("USD").Return([]exchanges.PendingTransfer{}, nil)
err := s.Sync()
assert.NotNil(t, err)
assert.Equal(t, "No sufficient amount for trade and autofund is disabled. Deposit money to proceed", err.Error())
}
func TestSyncShouldAskForConfirmationWhenForceIsOn(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
m := mocks.NewMockExchange(ctrl)
s := gdaxSchedule{}
s.logger = loggerStub(t).Sugar()
s.req = syncRequest{every: 24 * time.Hour, orderType: exchanges.Market, autoFund: false, currency: "USD", usd: 50, force: true} // setup run every 24 hrs
s.coins = map[string]orderDetails{"BTC": {symbol: "btcusd", amount: 50}}
s.sleepFunc = func(d time.Duration) {}
s.exchange = m
t.Run("when rejected", func(t *testing.T) {
s.confirmFunc = func(s string) bool {
return false
}
err := s.Sync()
assert.NotNil(t, err)
assert.Equal(t, "User rejected the trade", err.Error())
})
t.Run("when approved", func(t *testing.T) {
s.confirmFunc = func(s string) bool {
return true
}
result := exchanges.Order{OrderID: "1"}
m.EXPECT().GetFiatAccount("USD").Return(&exchanges.Account{Available: 50}, nil)
m.EXPECT().CreateOrder("btcusd", 50.0, exchanges.Market, gomock.Any()).Return(&result, nil)
err := s.Sync()
assert.Nil(t, err)
})
}
func TestSyncWhenDebugIsOn(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
m := mocks.NewMockExchange(ctrl)
s := gdaxSchedule{}
s.logger = loggerStub(t).Sugar()
s.req = syncRequest{every: 24 * time.Hour, orderType: exchanges.Market, autoFund: true, currency: "USD", usd: 50} // setup run every 24 hrs
s.debug = true
s.markerCoin = "BTC"
s.coins = map[string]orderDetails{"BTC": {symbol: "btcusd", amount: 50}}
s.sleepFunc = func(d time.Duration) {}
s.exchange = m
m.EXPECT().LastPurchaseTime("BTC", "USD", gomock.Any()).Return(nil, nil)
m.EXPECT().GetFiatAccount("USD").Return(&exchanges.Account{Available: 25}, nil)
m.EXPECT().GetPendingTransfers("USD").Return([]exchanges.PendingTransfer{}, nil)
err := s.Sync()
assert.Nil(t, err)
}