mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 01:01:56 +00:00
trade: support custom order by column
This commit is contained in:
parent
be27d32bd8
commit
9fb273e6a1
|
@ -2,6 +2,7 @@ package service
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -32,6 +33,10 @@ type QueryTradesOptions struct {
|
|||
|
||||
// ASC or DESC
|
||||
Ordering string
|
||||
|
||||
// OrderByColumn is the column name to order by
|
||||
// Currently we only support traded_at and gid column.
|
||||
OrderByColumn string
|
||||
Limit uint64
|
||||
}
|
||||
|
||||
|
@ -304,12 +309,29 @@ func (s *TradeService) Query(options QueryTradesOptions) ([]types.Trade, error)
|
|||
sel = sel.Where(sq.Eq{"exchange": options.Sessions})
|
||||
}
|
||||
|
||||
if options.Ordering != "" {
|
||||
sel = sel.OrderBy("traded_at " + options.Ordering)
|
||||
} else {
|
||||
sel = sel.OrderBy("traded_at ASC")
|
||||
var orderByColumn string
|
||||
switch options.OrderByColumn {
|
||||
case "":
|
||||
orderByColumn = "traded_at"
|
||||
case "traded_at", "gid":
|
||||
orderByColumn = options.OrderByColumn
|
||||
default:
|
||||
return nil, fmt.Errorf("invalid order by column: %s", options.OrderByColumn)
|
||||
}
|
||||
|
||||
var ordering string
|
||||
|
||||
switch strings.ToUpper(options.Ordering) {
|
||||
case "":
|
||||
ordering = "ASC"
|
||||
case "ASC", "DESC":
|
||||
ordering = strings.ToUpper(options.Ordering)
|
||||
default:
|
||||
return nil, fmt.Errorf("invalid ordering: %s", options.Ordering)
|
||||
}
|
||||
|
||||
sel = sel.OrderBy(orderByColumn + " " + ordering)
|
||||
|
||||
if options.Limit > 0 {
|
||||
sel = sel.Limit(options.Limit)
|
||||
}
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
|
@ -87,3 +89,32 @@ func Test_queryTradesSQL(t *testing.T) {
|
|||
}))
|
||||
})
|
||||
}
|
||||
|
||||
func TestTradeService_Query(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
sqlxDB := sqlx.NewDb(db, "mysql")
|
||||
defer sqlxDB.Close()
|
||||
|
||||
s := NewTradeService(sqlxDB)
|
||||
|
||||
_, err = s.Query(QueryTradesOptions{Ordering: "test_ordering"})
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, "invalid ordering: test_ordering", err.Error())
|
||||
|
||||
_, err = s.Query(QueryTradesOptions{OrderByColumn: "invalid_column"})
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, "invalid order by column: invalid_column", err.Error())
|
||||
|
||||
mock.ExpectQuery("SELECT \\* FROM trades ORDER BY gid DESC").WillReturnError(sql.ErrNoRows)
|
||||
_, err = s.Query(QueryTradesOptions{Ordering: "DESC", OrderByColumn: "gid"})
|
||||
assert.Equal(t, sql.ErrNoRows, err)
|
||||
|
||||
mock.ExpectQuery("SELECT \\* FROM trades ORDER BY traded_at ASC").WillReturnError(sql.ErrNoRows)
|
||||
_, err = s.Query(QueryTradesOptions{Ordering: "ASC", OrderByColumn: "traded_at"})
|
||||
assert.Equal(t, sql.ErrNoRows, err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user