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

     1  package bitfinex
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  func TestPositions(t *testing.T) {
    12  	httpDo = func(req *http.Request) (*http.Response, error) {
    13  		msg := `[
    14              {
    15                  "id":943715,
    16                  "symbol":"btcusd",
    17                  "status":"ACTIVE",
    18                  "base":"246.94",
    19                  "amount":"1.0",
    20                  "timestamp":"1444141857.0",
    21                  "swap":"0.0",
    22                  "pl":"-2.22042"
    23              }
    24          ]`
    25  		resp := http.Response{
    26  			Body:       ioutil.NopCloser(bytes.NewBufferString(msg)),
    27  			StatusCode: 200,
    28  		}
    29  		return &resp, nil
    30  	}
    31  
    32  	client := NewClient()
    33  	positions, err := client.Positions.All()
    34  	if err != nil {
    35  		t.Error(err)
    36  	}
    37  
    38  	if len(positions) != 1 {
    39  		t.Error("Expected", 1)
    40  		t.Error("Actual ", len(positions))
    41  	}
    42  
    43  	pos := positions[0]
    44  	if pos.Amount != "1.0" {
    45  		t.Error("Expected", "1.0")
    46  		t.Error("Actual ", pos.Amount)
    47  	}
    48  
    49  }
    50  
    51  func TestPositionsWhenEmpty(t *testing.T) {
    52  	httpDo = func(req *http.Request) (*http.Response, error) {
    53  		msg := `[{
    54              "id":943715,
    55              "symbol":"btcusd",
    56              "status":"ACTIVE",
    57              "base":"246.94",
    58              "amount":"1.0",
    59              "timestamp":"1444141857.0",
    60              "swap":"0.0",
    61              "pl":"-2.2304"
    62          }]`
    63  		resp := http.Response{
    64  			Body:       ioutil.NopCloser(bytes.NewBufferString(msg)),
    65  			StatusCode: 200,
    66  		}
    67  		return &resp, nil
    68  	}
    69  
    70  	client := NewClient()
    71  	positions, err := client.Positions.All()
    72  
    73  	if err != nil {
    74  		t.Error(err)
    75  	}
    76  
    77  	position := positions[0]
    78  	if position.ID != 943715 {
    79  		t.Error("Expected", 943715)
    80  		t.Error("Actual ", position.ID)
    81  	}
    82  
    83  	parsedTime, err := position.ParseTime()
    84  	loc, _ := time.LoadLocation("Europe/Berlin")
    85  	expectedTime := time.Date(2015, 10, 06, 16, 30, 57, 00, loc)
    86  
    87  	if err != nil {
    88  		t.Error(err)
    89  	}
    90  	if !(*parsedTime).Equal(expectedTime) {
    91  		t.Error("Expected", &expectedTime)
    92  		t.Error("Actual ", parsedTime)
    93  	}
    94  
    95  }
    96  
    97  func TestClaimPosition(t *testing.T) {
    98  
    99  	httpDo = func(req *http.Request) (*http.Response, error) {
   100  		msg := `{
   101                  "id":943715,
   102                  "symbol":"btcusd",
   103                  "status":"ACTIVE",
   104                  "base":"246.94",
   105                  "amount":"1.0",
   106                  "timestamp":"1444141857.0",
   107                  "swap":"0.0",
   108                  "pl":"-2.22042"
   109              }`
   110  		resp := http.Response{
   111  			Body:       ioutil.NopCloser(bytes.NewBufferString(msg)),
   112  			StatusCode: 200,
   113  		}
   114  		return &resp, nil
   115  	}
   116  
   117  	position, err := NewClient().Positions.Claim(943715, "0.5")
   118  
   119  	if err != nil {
   120  		t.Error(err)
   121  	}
   122  
   123  	if position.ID != 943715 {
   124  		t.Error("Expected", 943715)
   125  		t.Error("Actual ", position.ID)
   126  	}
   127  }