github.com/polygon-io/client-go@v1.16.4/rest/summaries_test.go (about)

     1  package polygon_test
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"testing"
     7  
     8  	"github.com/jarcoal/httpmock"
     9  	polygon "github.com/polygon-io/client-go/rest"
    10  	"github.com/polygon-io/client-go/rest/models"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestGetSummary(t *testing.T) {
    15  	c := polygon.New("API_KEY")
    16  
    17  	httpmock.ActivateNonDefault(c.HTTP.GetClient())
    18  	defer httpmock.DeactivateAndReset()
    19  	expectedSummaryResponse := `{
    20  		"request_id": "abc123",
    21  		"results": [
    22  		 {
    23  		  "branding": {
    24  		   "icon_url": "https://api.polygon.io/icon.png",
    25  		   "logo_url": "https://api.polygon.io/logo.svg"
    26  		  },
    27  		  "market_status": "closed",
    28  		  "name": "Norwegian Cruise Lines",
    29  		  "price": 22.3,
    30  		  "session": {
    31  		   "change": -1.05,
    32  		   "change_percent": -4.67,
    33  		   "close": 21.4,
    34  		   "early_trading_change": -0.39,
    35  		   "early_trading_change_percent": -0.07,
    36  		   "high": 22.49,
    37  		   "late_trading_change": 1.2,
    38  		   "late_trading_change_percent": 3.92,
    39  		   "low": 21.35,
    40  		   "open": 22.49,
    41  		   "previous_close": 22.45,
    42  		   "volume": 37
    43  		  },
    44  		  "ticker": "NCLH",
    45  		  "type": "stocks"
    46  		 },
    47  		 {
    48  		  "market_status": "closed",
    49  		  "name": "NCLH $5 Call",
    50  		  "options": {
    51  		   "contract_type": "call",
    52  		   "exercise_style": "american",
    53  		   "expiration_date": "2022-10-14",
    54  		   "shares_per_contract": 100,
    55  		   "strike_price": 5,
    56  		   "underlying_ticker": "NCLH"
    57  		  },
    58  		  "price": 6.6,
    59  		  "session": {
    60  		   "change": -0.05,
    61  		   "change_percent": -1.07,
    62  		   "close": 6.65,
    63  		   "early_trading_change": -0.01,
    64  		   "early_trading_change_percent": -0.03,
    65  		   "high": 7.01,
    66  		   "late_trading_change": -0.4,
    67  		   "late_trading_change_percent": -0.02,
    68  		   "low": 5.42,
    69  		   "open": 6.7,
    70  		   "previous_close": 6.71,
    71  		   "volume": 67
    72  		  },
    73  		  "ticker": "O:NCLH221014C00005000",
    74  		  "type": "options"
    75  		 },
    76  		 {
    77  		  "market_status": "open",
    78  		  "name": "Euro - United States Dollar",
    79  		  "price": 0.97989,
    80  		  "session": {
    81  		   "change": -0.0001,
    82  		   "change_percent": -0.67,
    83  		   "close": 0.97989,
    84  		   "high": 0.98999,
    85  		   "low": 0.96689,
    86  		   "open": 0.97889,
    87  		   "previous_close": 0.98001
    88  		  },
    89  		  "ticker": "C:EURUSD",
    90  		  "type": "fx"
    91  		 },
    92  		 {
    93  		  "branding": {
    94  		   "icon_url": "https://api.polygon.io/icon.png",
    95  		   "logo_url": "https://api.polygon.io/logo.svg"
    96  		  },
    97  		  "market_status": "open",
    98  		  "name": "Bitcoin - United States Dollar",
    99  		  "price": 32154.68,
   100  		  "session": {
   101  		   "change": -201.23,
   102  		   "change_percent": -0.77,
   103  		   "close": 32154.68,
   104  		   "high": 33124.28,
   105  		   "low": 28182.88,
   106  		   "open": 31129.32,
   107  		   "previous_close": 33362.18
   108  		  },
   109  		  "ticker": "X:BTCUSD",
   110  		  "type": "crypto"
   111  		 },
   112  		 {
   113  		  "error": "NOT_FOUND",
   114  		  "message": "Ticker not found.",
   115  		  "ticker": "APx"
   116  		 }
   117  		],
   118  		"status": "OK"
   119  	   }`
   120  	expectedGetSummaryUrl := "https://api.polygon.io/v1/summaries?ticker.any_of=NCLH%2CO%3ANCLH221014C00005000%2CC%3AEURUSD%2CX%3ABTCUSD%2CAPx"
   121  	registerResponder(expectedGetSummaryUrl, expectedSummaryResponse)
   122  	tickerAnyOf := []string{"NCLH", "O:NCLH221014C00005000", "C:EURUSD", "X:BTCUSD", "APx"}
   123  
   124  	res, err := c.GetSummaries(context.Background(), models.GetSummaryParams{}.WithTickerAnyOf(tickerAnyOf...))
   125  	assert.Nil(t, err)
   126  
   127  	var expect models.GetSummaryResponse
   128  	err = json.Unmarshal([]byte(expectedSummaryResponse), &expect)
   129  	assert.Nil(t, err)
   130  	assert.Equal(t, &expect, res)
   131  }