mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 06:53:52 +00:00
batch: add TradeQueryOptionsMatcher for testing trade query options
This commit is contained in:
parent
df125c0efb
commit
1dc1afc993
|
@ -31,6 +31,7 @@ func (e TradeBatchQuery) Query(
|
||||||
return e.ExchangeTradeHistoryService.QueryTrades(ctx, symbol, &types.TradeQueryOptions{
|
return e.ExchangeTradeHistoryService.QueryTrades(ctx, symbol, &types.TradeQueryOptions{
|
||||||
StartTime: &startTime,
|
StartTime: &startTime,
|
||||||
EndTime: &endTime,
|
EndTime: &endTime,
|
||||||
|
Limit: options.Limit,
|
||||||
LastTradeID: options.LastTradeID,
|
LastTradeID: options.LastTradeID,
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
@ -45,7 +46,7 @@ func (e TradeBatchQuery) Query(
|
||||||
|
|
||||||
return trade.Key().String()
|
return trade.Key().String()
|
||||||
},
|
},
|
||||||
JumpIfEmpty: 23 * time.Hour, // exchange may not have trades in the last 24 hours
|
JumpIfEmpty: 24*time.Hour - 5*time.Minute, // exchange may not have trades in the last 24 hours
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
|
|
|
@ -2,6 +2,7 @@ package batch
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -14,6 +15,49 @@ import (
|
||||||
"github.com/c9s/bbgo/pkg/types/mocks"
|
"github.com/c9s/bbgo/pkg/types/mocks"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func matchTradeQueryOptions(expected *types.TradeQueryOptions) *TradeQueryOptionsMatcher {
|
||||||
|
return &TradeQueryOptionsMatcher{
|
||||||
|
expected: expected,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type TradeQueryOptionsMatcher struct {
|
||||||
|
expected *types.TradeQueryOptions
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m TradeQueryOptionsMatcher) Matches(arg interface{}) bool {
|
||||||
|
given, ok := arg.(*types.TradeQueryOptions)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if given.StartTime != nil && m.expected.StartTime != nil {
|
||||||
|
if !given.StartTime.Equal(*m.expected.StartTime) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if given.EndTime != nil && m.expected.EndTime != nil {
|
||||||
|
if !given.EndTime.Equal(*m.expected.EndTime) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if given.Limit != m.expected.Limit {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if given.LastTradeID != m.expected.LastTradeID {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m TradeQueryOptionsMatcher) String() string {
|
||||||
|
return fmt.Sprintf("%+v", m.expected)
|
||||||
|
}
|
||||||
|
|
||||||
func Test_TradeBatchQuery(t *testing.T) {
|
func Test_TradeBatchQuery(t *testing.T) {
|
||||||
ctrl := gomock.NewController(t)
|
ctrl := gomock.NewController(t)
|
||||||
defer ctrl.Finish()
|
defer ctrl.Finish()
|
||||||
|
@ -55,7 +99,7 @@ func Test_TradeBatchQuery(t *testing.T) {
|
||||||
mockExchange = mocks.NewMockExchangeTradeHistoryService(ctrl)
|
mockExchange = mocks.NewMockExchangeTradeHistoryService(ctrl)
|
||||||
)
|
)
|
||||||
|
|
||||||
mockExchange.EXPECT().QueryTrades(ctx, expSymbol, expOptions).DoAndReturn(
|
mockExchange.EXPECT().QueryTrades(ctx, expSymbol, matchTradeQueryOptions(expOptions)).DoAndReturn(
|
||||||
func(ctx context.Context, symbol string, options *types.TradeQueryOptions) ([]types.Trade, error) {
|
func(ctx context.Context, symbol string, options *types.TradeQueryOptions) ([]types.Trade, error) {
|
||||||
assert.Equal(t, startTime, *options.StartTime)
|
assert.Equal(t, startTime, *options.StartTime)
|
||||||
assert.Equal(t, endTime, *options.EndTime)
|
assert.Equal(t, endTime, *options.EndTime)
|
||||||
|
@ -63,7 +107,13 @@ func Test_TradeBatchQuery(t *testing.T) {
|
||||||
assert.Equal(t, expOptions.Limit, options.Limit)
|
assert.Equal(t, expOptions.Limit, options.Limit)
|
||||||
return queryTrades1, nil
|
return queryTrades1, nil
|
||||||
}).Times(1)
|
}).Times(1)
|
||||||
mockExchange.EXPECT().QueryTrades(ctx, expSymbol, expOptions).DoAndReturn(
|
|
||||||
|
mockExchange.EXPECT().QueryTrades(ctx, expSymbol, matchTradeQueryOptions(&types.TradeQueryOptions{
|
||||||
|
StartTime: timePtr(queryTrades1[0].Time.Time()),
|
||||||
|
EndTime: expOptions.EndTime,
|
||||||
|
LastTradeID: 1,
|
||||||
|
Limit: 50,
|
||||||
|
})).DoAndReturn(
|
||||||
func(ctx context.Context, symbol string, options *types.TradeQueryOptions) ([]types.Trade, error) {
|
func(ctx context.Context, symbol string, options *types.TradeQueryOptions) ([]types.Trade, error) {
|
||||||
assert.Equal(t, queryTrades1[0].Time.Time(), *options.StartTime)
|
assert.Equal(t, queryTrades1[0].Time.Time(), *options.StartTime)
|
||||||
assert.Equal(t, endTime, *options.EndTime)
|
assert.Equal(t, endTime, *options.EndTime)
|
||||||
|
@ -71,7 +121,13 @@ func Test_TradeBatchQuery(t *testing.T) {
|
||||||
assert.Equal(t, expOptions.Limit, options.Limit)
|
assert.Equal(t, expOptions.Limit, options.Limit)
|
||||||
return queryTrades2, nil
|
return queryTrades2, nil
|
||||||
}).Times(1)
|
}).Times(1)
|
||||||
mockExchange.EXPECT().QueryTrades(ctx, expSymbol, expOptions).DoAndReturn(
|
|
||||||
|
mockExchange.EXPECT().QueryTrades(ctx, expSymbol, matchTradeQueryOptions(&types.TradeQueryOptions{
|
||||||
|
StartTime: timePtr(queryTrades2[1].Time.Time()),
|
||||||
|
EndTime: expOptions.EndTime,
|
||||||
|
LastTradeID: queryTrades2[1].ID,
|
||||||
|
Limit: 50,
|
||||||
|
})).DoAndReturn(
|
||||||
func(ctx context.Context, symbol string, options *types.TradeQueryOptions) ([]types.Trade, error) {
|
func(ctx context.Context, symbol string, options *types.TradeQueryOptions) ([]types.Trade, error) {
|
||||||
assert.Equal(t, queryTrades2[1].Time.Time(), *options.StartTime)
|
assert.Equal(t, queryTrades2[1].Time.Time(), *options.StartTime)
|
||||||
assert.Equal(t, endTime, *options.EndTime)
|
assert.Equal(t, endTime, *options.EndTime)
|
||||||
|
@ -138,3 +194,7 @@ func Test_TradeBatchQuery(t *testing.T) {
|
||||||
assert.Equal(t, rcvCount, 0)
|
assert.Equal(t, rcvCount, 0)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func timePtr(t time.Time) *time.Time {
|
||||||
|
return &t
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user