2020-10-12 09:33:02 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2020-10-12 14:46:06 +00:00
|
|
|
"time"
|
2020-10-12 09:33:02 +00:00
|
|
|
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/exchange/binance"
|
|
|
|
"github.com/c9s/bbgo/pkg/service"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTradeService(t *testing.T) {
|
|
|
|
db, mock, err := sqlmock.New()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_ = mock
|
|
|
|
|
|
|
|
xdb := sqlx.NewDb(db, "mysql")
|
|
|
|
service.NewTradeService(xdb)
|
|
|
|
/*
|
|
|
|
|
2020-10-26 07:31:13 +00:00
|
|
|
stmt := mock.ExpectQuery(`SELECT \* FROM trades WHERE symbol = \? ORDER BY gid DESC LIMIT 1`)
|
|
|
|
stmt.WithArgs("BTCUSDT")
|
|
|
|
stmt.WillReturnRows(sqlmock.NewRows([]string{"gid", "id", "exchange", "symbol", "price", "quantity"}))
|
2020-10-12 09:33:02 +00:00
|
|
|
|
2020-10-26 07:31:13 +00:00
|
|
|
stmt2 := mock.ExpectQuery(`INSERT INTO trades (id, exchange, symbol, price, quantity, quote_quantity, side, is_buyer, is_maker, fee, fee_currency, traded_at)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
|
|
|
stmt2.WithArgs()
|
2020-10-12 09:33:02 +00:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironment_Connect(t *testing.T) {
|
|
|
|
mysqlURL := os.Getenv("MYSQL_URL")
|
|
|
|
if len(mysqlURL) == 0 {
|
|
|
|
t.Skip("require mysql url")
|
|
|
|
}
|
|
|
|
mysqlURL = fmt.Sprintf("%s?parseTime=true", mysqlURL)
|
|
|
|
|
|
|
|
key, secret := os.Getenv("BINANCE_API_KEY"), os.Getenv("BINANCE_API_SECRET")
|
|
|
|
if len(key) == 0 || len(secret) == 0 {
|
|
|
|
t.Skip("require key and secret")
|
|
|
|
}
|
|
|
|
|
|
|
|
exchange := binance.New(key, secret)
|
|
|
|
assert.NotNil(t, exchange)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2020-10-26 07:06:39 +00:00
|
|
|
environment := NewEnvironment()
|
2020-10-12 09:33:02 +00:00
|
|
|
environment.AddExchange("binance", exchange).
|
2020-10-26 07:31:13 +00:00
|
|
|
Subscribe(types.KLineChannel, "BTCUSDT", types.SubscribeOptions{})
|
2020-10-12 09:33:02 +00:00
|
|
|
|
2020-10-26 07:31:13 +00:00
|
|
|
err := environment.Connect(ctx)
|
2020-10-12 09:33:02 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
}
|