-
-
Notifications
You must be signed in to change notification settings - Fork 380
Expand file tree
/
Copy pathstrategy_test.go
More file actions
66 lines (55 loc) · 1.62 KB
/
Copy pathstrategy_test.go
File metadata and controls
66 lines (55 loc) · 1.62 KB
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
package supertrend
import (
"testing"
"go.uber.org/mock/gomock"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/types/mocks"
)
func newTestSession(t *testing.T) *bbgo.ExchangeSession {
t.Helper()
mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)
mockEx := mocks.NewMockExchange(mockCtrl)
mockEx.EXPECT().NewStream().Return(&types.StandardStream{}).AnyTimes()
mockEx.EXPECT().Name().Return(types.ExchangeName("backtest")).AnyTimes()
return bbgo.NewExchangeSession("test", mockEx)
}
// A linearRegression block with window: 0 (or no interval) is nil-ed by
// setupIndicators, but Subscribe used to dereference it unconditionally and
// session.Subscribe panics on an empty kline interval.
func TestStrategySubscribe_LinearRegressionNilSafe(t *testing.T) {
cases := []struct {
name string
linReg *LinReg
subs int
}{
{name: "nil block", linReg: nil, subs: 1},
{
name: "window zero without interval",
linReg: &LinReg{IntervalWindow: types.IntervalWindow{Window: 0}},
subs: 1,
},
{
name: "valid block",
linReg: &LinReg{
IntervalWindow: types.IntervalWindow{Window: 20, Interval: types.Interval4h},
},
subs: 2,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
session := newTestSession(t)
s := &Strategy{
Symbol: "BTCUSDT",
IntervalWindow: types.IntervalWindow{Interval: types.Interval1h, Window: 5},
LinearRegression: tc.linReg,
}
s.Subscribe(session)
if got := len(session.Subscriptions); got != tc.subs {
t.Fatalf("subscriptions = %d, want %d", got, tc.subs)
}
})
}
}