Skip to content

Commit

Permalink
test: usdt perpetual linear replace order (#69)
Browse files Browse the repository at this point in the history
* test: integration

* test: unit

* docs: update
  • Loading branch information
hirokisan authored Dec 19, 2022
1 parent 7ab8e57 commit ef52081
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ The following API endpoints have been implemented
- `/private/linear/order/list` Get Active Order
- `/private/linear/order/cancel` Cancel Active Order
- `/private/linear/order/cancel-all` Cancel All Active Orders
- `/private/linear/order/replace` Replace Active Order
- `/private/linear/order/search` Query Active Order (real-time)
- `/private/linear/stop-order/create` Place Conditional Order
- `/private/linear/stop-order/list` Get Conditional Order
Expand Down
69 changes: 69 additions & 0 deletions future_usdt_perpetual_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,75 @@ func TestCancelAllLinearStopOrder(t *testing.T) {
})
}

func TestReplaceLinearOrder(t *testing.T) {
t.Run("success", func(t *testing.T) {
orderID := "test"
newPrice := 10000.0
param := ReplaceLinearOrderParam{
Symbol: SymbolFutureBTCUSDT,
OrderID: &orderID,
NewPrice: &newPrice,
}

path := "/private/linear/order/replace"
method := http.MethodPost
status := http.StatusOK
respBody := ReplaceLinearOrderResponse{
Result: ReplaceLinearOrderResult{
OrderID: "b777573b-026e-48ee-b9ac-28c81e829ae8",
},
}
bytesBody, err := json.Marshal(respBody)
require.NoError(t, err)

server, teardown := testhelper.NewServer(
testhelper.WithHandlerOption(path, method, status, bytesBody),
)
defer teardown()

client := NewTestClient().
WithBaseURL(server.URL).
WithAuth("test", "test")

resp, err := client.Future().USDTPerpetual().ReplaceLinearOrder(param)
require.NoError(t, err)

require.NotNil(t, resp)
assert.Equal(t, respBody, *resp)
})
t.Run("authentication required", func(t *testing.T) {
orderID := "test"
newPrice := 10000.0
param := ReplaceLinearOrderParam{
Symbol: SymbolFutureBTCUSDT,
OrderID: &orderID,
NewPrice: &newPrice,
}

path := "/private/linear/order/replace"
method := http.MethodPost
status := http.StatusOK
respBody := ReplaceLinearOrderResponse{
Result: ReplaceLinearOrderResult{
OrderID: "b777573b-026e-48ee-b9ac-28c81e829ae8",
},
}
bytesBody, err := json.Marshal(respBody)
require.NoError(t, err)

server, teardown := testhelper.NewServer(
testhelper.WithHandlerOption(path, method, status, bytesBody),
)
defer teardown()

client := NewTestClient().
WithBaseURL(server.URL)

_, err = client.Future().USDTPerpetual().ReplaceLinearOrder(param)
assert.Error(t, err)
})
}

func TestQueryLinearStopOrder(t *testing.T) {
t.Run("success", func(t *testing.T) {
param := QueryLinearStopOrderParam{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,52 @@ func TestLinearTradingStop(t *testing.T) {
require.NoError(t, err)
}
}

func TestReplaceLinearOrder(t *testing.T) {
t.Run("ok", func(t *testing.T) {
client := bybit.NewTestClient().WithAuthFromEnv()

symbol := bybit.SymbolFutureBTCUSDT
var orderID string
{
price := 10000.0
res, err := client.Future().USDTPerpetual().CreateLinearOrder(bybit.CreateLinearOrderParam{
Side: bybit.SideBuy,
Symbol: symbol,
OrderType: bybit.OrderTypeLimit,
Qty: 0.001,
TimeInForce: bybit.TimeInForceGoodTillCancel,
Price: &price,
})
require.NoError(t, err)
orderID = res.Result.OrderID
}
{
newPrice := 11000.0
res, err := client.Future().USDTPerpetual().ReplaceLinearOrder(bybit.ReplaceLinearOrderParam{
Symbol: symbol,
OrderID: &orderID,
NewPrice: &newPrice,
})
require.NoError(t, err)
goldenFilename := "./testdata/private-linear-order-replace.json"
testhelper.Compare(t, goldenFilename, testhelper.ConvertToJSON(res.Result))
testhelper.UpdateFile(t, goldenFilename, testhelper.ConvertToJSON(res.Result))

orderID = res.Result.OrderID
}
{
_, err := client.Future().USDTPerpetual().CancelLinearOrder(bybit.CancelLinearOrderParam{
Symbol: symbol,
OrderID: &orderID,
})
require.NoError(t, err)
}
})

t.Run("auth error", func(t *testing.T) {
client := bybit.NewTestClient()
_, err := client.Future().USDTPerpetual().ReplaceLinearOrder(bybit.ReplaceLinearOrderParam{})
require.Error(t, err)
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"order_id": "b777573b-026e-48ee-b9ac-28c81e829ae8"
}

0 comments on commit ef52081

Please sign in to comment.