Skip to content

Commit

Permalink
fix mock tests and config loading
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Jan 5, 2025
1 parent cc4437a commit 00aa355
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
4 changes: 4 additions & 0 deletions pkg/bbgo/account_value_calc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestAccountValueCalculator_NetValue(t *testing.T) {
ticker := Ticker(symbol)
mockEx := mocks.NewMockExchange(mockCtrl)
// for market data stream and user data stream
mockEx.EXPECT().Name().Return(types.ExchangeName("test")).AnyTimes()
mockEx.EXPECT().NewStream().Return(&types.StandardStream{}).Times(2)
mockEx.EXPECT().QueryTicker(gomock.Any(), symbol).Return(&ticker, nil).AnyTimes()

Expand Down Expand Up @@ -65,6 +66,8 @@ func TestAccountValueCalculator_NetValue(t *testing.T) {
ticker := Ticker(symbol)

mockEx := mocks.NewMockExchange(mockCtrl)
mockEx.EXPECT().Name().Return(types.ExchangeName("test")).AnyTimes()

// for market data stream and user data stream
mockEx.EXPECT().NewStream().Return(&types.StandardStream{}).Times(2)
mockEx.EXPECT().QueryTicker(gomock.Any(), symbol).Return(&ticker, nil).AnyTimes()
Expand Down Expand Up @@ -106,6 +109,7 @@ func TestNewAccountValueCalculator_MarginLevel(t *testing.T) {
ticker := Ticker(symbol)

mockEx := mocks.NewMockExchange(mockCtrl)
mockEx.EXPECT().Name().Return(types.ExchangeName("test")).AnyTimes()
// for market data stream and user data stream
mockEx.EXPECT().NewStream().Return(&types.StandardStream{}).Times(2)
mockEx.EXPECT().QueryTicker(gomock.Any(), symbol).Return(&ticker, nil).AnyTimes()
Expand Down
14 changes: 11 additions & 3 deletions pkg/bbgo/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,10 +665,12 @@ func loadExchangeStrategies(config *Config, stash Stash) (err error) {
}
}

// configStash is a map of strategy id and its config
// it has two keys: "on" and {strategyID}
strategyLoaded := false
for id, conf := range configStash {
if id == "on" {
// Show error when we didn't find the Strategy
return fmt.Errorf("strategy %s is not defined, possibly caused by an incorrect config format, please check your config file", id)
if id == "on" || id == "off" {
continue
}

strategyStruct, err := GetRegisteredStrategy(id)
Expand All @@ -690,6 +692,12 @@ func loadExchangeStrategies(config *Config, stash Stash) (err error) {
Mounts: mounts,
Strategy: singleExchangeStrategyInstance,
})

strategyLoaded = true
}

if !strategyLoaded {
return fmt.Errorf("strategy is not loaded, possibly caused by an incorrect config format, please check your config file")
}
}

Expand Down
10 changes: 4 additions & 6 deletions pkg/bbgo/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,12 @@ func TestLoadConfig(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config, err := Load(tt.args.configFile, true)
if err != nil {
t.Errorf("Load() error = %v", err)

if tt.wantErr {
assert.Error(t, err)
return
} else {
if tt.wantErr {
t.Errorf("Load() error = %v, wantErr %v", err, tt.wantErr)
return
}
assert.NoError(t, err)
}

assert.NotNil(t, config)
Expand Down
2 changes: 2 additions & 0 deletions pkg/bbgo/exit_trailing_stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestTrailingStop_ShortPosition(t *testing.T) {
defer mockCtrl.Finish()

mockEx := mocks.NewMockExchange(mockCtrl)
mockEx.EXPECT().Name().Return(types.ExchangeName("test")).AnyTimes()
mockEx.EXPECT().NewStream().Return(&types.StandardStream{}).Times(2)
mockEx.EXPECT().SubmitOrder(gomock.Any(), types.SubmitOrder{
Symbol: "BTCUSDT",
Expand Down Expand Up @@ -112,6 +113,7 @@ func TestTrailingStop_LongPosition(t *testing.T) {
defer mockCtrl.Finish()

mockEx := mocks.NewMockExchange(mockCtrl)
mockEx.EXPECT().Name().Return(types.ExchangeName("test")).AnyTimes()
mockEx.EXPECT().NewStream().Return(&types.StandardStream{}).Times(2)
mockEx.EXPECT().SubmitOrder(gomock.Any(), types.SubmitOrder{
Symbol: "BTCUSDT",
Expand Down

0 comments on commit 00aa355

Please sign in to comment.