Skip to content

Commit

Permalink
clearing -test
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Oct 12, 2023
1 parent 14b5ff3 commit ac73df9
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 66 deletions.
16 changes: 8 additions & 8 deletions cmd/haobase/clearing/clearing_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
type clean struct {
db *xorm.Session
trading_varieties *symbols.TradingVarieties
ask *orders.Order
bid *orders.Order
ask orders.Order
bid orders.Order
raw trading_engine.TradeResult
tradelog orders.TradeLog
err error
Expand All @@ -28,8 +28,10 @@ func newClean(raw trading_engine.TradeResult) {

item := clean{
db: db,
raw: raw,
trading_varieties: symbols.NewTradingVarieties(raw.Symbol),
ask: orders.Order{},
bid: orders.Order{},
raw: raw,
}

item.flow()
Expand All @@ -48,20 +50,18 @@ func (c *clean) flow() {

c.err = c.check_order()
c.err = c.trade_log()

c.err = c.update_order(trading_engine.OrderSideSell)
c.err = c.update_order(trading_engine.OrderSideBuy)

c.err = c.transfer()
}

func (c *clean) check_order() error {
_, err := c.db.Table(orders.GetOrderTableName(c.raw.Symbol)).Where("order_id=?", c.raw.AskOrderId).ForUpdate().Get(c.ask)
_, err := c.db.Table(orders.GetOrderTableName(c.raw.Symbol)).Where("order_id=?", c.raw.AskOrderId).ForUpdate().Get(&c.ask)
if err != nil {
return err
}

_, err = c.db.Table(orders.GetOrderTableName(c.raw.Symbol)).Where("order_id=?", c.raw.BidOrderId).ForUpdate().Get(c.bid)
_, err = c.db.Table(orders.GetOrderTableName(c.raw.Symbol)).Where("order_id=?", c.raw.BidOrderId).ForUpdate().Get(&c.bid)
if err != nil {
return err
}
Expand Down Expand Up @@ -99,7 +99,7 @@ func (c *clean) trade_log() error {
}

func (c *clean) update_order(side trading_engine.OrderSide) error {
var order *orders.Order
var order orders.Order
if side == trading_engine.OrderSideSell {
order = c.ask
order.Fee = utils.D(order.Fee).Add(utils.D(c.tradelog.AskFee)).String()
Expand Down
80 changes: 80 additions & 0 deletions cmd/haobase/clearing/clearing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package clearing

import (
"testing"
"time"

"github.com/redis/go-redis/v9"
"github.com/yzimhao/trading_engine"
"github.com/yzimhao/trading_engine/cmd/haobase/assets"
"github.com/yzimhao/trading_engine/cmd/haobase/base"
"github.com/yzimhao/trading_engine/cmd/haobase/base/symbols"
"github.com/yzimhao/trading_engine/cmd/haobase/orders"
"github.com/yzimhao/trading_engine/utils"
"xorm.io/xorm"

_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
. "github.com/smartystreets/goconvey/convey"
)

var (
sellUser = "user1"
buyUser = "user2"
testSymbol = "usdjpy"
)

func initdb(t *testing.T) {
db, err := xorm.NewEngine("mysql", "root:root@tcp(localhost:3306)/test?charset=utf8&loc=Local")
if err != nil {
t.Logf("mysql err: %s", err)
}

rdc := redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379", DB: 0})
base.Init(db, rdc)
db.ShowSQL(true)

cleanAssets(t)

}

func cleanAssets(t *testing.T) {
base.DB().DropIndexes(new(assets.Assets))
base.DB().DropIndexes("assets_freeze")
base.DB().DropIndexes("assets_log")
err := base.DB().DropTables(new(assets.Assets), "assets_freeze", "assets_log")
if err != nil {
t.Logf("mysql droptables: %s", err)
}

assets.Init(base.DB(), base.RDC())
symbols.DemoData()

assets.SysRecharge("user1", "usd", "10000.00", "C001")
assets.SysRecharge("user1", "jpy", "10000.00", "C001")
assets.SysRecharge("user2", "usd", "10000.00", "C001")
assets.SysRecharge("user2", "jpy", "10000.00", "C001")
}

func TestLimitOrder(t *testing.T) {
initdb(t)
Convey("限价单结算测试", t, func() {
sell, err := orders.NewLimitOrder(sellUser, testSymbol, trading_engine.OrderSideSell, "1.00", "1")
So(err, ShouldBeNil)

buy, err := orders.NewLimitOrder(buyUser, testSymbol, trading_engine.OrderSideBuy, "1.00", "1")
So(err, ShouldBeNil)

result := trading_engine.TradeResult{
Symbol: testSymbol,
AskOrderId: sell.OrderId,
BidOrderId: buy.OrderId,
TradePrice: utils.D("1.00"),
TradeQuantity: utils.D("1"),
TradeTime: time.Now().UnixNano(),
}
newClean(result)
// So()
})
}
58 changes: 0 additions & 58 deletions cmd/haobase/orders/limit_order_test.go
Original file line number Diff line number Diff line change
@@ -1,59 +1 @@
package orders

import (
"testing"

"github.com/redis/go-redis/v9"
"github.com/yzimhao/trading_engine"
"github.com/yzimhao/trading_engine/cmd/haobase/assets"
"github.com/yzimhao/trading_engine/cmd/haobase/base"
"github.com/yzimhao/trading_engine/cmd/haobase/base/symbols"
"xorm.io/xorm"

_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
. "github.com/smartystreets/goconvey/convey"
)

var (
sellUser = "user1"
buyUser = "user2"
testSymbol = "usdjpy"
)

func initdb(t *testing.T) {
db, err := xorm.NewEngine("mysql", "root:root@tcp(localhost:3306)/test?charset=utf8&loc=Local")
if err != nil {
t.Logf("mysql err: %s", err)
}

rdc := redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379", DB: 0})
base.Init(db, rdc)

db.ShowSQL(true)

cleanAssets(t)
assets.Init(db, rdc)

symbols.DemoData()
assets.DemoData()
}

func cleanAssets(t *testing.T) {
base.DB().DropIndexes(new(assets.Assets))
base.DB().DropIndexes("assets_freeze")
base.DB().DropIndexes("assets_log")
err := base.DB().DropTables(new(assets.Assets), "assets_freeze", "assets_log")
if err != nil {
t.Logf("mysql droptables: %s", err)
}
}

func TestLimitOrder(t *testing.T) {
initdb(t)
Convey("限价卖单下单测试", t, func() {
_, err := NewLimitOrder(sellUser, testSymbol, trading_engine.OrderSideSell, "1.00", "1")
So(err, ShouldBeNil)
})
}

0 comments on commit ac73df9

Please sign in to comment.