mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 00:35:15 +00:00
Merge pull request #103 from ycdesu/minor/order/tests
This commit is contained in:
commit
d821d2ef95
|
@ -58,6 +58,23 @@ type QueryOrdersOptions struct {
|
|||
}
|
||||
|
||||
func (s *OrderService) Query(options QueryOrdersOptions) ([]AggOrder, error) {
|
||||
sql := genOrderSQL(options)
|
||||
|
||||
rows, err := s.DB.NamedQuery(sql, map[string]interface{}{
|
||||
"exchange": options.Exchange,
|
||||
"symbol": options.Symbol,
|
||||
"gid": options.LastGID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
return s.scanAggRows(rows)
|
||||
}
|
||||
|
||||
func genOrderSQL(options QueryOrdersOptions) string {
|
||||
// ascending
|
||||
ordering := "ASC"
|
||||
switch v := strings.ToUpper(options.Ordering); v {
|
||||
|
@ -93,19 +110,7 @@ func (s *OrderService) Query(options QueryOrdersOptions) ([]AggOrder, error) {
|
|||
sql += ` LIMIT ` + strconv.Itoa(500)
|
||||
|
||||
log.Info(sql)
|
||||
|
||||
rows, err := s.DB.NamedQuery(sql, map[string]interface{}{
|
||||
"exchange": options.Exchange,
|
||||
"symbol": options.Symbol,
|
||||
"gid": options.LastGID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
return s.scanAggRows(rows)
|
||||
return sql
|
||||
}
|
||||
|
||||
func (s *OrderService) scanAggRows(rows *sqlx.Rows) (orders []AggOrder, err error) {
|
||||
|
|
24
pkg/service/order_test.go
Normal file
24
pkg/service/order_test.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_genOrderSQL(t *testing.T) {
|
||||
t.Run("accept empty options", func(t *testing.T) {
|
||||
o := QueryOrdersOptions{}
|
||||
assert.Equal(t, "SELECT orders.*, IFNULL(SUM(t.price * t.quantity)/SUM(t.quantity), orders.price) AS average_price FROM orders LEFT JOIN trades AS t ON (t.order_id = orders.order_id) GROUP BY orders.gid ORDER BY orders.gid ASC LIMIT 500", genOrderSQL(o))
|
||||
})
|
||||
|
||||
t.Run("different ordering ", func(t *testing.T) {
|
||||
o := QueryOrdersOptions{}
|
||||
assert.Equal(t, "SELECT orders.*, IFNULL(SUM(t.price * t.quantity)/SUM(t.quantity), orders.price) AS average_price FROM orders LEFT JOIN trades AS t ON (t.order_id = orders.order_id) GROUP BY orders.gid ORDER BY orders.gid ASC LIMIT 500", genOrderSQL(o))
|
||||
o.Ordering = "ASC"
|
||||
assert.Equal(t, "SELECT orders.*, IFNULL(SUM(t.price * t.quantity)/SUM(t.quantity), orders.price) AS average_price FROM orders LEFT JOIN trades AS t ON (t.order_id = orders.order_id) GROUP BY orders.gid ORDER BY orders.gid ASC LIMIT 500", genOrderSQL(o))
|
||||
o.Ordering = "DESC"
|
||||
assert.Equal(t, "SELECT orders.*, IFNULL(SUM(t.price * t.quantity)/SUM(t.quantity), orders.price) AS average_price FROM orders LEFT JOIN trades AS t ON (t.order_id = orders.order_id) GROUP BY orders.gid ORDER BY orders.gid DESC LIMIT 500", genOrderSQL(o))
|
||||
})
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user