github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/candle/candle_test.go (about)

     1  package candle_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/candle"
     7  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/common"
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestNewCandleFromRaw(t *testing.T) {
    13  	t.Run("invalid arguments", func(t *testing.T) {
    14  		payload := []interface{}{1.5948918e+12}
    15  
    16  		c, err := candle.FromRaw("tBTCUSD", common.FiveMinutes, payload)
    17  		require.NotNil(t, err)
    18  		require.Nil(t, c)
    19  	})
    20  
    21  	t.Run("valid arguments", func(t *testing.T) {
    22  		payload := []interface{}{
    23  			1.5948918e+12,
    24  			9100,
    25  			9076.9,
    26  			9100.1,
    27  			9054.1,
    28  			149.87216331,
    29  		}
    30  
    31  		w, err := candle.FromRaw("tBTCUSD", common.FiveMinutes, payload)
    32  		require.Nil(t, err)
    33  
    34  		expected := &candle.Candle{
    35  			Symbol:     "tBTCUSD",
    36  			Resolution: "5m",
    37  			MTS:        1594891800000,
    38  			Open:       9100,
    39  			Close:      9076.9,
    40  			High:       9100.1,
    41  			Low:        9054.1,
    42  			Volume:     149.87216331,
    43  		}
    44  
    45  		assert.Equal(t, expected, w)
    46  	})
    47  }
    48  
    49  func TestSnapshotFromRaw(t *testing.T) {
    50  	t.Run("invalid arguments", func(t *testing.T) {
    51  		payload := [][]interface{}{}
    52  
    53  		c, err := candle.SnapshotFromRaw("tBTCUSD", common.FiveMinutes, payload)
    54  		require.NotNil(t, err)
    55  		require.Nil(t, c)
    56  	})
    57  
    58  	t.Run("valid arguments", func(t *testing.T) {
    59  		payload := [][]interface{}{
    60  			{
    61  				1.5948918e+12,
    62  				9100,
    63  				9076.9,
    64  				9100.1,
    65  				9054.1,
    66  				149.87216331,
    67  			},
    68  			{
    69  				1.5948918e+12,
    70  				9200,
    71  				9076.9,
    72  				9100.1,
    73  				9054.1,
    74  				149.87216331,
    75  			},
    76  		}
    77  
    78  		ss, err := candle.SnapshotFromRaw("tBTCUSD", common.FiveMinutes, payload)
    79  		require.Nil(t, err)
    80  
    81  		expected := &candle.Snapshot{
    82  			Snapshot: []*candle.Candle{
    83  				{
    84  					Symbol:     "tBTCUSD",
    85  					Resolution: "5m",
    86  					MTS:        1594891800000,
    87  					Open:       9100,
    88  					Close:      9076.9,
    89  					High:       9100.1,
    90  					Low:        9054.1,
    91  					Volume:     149.87216331,
    92  				},
    93  				{
    94  					Symbol:     "tBTCUSD",
    95  					Resolution: "5m",
    96  					MTS:        1594891800000,
    97  					Open:       9200,
    98  					Close:      9076.9,
    99  					High:       9100.1,
   100  					Low:        9054.1,
   101  					Volume:     149.87216331,
   102  				},
   103  			},
   104  		}
   105  
   106  		require.Nil(t, err)
   107  		assert.Equal(t, expected, ss)
   108  	})
   109  }
   110  
   111  func TestFromWSRaw(t *testing.T) {
   112  	t.Run("invalid arguments", func(t *testing.T) {
   113  		payload := []interface{}{1.5948918e+12}
   114  		c, err := candle.FromWSRaw("trade:1m:tBTCUSD", payload)
   115  		require.NotNil(t, err)
   116  		require.Nil(t, c)
   117  	})
   118  
   119  	t.Run("missing arguments", func(t *testing.T) {
   120  		payload := []interface{}{}
   121  		c, err := candle.FromWSRaw("trade:1m:tBTCUSD", payload)
   122  		require.NotNil(t, err)
   123  		require.Nil(t, c)
   124  	})
   125  
   126  	t.Run("invalid key", func(t *testing.T) {
   127  		payload := []interface{}{}
   128  		c, err := candle.FromWSRaw("tBTCUSD", payload)
   129  		require.NotNil(t, err)
   130  		require.Nil(t, c)
   131  	})
   132  
   133  	t.Run("valid candle arguments", func(t *testing.T) {
   134  		payload := []interface{}{
   135  			1.5948918e+12,
   136  			9100,
   137  			9076.9,
   138  			9100.1,
   139  			9054.1,
   140  			149.87216331,
   141  		}
   142  
   143  		w, err := candle.FromWSRaw("trade:1m:tBTCUSD", payload)
   144  		require.Nil(t, err)
   145  
   146  		expected := &candle.Candle{
   147  			Symbol:     "tBTCUSD",
   148  			Resolution: "1m",
   149  			MTS:        1594891800000,
   150  			Open:       9100,
   151  			Close:      9076.9,
   152  			High:       9100.1,
   153  			Low:        9054.1,
   154  			Volume:     149.87216331,
   155  		}
   156  
   157  		assert.Equal(t, expected, w)
   158  	})
   159  
   160  	t.Run("valid snapshot arguments", func(t *testing.T) {
   161  		payload := []interface{}{
   162  			[]interface{}{
   163  				1.5948918e+12,
   164  				9100,
   165  				9076.9,
   166  				9100.1,
   167  				9054.1,
   168  				149.87216331,
   169  			},
   170  			[]interface{}{
   171  				1.5948918e+12,
   172  				9200,
   173  				9076.9,
   174  				9100.1,
   175  				9054.1,
   176  				149.87216331,
   177  			},
   178  		}
   179  
   180  		ss, err := candle.FromWSRaw("trade:1m:tBTCUSD", payload)
   181  		require.Nil(t, err)
   182  
   183  		expected := &candle.Snapshot{
   184  			Snapshot: []*candle.Candle{
   185  				{
   186  					Symbol:     "tBTCUSD",
   187  					Resolution: "1m",
   188  					MTS:        1594891800000,
   189  					Open:       9100,
   190  					Close:      9076.9,
   191  					High:       9100.1,
   192  					Low:        9054.1,
   193  					Volume:     149.87216331,
   194  				},
   195  				{
   196  					Symbol:     "tBTCUSD",
   197  					Resolution: "1m",
   198  					MTS:        1594891800000,
   199  					Open:       9200,
   200  					Close:      9076.9,
   201  					High:       9100.1,
   202  					Low:        9054.1,
   203  					Volume:     149.87216331,
   204  				},
   205  			},
   206  		}
   207  
   208  		require.Nil(t, err)
   209  		assert.Equal(t, expected, ss)
   210  	})
   211  }