-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Your Name
committed
Oct 12, 2023
1 parent
14b5ff3
commit ac73df9
Showing
3 changed files
with
88 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |