bbgo_origin/pkg/bbgo/config_test.go

111 lines
2.7 KiB
Go
Raw Normal View History

2020-10-26 13:45:02 +00:00
package bbgo
2020-10-20 05:52:25 +00:00
import (
2020-10-26 13:45:02 +00:00
"context"
2020-10-20 05:52:25 +00:00
"testing"
"github.com/stretchr/testify/assert"
)
2020-10-26 13:45:02 +00:00
func init() {
2020-10-28 23:54:59 +00:00
RegisterStrategy("test", &TestStrategy{})
2020-10-26 13:45:02 +00:00
}
type TestStrategy struct {
Symbol string `json:"symbol"`
Interval string `json:"interval"`
BaseQuantity float64 `json:"baseQuantity"`
MaxAssetQuantity float64 `json:"maxAssetQuantity"`
MinDropPercentage float64 `json:"minDropPercentage"`
}
func (s *TestStrategy) Run(ctx context.Context, orderExecutor OrderExecutor, session *ExchangeSession) error {
return nil
}
2020-10-23 06:01:45 +00:00
func TestLoadConfig(t *testing.T) {
2020-10-20 05:52:25 +00:00
type args struct {
configFile string
}
tests := []struct {
name string
args args
wantErr bool
2020-10-26 09:57:28 +00:00
f func(t *testing.T, config *Config)
2020-10-20 05:52:25 +00:00
}{
2020-10-27 00:48:47 +00:00
{
name: "notification",
args: args{configFile: "testdata/notification.yaml"},
wantErr: false,
f: func(t *testing.T, config *Config) {
assert.NotNil(t, config.Notifications)
assert.NotNil(t, config.Notifications.SessionChannels)
assert.NotNil(t, config.Notifications.SymbolChannels)
assert.Equal(t, map[string]string{
"^BTC": "#btc",
"^ETH": "#eth",
}, config.Notifications.SymbolChannels)
assert.NotNil(t, config.Notifications.Routing)
assert.Equal(t, "#dev-bbgo", config.Notifications.Slack.DefaultChannel)
assert.Equal(t, "#error", config.Notifications.Slack.ErrorChannel)
},
},
2020-10-20 05:52:25 +00:00
{
2020-10-26 09:57:28 +00:00
name: "strategy",
args: args{configFile: "testdata/strategy.yaml"},
wantErr: false,
f: func(t *testing.T, config *Config) {
assert.Len(t, config.ExchangeStrategies, 1)
2020-10-20 05:52:25 +00:00
},
2020-10-26 09:57:28 +00:00
},
{
name: "order_executor",
args: args{configFile: "testdata/order_executor.yaml"},
2020-10-20 05:52:25 +00:00
wantErr: false,
2020-10-26 09:57:28 +00:00
f: func(t *testing.T, config *Config) {
assert.Len(t, config.Sessions, 2)
session, ok := config.Sessions["max"]
assert.True(t, ok)
assert.NotNil(t, session)
riskControls := config.RiskControls
assert.NotNil(t, riskControls)
assert.NotNil(t, riskControls.SessionBasedRiskControl)
2020-10-26 09:57:28 +00:00
conf, ok := riskControls.SessionBasedRiskControl["max"]
assert.True(t, ok)
assert.NotNil(t, conf)
assert.NotNil(t, conf.OrderExecutor)
assert.NotNil(t, conf.OrderExecutor.BySymbol)
executorConf, ok := conf.OrderExecutor.BySymbol["BTCUSDT"]
2020-10-26 09:57:28 +00:00
assert.True(t, ok)
assert.NotNil(t, executorConf)
},
2020-10-20 05:52:25 +00:00
},
}
2020-10-20 06:14:21 +00:00
2020-10-20 05:52:25 +00:00
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2020-10-20 06:32:22 +00:00
config, err := Load(tt.args.configFile)
2020-10-20 05:52:25 +00:00
if err != nil {
2020-10-20 06:32:22 +00:00
t.Errorf("Load() error = %v", err)
return
2020-10-20 05:52:25 +00:00
} else {
if tt.wantErr {
2020-10-20 06:32:22 +00:00
t.Errorf("Load() error = %v, wantErr %v", err, tt.wantErr)
return
2020-10-20 05:52:25 +00:00
}
}
2020-10-20 06:32:22 +00:00
assert.NotNil(t, config)
2020-10-26 09:57:28 +00:00
if tt.f != nil {
tt.f(t, config)
}
2020-10-20 05:52:25 +00:00
})
}
}