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

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