Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
liaodong authored and liaodong committed Oct 12, 2023
1 parent f1c3f7d commit d6548dc
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 23 deletions.
6 changes: 6 additions & 0 deletions cmd/haobase/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ type assetsFreeze struct {
CreateTime time.Time `xorm:"timestamp created"`
UpdateTime time.Time `xorm:"timestamp updated"`
}

func FindSymbol(user_id string, symbol string) *Assets {
var row Assets
db_engine.Table(new(Assets)).Where("user_id=? and symbol=?", user_id, symbol).Get(&row)
return &row
}
57 changes: 44 additions & 13 deletions cmd/haobase/clearing/clearing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import (
)

var (
sellUser = "user1"
buyUser = "user2"
testSymbol = "usdjpy"
sellUser = "user1"
buyUser = "user2"
testSymbol = "usdjpy"
testTargetSymbol = "usd"
testStandardSymbol = "jpy"
)

func initdb(t *testing.T) {
Expand All @@ -36,9 +38,20 @@ func initdb(t *testing.T) {
db.ShowSQL(true)

cleanAssets(t)
cleanOrders(t)

}

func initAssets(t *testing.T) {
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 cleanAssets(t *testing.T) {
base.DB().DropIndexes(new(assets.Assets))
base.DB().DropIndexes("assets_freeze")
Expand All @@ -48,13 +61,6 @@ func cleanAssets(t *testing.T) {
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 cleanOrders(t *testing.T) {
Expand All @@ -70,7 +76,11 @@ func cleanOrders(t *testing.T) {

func TestLimitOrder(t *testing.T) {
initdb(t)
Convey("限价单结算测试", t, func() {
Convey("限价单完全成交结算测试", t, func() {
initAssets(t)
// defer cleanOrders(t)
// defer cleanAssets(t)

sell, err := orders.NewLimitOrder(sellUser, testSymbol, trading_engine.OrderSideSell, "1.00", "1")
So(err, ShouldBeNil)

Expand All @@ -86,8 +96,29 @@ func TestLimitOrder(t *testing.T) {
TradeTime: time.Now().UnixNano(),
}
newClean(result)
// So()

// cleanOrders(t)
//检查资产
sell_assets_target := assets.FindSymbol(sellUser, testTargetSymbol)
sell_assets_standard := assets.FindSymbol(sellUser, testStandardSymbol)

buy_assets_target := assets.FindSymbol(buyUser, testTargetSymbol)
buy_assets_standard := assets.FindSymbol(buyUser, testStandardSymbol)
So(utils.D(sell_assets_target.Total), ShouldEqual, utils.D("9999"))
So(utils.D(sell_assets_standard.Total), ShouldEqual, utils.D("10000.995"))

So(utils.D(buy_assets_target.Total), ShouldEqual, utils.D("10001"))
So(utils.D(buy_assets_standard.Total), ShouldEqual, utils.D("9998.995"))

//检查订单状态
sell_order := orders.Find(testSymbol, sell.OrderId)
So(sell_order.Status, ShouldEqual, orders.OrderStatusDone)
buy_order := orders.Find(testSymbol, buy.OrderId)
So(buy_order.Status, ShouldEqual, orders.OrderStatusDone)

sell_unfinished := orders.FindUnfinished(testSymbol, sell.OrderId)
So(sell_unfinished, ShouldBeNil)
buy_unfinished := orders.FindUnfinished(testSymbol, buy.OrderId)
So(buy_unfinished, ShouldBeNil)

})
}
31 changes: 22 additions & 9 deletions cmd/haobase/orders/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/yzimhao/trading_engine"
"github.com/yzimhao/trading_engine/cmd/haobase/assets"
"xorm.io/xorm"
)

Expand Down Expand Up @@ -38,41 +39,53 @@ type Order struct {
UpdateTime time.Time `xorm:"timestamp updated" json:"-"`
}

func (to *Order) Save(db *xorm.Session) error {
func (o *Order) Save(db *xorm.Session) error {
//todo 频繁查询表是否存在,后面考虑缓存一下
exist, err := db.IsTableExist(to.TableName())
exist, err := db.IsTableExist(o.TableName())
if err != nil {
return err
}
if !exist {
err := db.CreateTable(to)
err := db.CreateTable(o)
if err != nil {
return err
}

err = db.CreateIndexes(to)
err = db.CreateIndexes(o)
if err != nil {
return err
}

err = db.CreateUniques(to)
err = db.CreateUniques(o)
if err != nil {
return err
}
}

to.CreateTime = time.Now().UnixNano()
_, err = db.Table(to).Insert(to)
o.CreateTime = time.Now().UnixNano()
_, err = db.Table(o).Insert(o)
if err != nil {
return err
}
return nil
}

func (to *Order) TableName() string {
return fmt.Sprintf("order_%s", to.Symbol)
func (o *Order) TableName() string {
return GetOrderTableName(o.Symbol)
}

func GetOrderTableName(symbol string) string {
return fmt.Sprintf("order_%s", symbol)
}

func Find(symbol string, order_id string) *Order {
db := assets.DB().NewSession()
defer db.Close()

var row Order
db.Table(GetOrderTableName(symbol)).Where("order_id=?", order_id).Get(&row)
if row.Id > 0 {
return &row
}
return nil
}
17 changes: 16 additions & 1 deletion cmd/haobase/orders/unfinished_order.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package orders

import "xorm.io/xorm"
import (
"github.com/yzimhao/trading_engine/cmd/haobase/assets"
"xorm.io/xorm"
)

// 未完全成交的委托订单记录表
type UnfinishedOrder struct {
Expand Down Expand Up @@ -40,3 +43,15 @@ func (u *UnfinishedOrder) Create(db *xorm.Session) error {
}
return nil
}

func FindUnfinished(symbol string, order_id string) *Order {
db := assets.DB().NewSession()
defer db.Close()

var row Order
db.Table(new(UnfinishedOrder)).Where("order_id=?", order_id).Get(&row)
if row.Id > 0 {
return &row
}
return nil
}

0 comments on commit d6548dc

Please sign in to comment.