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

     1  package status_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/status"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestDerivFromRaw(t *testing.T) {
    11  	testCases := map[string]struct {
    12  		symbol  string
    13  		data    []interface{}
    14  		err     func(*testing.T, error)
    15  		success func(*testing.T, interface{})
    16  	}{
    17  		"empty slice": {
    18  			symbol: "tBTCF0:USTF0",
    19  			data:   []interface{}{},
    20  			err: func(t *testing.T, got error) {
    21  				assert.Error(t, got)
    22  			},
    23  			success: func(t *testing.T, got interface{}) {
    24  				assert.Nil(t, got)
    25  			},
    26  		},
    27  		"invalid pld": {
    28  			symbol: "tBTCF0:USTF0",
    29  			data:   []interface{}{1},
    30  			err: func(t *testing.T, got error) {
    31  				assert.Error(t, got)
    32  			},
    33  			success: func(t *testing.T, got interface{}) {
    34  				assert.Nil(t, got)
    35  			},
    36  		},
    37  		"valid pld": {
    38  			symbol: "tBTCF0:USTF0",
    39  			data: []interface{}{
    40  				1596124822000, nil, 0.896, 0.771995, nil, 1396531.67460709,
    41  				nil, 1596153600000, 0.0001056, 6, nil, -0.01381344, nil, nil,
    42  				0.7664, nil, nil, 246502.09001551, nil, nil, nil, nil, 0.3,
    43  			},
    44  			err: func(t *testing.T, got error) {
    45  				assert.Nil(t, got)
    46  			},
    47  			success: func(t *testing.T, got interface{}) {
    48  				assert.Equal(t, got, &status.Derivative{
    49  					Symbol:               "tBTCF0:USTF0",
    50  					MTS:                  1596124822000,
    51  					Price:                0.896,
    52  					SpotPrice:            0.771995,
    53  					InsuranceFundBalance: 1396531.67460709,
    54  					FundingEventMTS:      1596153600000,
    55  					FundingAccrued:       0.0001056,
    56  					FundingStep:          6,
    57  					CurrentFunding:       -0.01381344,
    58  					MarkPrice:            0.7664,
    59  					OpenInterest:         246502.09001551,
    60  					ClampMIN:             0,
    61  					ClampMAX:             0.3,
    62  				})
    63  			},
    64  		},
    65  	}
    66  
    67  	for k, v := range testCases {
    68  		t.Run(k, func(t *testing.T) {
    69  			got, err := status.DerivFromRaw(v.symbol, v.data)
    70  			v.err(t, err)
    71  			v.success(t, got)
    72  		})
    73  	}
    74  }
    75  
    76  func TestDerivFromRestRaw(t *testing.T) {
    77  	testCases := map[string]struct {
    78  		data    []interface{}
    79  		err     func(*testing.T, error)
    80  		success func(*testing.T, interface{})
    81  	}{
    82  		"empty slice": {
    83  			data: []interface{}{},
    84  			err: func(t *testing.T, got error) {
    85  				assert.Error(t, got)
    86  			},
    87  			success: func(t *testing.T, got interface{}) {
    88  				assert.Nil(t, got)
    89  			},
    90  		},
    91  		"invalid pld": {
    92  			data: []interface{}{1},
    93  			err: func(t *testing.T, got error) {
    94  				assert.Error(t, got)
    95  			},
    96  			success: func(t *testing.T, got interface{}) {
    97  				assert.Nil(t, got)
    98  			},
    99  		},
   100  		"valid pld": {
   101  			data: []interface{}{
   102  				"tBTCF0:USTF0", 1596124822000, nil, 0.896, 0.771995, nil,
   103  				1396531.67460709, nil, 1596153600000, 0.0001056, 6, nil,
   104  				-0.01381344, nil, nil, 0.7664, nil, nil, 246502.09001551,
   105  				nil, nil, nil, nil, 0.3,
   106  			},
   107  			err: func(t *testing.T, got error) {
   108  				assert.Nil(t, got)
   109  			},
   110  			success: func(t *testing.T, got interface{}) {
   111  				assert.Equal(t, got, &status.Derivative{
   112  					Symbol:               "tBTCF0:USTF0",
   113  					MTS:                  1596124822000,
   114  					Price:                0.896,
   115  					SpotPrice:            0.771995,
   116  					InsuranceFundBalance: 1396531.67460709,
   117  					FundingEventMTS:      1596153600000,
   118  					FundingAccrued:       0.0001056,
   119  					FundingStep:          6,
   120  					CurrentFunding:       -0.01381344,
   121  					MarkPrice:            0.7664,
   122  					OpenInterest:         246502.09001551,
   123  					ClampMIN:             0,
   124  					ClampMAX:             0.3,
   125  				})
   126  			},
   127  		},
   128  	}
   129  
   130  	for k, v := range testCases {
   131  		t.Run(k, func(t *testing.T) {
   132  			got, err := status.DerivFromRestRaw(v.data)
   133  			v.err(t, err)
   134  			v.success(t, got)
   135  		})
   136  	}
   137  }
   138  
   139  func TestDerivSnapshotFromRaw(t *testing.T) {
   140  	testCases := map[string]struct {
   141  		symbol  string
   142  		data    [][]interface{}
   143  		err     func(*testing.T, error)
   144  		success func(*testing.T, interface{})
   145  	}{
   146  		"empty slice": {
   147  			symbol: "tBTCF0:USTF0",
   148  			data:   [][]interface{}{},
   149  			err: func(t *testing.T, got error) {
   150  				assert.Error(t, got)
   151  			},
   152  			success: func(t *testing.T, got interface{}) {
   153  				assert.Nil(t, got)
   154  			},
   155  		},
   156  		"invalid pld": {
   157  			symbol: "tBTCF0:USTF0",
   158  			data:   [][]interface{}{{1}},
   159  			err: func(t *testing.T, got error) {
   160  				assert.Error(t, got)
   161  			},
   162  			success: func(t *testing.T, got interface{}) {
   163  				assert.Nil(t, got)
   164  			},
   165  		},
   166  		"valid pld": {
   167  			symbol: "tBTCF0:USTF0",
   168  			data: [][]interface{}{
   169  				{
   170  					1596124822000, nil, 0.896, 0.771995, nil, 1396531.67460709,
   171  					nil, 1596153600000, 0.0001056, 6, nil, -0.01381344, nil, nil,
   172  					0.7664, nil, nil, 246502.09001551, nil, nil, nil, nil, 0.3,
   173  				},
   174  				{
   175  					1596124822001, nil, 0.896, 0.771995, nil, 1396531.67460709,
   176  					nil, 1596153600000, 0.0001056, 6, nil, -0.01381344, nil, nil,
   177  					0.7664, nil, nil, 246502.09001551, nil, nil, nil, nil, 0.3, nil, 123,
   178  				},
   179  			},
   180  			err: func(t *testing.T, got error) {
   181  				assert.Nil(t, got)
   182  			},
   183  			success: func(t *testing.T, got interface{}) {
   184  				assert.Equal(t, got, &status.DerivativesSnapshot{
   185  					Snapshot: []*status.Derivative{
   186  						{
   187  							Symbol:               "tBTCF0:USTF0",
   188  							MTS:                  1596124822000,
   189  							Price:                0.896,
   190  							SpotPrice:            0.771995,
   191  							InsuranceFundBalance: 1396531.67460709,
   192  							FundingEventMTS:      1596153600000,
   193  							FundingAccrued:       0.0001056,
   194  							FundingStep:          6,
   195  							CurrentFunding:       -0.01381344,
   196  							MarkPrice:            0.7664,
   197  							OpenInterest:         246502.09001551,
   198  							ClampMIN:             0,
   199  							ClampMAX:             0.3,
   200  						},
   201  						{
   202  							Symbol:               "tBTCF0:USTF0",
   203  							MTS:                  1596124822001,
   204  							Price:                0.896,
   205  							SpotPrice:            0.771995,
   206  							InsuranceFundBalance: 1396531.67460709,
   207  							FundingEventMTS:      1596153600000,
   208  							FundingAccrued:       0.0001056,
   209  							FundingStep:          6,
   210  							CurrentFunding:       -0.01381344,
   211  							MarkPrice:            0.7664,
   212  							OpenInterest:         246502.09001551,
   213  							ClampMIN:             0,
   214  							ClampMAX:             0.3,
   215  						},
   216  					},
   217  				})
   218  			},
   219  		},
   220  	}
   221  
   222  	for k, v := range testCases {
   223  		t.Run(k, func(t *testing.T) {
   224  			got, err := status.DerivSnapshotFromRaw(v.symbol, v.data)
   225  			v.err(t, err)
   226  			v.success(t, got)
   227  		})
   228  	}
   229  }