From 00aa355547286bc8d63b58406d7e38feb4791ba5 Mon Sep 17 00:00:00 2001 From: c9s Date: Sun, 5 Jan 2025 16:47:37 +0800 Subject: [PATCH] fix mock tests and config loading --- pkg/bbgo/account_value_calc_test.go | 4 ++++ pkg/bbgo/config.go | 14 +++++++++++--- pkg/bbgo/config_test.go | 10 ++++------ pkg/bbgo/exit_trailing_stop_test.go | 2 ++ 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/pkg/bbgo/account_value_calc_test.go b/pkg/bbgo/account_value_calc_test.go index f9c06a43fe..06e79b54d2 100644 --- a/pkg/bbgo/account_value_calc_test.go +++ b/pkg/bbgo/account_value_calc_test.go @@ -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() @@ -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() @@ -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() diff --git a/pkg/bbgo/config.go b/pkg/bbgo/config.go index a45f3726dd..e8aa0e682e 100644 --- a/pkg/bbgo/config.go +++ b/pkg/bbgo/config.go @@ -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) @@ -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") } } diff --git a/pkg/bbgo/config_test.go b/pkg/bbgo/config_test.go index f20494c756..c7eef74756 100644 --- a/pkg/bbgo/config_test.go +++ b/pkg/bbgo/config_test.go @@ -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) diff --git a/pkg/bbgo/exit_trailing_stop_test.go b/pkg/bbgo/exit_trailing_stop_test.go index d43c6d70a5..41ad42954e 100644 --- a/pkg/bbgo/exit_trailing_stop_test.go +++ b/pkg/bbgo/exit_trailing_stop_test.go @@ -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", @@ -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",