add test case for config.Map method

This commit is contained in:
c9s 2021-02-03 09:34:53 +08:00
parent 1a2c3556a8
commit 098a966813
4 changed files with 83 additions and 23 deletions

View File

@ -132,15 +132,15 @@ type BuildTargetConfig struct {
Name string `json:"name" yaml:"name"`
Arch string `json:"arch" yaml:"arch"`
OS string `json:"os" yaml:"os"`
LDFlags datatype.StringSlice `json:"ldflags" yaml:"ldflags"`
GCFlags datatype.StringSlice `json:"gcflags" yaml:"gcflags"`
Imports []string `json:"imports" yaml:"imports"`
LDFlags datatype.StringSlice `json:"ldflags,omitempty" yaml:"ldflags,omitempty"`
GCFlags datatype.StringSlice `json:"gcflags,omitempty" yaml:"gcflags,omitempty"`
Imports []string `json:"imports,omitempty" yaml:"imports,omitempty"`
}
type BuildConfig struct {
BuildDir string `json:"buildDir" yaml:"buildDir"`
Imports []string `json:"imports" yaml:"imports"`
Targets []BuildTargetConfig `json:"targets" yaml:"targets"`
BuildDir string `json:"buildDir,omitempty" yaml:"buildDir,omitempty"`
Imports []string `json:"imports,omitempty" yaml:"imports,omitempty"`
Targets []BuildTargetConfig `json:"targets,omitempty" yaml:"targets,omitempty"`
}
func GetNativeBuildTargetConfig() BuildTargetConfig {
@ -187,9 +187,27 @@ func (c *Config) Map() (map[string]interface{}, error) {
}
// convert strategy config back to the DSL format
for _, st := range c.ExchangeStrategies {
_ = st.Strategy
var exchangeStrategies []map[string]interface{}
for _, m := range c.ExchangeStrategies {
strategyID := m.Strategy.ID()
var params Stash
out, err := json.Marshal(m.Strategy)
if err != nil {
return nil, err
}
if err := json.Unmarshal(out, &params); err != nil {
return nil, err
}
exchangeStrategies = append(exchangeStrategies, map[string]interface{}{
"on": m.Mounts,
strategyID: params,
})
}
data["exchangeStrategies"] = exchangeStrategies
return data, err
}

View File

@ -55,12 +55,65 @@ func TestLoadConfig(t *testing.T) {
assert.Equal(t, "#error", config.Notifications.Slack.ErrorChannel)
},
},
{
name: "strategy",
args: args{configFile: "testdata/strategy.yaml"},
wantErr: false,
f: func(t *testing.T, config *Config) {
assert.Len(t, config.ExchangeStrategies, 1)
assert.Equal(t, config.ExchangeStrategies, []ExchangeStrategyMount{{
Mounts: []string{"binance"},
Strategy: &TestStrategy{
Symbol: "BTCUSDT",
Interval: "1m",
BaseQuantity: 0.1,
MaxAssetQuantity: 0,
MinDropPercentage: -0.05,
},
}})
m, err := config.Map()
assert.NoError(t, err)
assert.Equal(t, map[string]interface{}{
"sessions": map[string]interface{}{
"max": map[string]interface{}{
"exchange": "max",
"envVarPrefix": "MAX_",
},
"binance": map[string]interface{}{
"exchange": "binance",
"envVarPrefix": "BINANCE_",
},
},
"build": map[string]interface{}{
"buildDir": "build",
"targets": []interface{}{
map[string]interface{}{
"name": "bbgow-amd64-darwin",
"arch": "amd64",
"os": "darwin",
},
map[string]interface{}{
"name": "bbgow-amd64-linux",
"arch": "amd64",
"os": "linux",
},
},
},
"exchangeStrategies": []map[string]interface{}{
{
"on": []string{"binance"},
"test": Stash{
"symbol": "BTCUSDT",
"baseQuantity": 0.1,
"interval": "1m",
"maxAssetQuantity": 0.0,
"minDropPercentage": -0.05,
},
},
},
}, m)
},
},

View File

@ -120,9 +120,9 @@ type ExchangeSession struct {
// ---------------------------
// The exchange account states
Account *types.Account `json:"account" yaml:"-"`
Account *types.Account `json:"-" yaml:"-"`
IsInitialized bool `json:"isInitialized" yaml:"-"`
IsInitialized bool `json:"-" yaml:"-"`
// Stream is the connection stream of the exchange
Stream types.Stream `json:"-" yaml:"-"`

View File

@ -1,15 +1,12 @@
---
imports:
- github.com/c9s/bbgo/pkg/strategy/buyandhold
sessions:
max:
exchange: max
envVarPrefix: max
envVarPrefix: MAX_
binance:
exchange: binance
envVarPrefix: binance
envVarPrefix: BINANCE_
exchangeStrategies:
- on: binance
@ -19,11 +16,3 @@ exchangeStrategies:
baseQuantity: 0.1
minDropPercentage: -0.05
reportPnL:
- averageCostBySymbols:
- "BTCUSDT"
- "BNBUSDT"
of: binance
when:
- "@daily"
- "@hourly"