github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/v1/orders_test.go (about)

     1  package bitfinex
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"testing"
     8  )
     9  
    10  func TestOrdersAll(t *testing.T) {
    11  	httpDo = func(req *http.Request) (*http.Response, error) {
    12  		msg := `
    13          [{
    14             "id":448411365,
    15             "symbol":"btcusd",
    16             "exchange":"bitfinex",
    17             "price":"0.02",
    18             "avg_execution_price":"0.0",
    19             "side":"buy",
    20             "type":"exchange limit",
    21             "timestamp":"1444276597.0",
    22             "is_live":true,
    23             "is_cancelled":false,
    24             "is_hidden":false,
    25             "was_forced":false,
    26             "original_amount":"0.02",
    27             "remaining_amount":"0.02",
    28             "executed_amount":"0.0"
    29           }]`
    30  		resp := http.Response{
    31  			Body:       ioutil.NopCloser(bytes.NewBufferString(msg)),
    32  			StatusCode: 200,
    33  		}
    34  		return &resp, nil
    35  	}
    36  
    37  	orders, err := NewClient().Orders.All()
    38  
    39  	if err != nil {
    40  		t.Error(err)
    41  	}
    42  
    43  	expectedID := int64(448411365)
    44  	if orders[0].ID != expectedID {
    45  		t.Error("Expected", expectedID)
    46  		t.Error("Actual ", orders[0].ID)
    47  	}
    48  
    49  }
    50  
    51  func TestCreateMulti(t *testing.T) {
    52  	httpDo = func(req *http.Request) (*http.Response, error) {
    53  		msg := `{
    54              "order_ids":[{
    55              "id":448383727,
    56              "symbol":"btcusd",
    57              "exchange":"bitfinex",
    58              "price":"0.01",
    59              "avg_execution_price":"0.0",
    60              "side":"buy",
    61              "type":"exchange limit",
    62              "timestamp":"1444274013.621701916",
    63              "is_live":true,
    64              "is_cancelled":false,
    65              "is_hidden":false,
    66              "was_forced":false,
    67              "original_amount":"0.01",
    68              "remaining_amount":"0.01",
    69              "executed_amount":"0.0"
    70           },{
    71              "id":448383729,
    72              "symbol":"btcusd",
    73              "exchange":"bitfinex",
    74              "price":"0.03",
    75              "avg_execution_price":"0.0",
    76              "side":"buy",
    77              "type":"exchange limit",
    78              "timestamp":"1444274013.661297306",
    79              "is_live":true,
    80              "is_cancelled":false,
    81              "is_hidden":false,
    82              "was_forced":false,
    83              "original_amount":"0.02",
    84              "remaining_amount":"0.02",
    85              "executed_amount":"0.0"
    86            }],
    87            "status":"success"
    88         }`
    89  		resp := http.Response{
    90  			Body:       ioutil.NopCloser(bytes.NewBufferString(msg)),
    91  			StatusCode: 200,
    92  		}
    93  		return &resp, nil
    94  	}
    95  
    96  	reqOrders := []SubmitOrder{{
    97  		Symbol: "BTCUSD",
    98  		Amount: 10.0,
    99  		Price:  450.0,
   100  		Type:   OrderTypeLimit,
   101  	}, {
   102  		Symbol: "BTCUSD",
   103  		Amount: 10.0,
   104  		Price:  450.0,
   105  		Type:   OrderTypeLimit,
   106  	}}
   107  	response, err := NewClient().Orders.CreateMulti(reqOrders)
   108  
   109  	if err != nil {
   110  		t.Error(err)
   111  	}
   112  
   113  	if len(response.Orders) != 2 {
   114  		t.Error("Expected", 2)
   115  		t.Error("Actual ", len(response.Orders))
   116  	}
   117  }
   118  
   119  func TestCancelMulti(t *testing.T) {
   120  	httpDo = func(req *http.Request) (*http.Response, error) {
   121  		msg := `{"result":"Orders cancelled"}`
   122  		resp := http.Response{
   123  			Body:       ioutil.NopCloser(bytes.NewBufferString(msg)),
   124  			StatusCode: 200,
   125  		}
   126  		return &resp, nil
   127  	}
   128  
   129  	orders := []int64{1000, 1001, 1002}
   130  	response, err := NewClient().Orders.CancelMulti(orders)
   131  
   132  	if err != nil {
   133  		t.Error(err)
   134  	}
   135  
   136  	if response != "Orders cancelled" {
   137  		t.Error("Expected", "Orders cancelled")
   138  		t.Error("Actual ", response)
   139  	}
   140  }