add unit test for okex exchange

This commit is contained in:
Alan.sung 2023-07-21 17:05:19 +08:00
parent 3b68d6558e
commit cba5663fac
2 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,76 @@
package okexapi
import (
"context"
"os"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/c9s/bbgo/pkg/testutil"
)
func getTestClientOrSkip(t *testing.T) *RestClient {
if b, _ := strconv.ParseBool(os.Getenv("CI")); b {
t.Skip("skip test for CI")
}
key, secret, passphrase, ok := testutil.IntegrationTestWithPassphraseConfigured(t, "OKEX")
if !ok {
t.SkipNow()
return nil
}
client := NewClient()
client.Auth(key, secret, passphrase)
return client
}
func TestClient_GetInstrumentsRequest(t *testing.T) {
client := NewClient()
ctx := context.Background()
ser := PublicDataService{client: client}
req := ser.NewGetInstrumentsRequest()
instruments, err := req.
InstrumentType(InstrumentTypeSpot).
Do(ctx)
assert.NoError(t, err)
assert.NotEmpty(t, instruments)
t.Logf("instruments: %+v", instruments)
}
func TestClient_GetFundingRateRequest(t *testing.T) {
client := NewClient()
ctx := context.Background()
ser := PublicDataService{client: client}
req := ser.NewGetFundingRate()
instrument, err := req.
InstrumentID("BTC-USDT-SWAP").
Do(ctx)
assert.NoError(t, err)
assert.NotEmpty(t, instrument)
t.Logf("instrument: %+v", instrument)
}
func TestClient_PlaceOrderRequest(t *testing.T) {
client := getTestClientOrSkip(t)
ctx := context.Background()
ser := TradeService{client: client}
req := ser.NewPlaceOrderRequest()
order, err := req.
InstrumentID("XTZ-BTC").
TradeMode("cash").
Side(SideTypeSell).
OrderType(OrderTypeLimit).
Price("0.001").
Quantity("0.01").
Do(ctx)
assert.NoError(t, err)
assert.NotEmpty(t, order)
t.Logf("order: %+v", order) // Right now account has no money
}

View File

@ -23,3 +23,16 @@ func IntegrationTestConfigured(t *testing.T, prefix string) (key, secret string,
return key, secret, ok
}
func IntegrationTestWithPassphraseConfigured(t *testing.T, prefix string) (key, secret, passphrase string, ok bool) {
var hasKey, hasSecret, hasPassphrase bool
key, hasKey = os.LookupEnv(prefix + "_API_KEY")
secret, hasSecret = os.LookupEnv(prefix + "_API_SECRET")
passphrase, hasPassphrase = os.LookupEnv(prefix + "_API_PASSPHRASE")
ok = hasKey && hasSecret && hasPassphrase && os.Getenv("TEST_"+prefix) == "1"
if ok {
t.Logf(prefix+" api integration test enabled, key = %s, secret = %s, passphrase= %s", maskSecret(key), maskSecret(secret), maskSecret(passphrase))
}
return key, secret, passphrase, ok
}