Skip to content

Commit

Permalink
add stream_test.go unit test, change Arg []WebsocketSubscription to A…
Browse files Browse the repository at this point in the history
…rg WebsocketSubscription in Op event, Not slice
  • Loading branch information
Alan.sung authored and Alan.sung committed Oct 2, 2023
1 parent 19713e3 commit 32639bc
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 10 deletions.
6 changes: 3 additions & 3 deletions pkg/exchange/okex/okexapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ type BalanceDetail struct {
}

type Account struct {
TotalEquityInUSD fixedpoint.Value `json:"totalEq"`
UpdateTime string `json:"uTime"`
Details []BalanceDetail `json:"details"`
TotalEquityInUSD fixedpoint.Value `json:"totalEq"`
UpdateTime types.MillisecondTimestamp `json:"uTime"`
Details []BalanceDetail `json:"details"`
}

func (c *RestClient) AccountBalances(ctx context.Context) (*Account, error) {
Expand Down
10 changes: 8 additions & 2 deletions pkg/exchange/okex/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/c9s/bbgo/pkg/types"
)

func parseWebSocketEvent(in []byte) (interface{}, error) {
func (s *Stream) parseWebSocketEvent(in []byte) (interface{}, error) {
var e WsEvent

err := json.Unmarshal(in, &e)
Expand All @@ -24,7 +24,13 @@ func parseWebSocketEvent(in []byte) (interface{}, error) {
}
switch {
case e.IsOp():
return e.WebSocketOpEvent, nil
// need unmarshal again because arg in both WebSocketOpEvent and WebSocketPushDataEvent
var opEvent WebSocketOpEvent
err := json.Unmarshal(in, &opEvent)
if err != nil {
return nil, err
}
return &opEvent, nil
case e.IsPushDataEvent():
// need unmarshal again because arg in both WebSocketOpEvent and WebSocketPushDataEvent
var pushDataEvent WebSocketPushDataEvent
Expand Down
2 changes: 1 addition & 1 deletion pkg/exchange/okex/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewStream(client *okexapi.RestClient) *Stream {
lastCandle: make(map[CandleKey]Candle),
}

stream.SetParser(parseWebSocketEvent)
stream.SetParser(stream.parseWebSocketEvent)
stream.SetDispatcher(stream.dispatchEvent)
stream.SetEndpointCreator(stream.createEndpoint)

Expand Down
126 changes: 126 additions & 0 deletions pkg/exchange/okex/stream_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package okex

import (
"os"
"strconv"
"testing"

"github.com/c9s/bbgo/pkg/exchange/okex/okexapi"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/testutil"
"github.com/c9s/bbgo/pkg/types"
"github.com/stretchr/testify/assert"
)

func getTestClientOrSkip(t *testing.T) *Stream {
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.Skip("Please configure all credentials about OKEX")
return nil
}

exchange := New(key, secret, passphrase)
return NewStream(exchange.client)
}

func TestStream_parseWebSocketEvent(t *testing.T) {
s := Stream{}

t.Run("op", func(t *testing.T) {
input := `{
"event": "subscribe",
"arg": {
"channel": "tickers",
"instId": "LTC-USDT"
}
}`
res, err := s.parseWebSocketEvent([]byte(input))
assert.NoError(t, err)
opEvent, ok := res.(*WebSocketOpEvent)
assert.True(t, ok)
assert.Equal(t, WebSocketOpEvent{
Event: WsOpTypeSubscribe,
Arg: WebsocketSubscription{
Channel: WebSocketChannelType("tickers"),
InstrumentID: "LTC-USDT",
},
}, *opEvent)
})

t.Run("account event", func(t *testing.T) {
input := `{
"arg": {
"channel": "account",
"ccy": "BTC",
"uid": "77982378738415879"
},
"data": [
{
"uTime": "1597026383085",
"totalEq": "41624.32",
"isoEq": "3624.32",
"adjEq": "41624.32",
"ordFroz": "0",
"imr": "4162.33",
"mmr": "4",
"borrowFroz": "",
"notionalUsd": "",
"mgnRatio": "41624.32",
"details": [
{
"availBal": "",
"availEq": "1",
"ccy": "BTC",
"cashBal": "1",
"uTime": "1617279471503",
"disEq": "50559.01",
"eq": "1",
"eqUsd": "45078.3790756226851775",
"frozenBal": "0",
"interest": "0",
"isoEq": "0",
"liab": "0",
"maxLoan": "",
"mgnRatio": "",
"notionalLever": "0.0022195262185864",
"ordFrozen": "0",
"upl": "0",
"uplLiab": "0",
"crossLiab": "0",
"isoLiab": "0",
"coinUsdPrice": "60000",
"stgyEq":"0",
"spotInUseAmt":"",
"isoUpl":"",
"borrowFroz": ""
}
]
}
]
}`
res, err := s.parseWebSocketEvent([]byte(input))
assert.NoError(t, err)
opEvent, ok := res.(*okexapi.Account)
assert.True(t, ok)
assert.Equal(t, okexapi.Account{
TotalEquityInUSD: fixedpoint.MustNewFromString("41624.32"),
UpdateTime: types.MustParseMillisecondTimestamp("1597026383085"),
Details: []okexapi.BalanceDetail{
{Currency: "BTC",
Available: fixedpoint.MustNewFromString("1"),
CashBalance: fixedpoint.MustNewFromString("1"),
OrderFrozen: fixedpoint.MustNewFromString("0"),
Frozen: fixedpoint.MustNewFromString("0"),
Equity: fixedpoint.MustNewFromString("1"),
EquityInUSD: fixedpoint.MustNewFromString("45078.3790756226851775"),
UpdateTime: types.MustParseMillisecondTimestamp("1617279471503"),
UnrealizedProfitAndLoss: fixedpoint.MustNewFromString("0"),
},
},
}, *opEvent)
})
}
9 changes: 5 additions & 4 deletions pkg/exchange/okex/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ type WebSocketOpEvent struct {
Op WsOpType `json:"op"`
Args []WebsocketSubscription `json:"args,omitempty"`
// Below is Websocket Response field
Event WsOpType `json:"event,omitempty"`
Code string `json:"code,omitempty"`
Message string `json:"msg,omitempty"`
Arg []WebsocketSubscription `json:"arg,omitempty"`
Event WsOpType `json:"event,omitempty"`
Code string `json:"code,omitempty"`
Message string `json:"msg,omitempty"`
Arg WebsocketSubscription `json:"arg,omitempty"`
}

// Websocket Response event for private channel
Expand Down Expand Up @@ -141,6 +141,7 @@ type WebsocketSubscription struct {
TdMode string `json:"tdMode,omitempty"`
OrderType string `json:"ordType,omitempty"`
Quantity fixedpoint.Value `json:"sz,omitempty"`
Currency string `json:"ccy,omitempty"`
// below for op login
Key string `json:"apiKey,omitempty"`
Passphrase string `json:"passphrase,omitempty"`
Expand Down

0 comments on commit 32639bc

Please sign in to comment.