From 72b1877e4a63d23bea05020aa35bef1d210b3deb Mon Sep 17 00:00:00 2001 From: ycdesu Date: Wed, 3 Feb 2021 17:12:06 +0800 Subject: [PATCH] order: extract query order sql generator --- pkg/service/order.go | 31 ++++++++++++++++++------------- pkg/service/order_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 pkg/service/order_test.go diff --git a/pkg/service/order.go b/pkg/service/order.go index 422685fbc..3ff86124e 100644 --- a/pkg/service/order.go +++ b/pkg/service/order.go @@ -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) { diff --git a/pkg/service/order_test.go b/pkg/service/order_test.go new file mode 100644 index 000000000..d7efd534e --- /dev/null +++ b/pkg/service/order_test.go @@ -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)) + }) + +}