github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/v2/rest/market_test.go (about)

     1  package rest_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"testing"
     8  
     9  	"github.com/bitfinexcom/bitfinex-api-go/v2/rest"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestMarketAveragePrice(t *testing.T) {
    15  	t.Run("calls with valid query params", func(t *testing.T) {
    16  		handler := func(w http.ResponseWriter, r *http.Request) {
    17  			assert.Equal(t, "/calc/trade/avg?amount=100&period=2&rate_limit=1000.5&symbol=fUSD", r.RequestURI)
    18  			assert.Equal(t, "POST", r.Method)
    19  			respMock := []interface{}{
    20  				123.123,
    21  				100,
    22  			}
    23  			payload, _ := json.Marshal(respMock)
    24  			_, err := w.Write(payload)
    25  			require.Nil(t, err)
    26  		}
    27  
    28  		server := httptest.NewServer(http.HandlerFunc(handler))
    29  		defer server.Close()
    30  
    31  		c := rest.NewClientWithURL(server.URL)
    32  		args := rest.AveragePriceRequest{
    33  			Symbol:    "fUSD",
    34  			Amount:    "100",
    35  			RateLimit: "1000.5",
    36  			Period:    2,
    37  		}
    38  
    39  		avgPrice, err := c.Market.AveragePrice(args)
    40  		require.Nil(t, err)
    41  
    42  		expected := []float64{
    43  			123.123,
    44  			100,
    45  		}
    46  		assert.Equal(t, expected, avgPrice)
    47  	})
    48  }
    49  
    50  func TestForeignExchangeRate(t *testing.T) {
    51  	t.Run("missing arguments", func(t *testing.T) {
    52  		c := rest.NewClient()
    53  		rsp, err := c.Market.ForeignExchangeRate(rest.ForeignExchangeRateRequest{
    54  			FirstCurrency: "BTC",
    55  		})
    56  
    57  		require.NotNil(t, err)
    58  		require.Nil(t, rsp)
    59  	})
    60  
    61  	t.Run("calls correct resource with correct payload", func(t *testing.T) {
    62  		handler := func(w http.ResponseWriter, r *http.Request) {
    63  			assert.Equal(t, "/calc/fx", r.RequestURI)
    64  			assert.Equal(t, "POST", r.Method)
    65  
    66  			gotReqPld := rest.ForeignExchangeRateRequest{}
    67  			err := json.NewDecoder(r.Body).Decode(&gotReqPld)
    68  			require.Nil(t, err)
    69  
    70  			expectedReqPld := rest.ForeignExchangeRateRequest{
    71  				FirstCurrency:  "BTC",
    72  				SecondCurrency: "USD",
    73  			}
    74  			assert.Equal(t, expectedReqPld, gotReqPld)
    75  
    76  			respMock := []interface{}{9151.5}
    77  			payload, _ := json.Marshal(respMock)
    78  			_, err = w.Write(payload)
    79  			require.Nil(t, err)
    80  		}
    81  
    82  		server := httptest.NewServer(http.HandlerFunc(handler))
    83  		defer server.Close()
    84  
    85  		c := rest.NewClientWithURL(server.URL)
    86  		rsp, err := c.Market.ForeignExchangeRate(rest.ForeignExchangeRateRequest{
    87  			FirstCurrency:  "BTC",
    88  			SecondCurrency: "USD",
    89  		})
    90  
    91  		require.Nil(t, err)
    92  		assert.Equal(t, float64(9151.5), rsp[0])
    93  	})
    94  }