github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/status/status_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 TestFromWSRaw(t *testing.T) {
    11  	testCases := map[string]struct {
    12  		key     string
    13  		data    []interface{}
    14  		err     func(*testing.T, error)
    15  		success func(*testing.T, interface{})
    16  	}{
    17  		"empty slice": {
    18  			key:  "",
    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 key": {
    28  			key:  "foo",
    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  		"deriv snapshot invalid pld": {
    38  			key:  "deriv:tBTCF0:USTF0",
    39  			data: []interface{}{[]interface{}{1}},
    40  			err: func(t *testing.T, got error) {
    41  				assert.Error(t, got)
    42  			},
    43  			success: func(t *testing.T, got interface{}) {
    44  				assert.Nil(t, got)
    45  			},
    46  		},
    47  		"deriv raw invalid pld": {
    48  			key:  "deriv:tBTCF0:USTF0",
    49  			data: []interface{}{1},
    50  			err: func(t *testing.T, got error) {
    51  				assert.Error(t, got)
    52  			},
    53  			success: func(t *testing.T, got interface{}) {
    54  				assert.Nil(t, got)
    55  			},
    56  		},
    57  		"liq snapshot invalid pld": {
    58  			key:  "liq:global",
    59  			data: []interface{}{[]interface{}{1}},
    60  			err: func(t *testing.T, got error) {
    61  				assert.Error(t, got)
    62  			},
    63  			success: func(t *testing.T, got interface{}) {
    64  				assert.Nil(t, got)
    65  			},
    66  		},
    67  		"liq raw invalid pld": {
    68  			key:  "liq:global",
    69  			data: []interface{}{1},
    70  			err: func(t *testing.T, got error) {
    71  				assert.Error(t, got)
    72  			},
    73  			success: func(t *testing.T, got interface{}) {
    74  				assert.Nil(t, got)
    75  			},
    76  		},
    77  		"deriv snapshot valid pld": {
    78  			key: "deriv:tBTCF0:USTF0",
    79  			data: []interface{}{
    80  				[]interface{}{
    81  					1596124822000, nil, 0.896, 0.771995, nil, 1396531.67460709,
    82  					nil, 1596153600000, 0.0001056, 6, nil, -0.01381344, nil, nil,
    83  					0.7664, nil, nil, 246502.09001551, nil, nil, nil, nil, 0.3,
    84  				},
    85  			},
    86  			err: func(t *testing.T, got error) {
    87  				assert.Nil(t, got)
    88  			},
    89  			success: func(t *testing.T, got interface{}) {
    90  				assert.Equal(t, got, &status.DerivativesSnapshot{
    91  					Snapshot: []*status.Derivative{
    92  						{
    93  							Symbol:               "tBTCF0:USTF0",
    94  							MTS:                  1596124822000,
    95  							Price:                0.896,
    96  							SpotPrice:            0.771995,
    97  							InsuranceFundBalance: 1396531.67460709,
    98  							FundingEventMTS:      1596153600000,
    99  							FundingAccrued:       0.0001056,
   100  							FundingStep:          6,
   101  							CurrentFunding:       -0.01381344,
   102  							MarkPrice:            0.7664,
   103  							OpenInterest:         246502.09001551,
   104  							ClampMIN:             0,
   105  							ClampMAX:             0.3,
   106  						},
   107  					},
   108  				})
   109  			},
   110  		},
   111  		"deriv raw valid pld": {
   112  			key: "deriv:tBTCF0:USTF0",
   113  			data: []interface{}{
   114  				1596124822000, nil, 0.896, 0.771995, nil, 1396531.67460709,
   115  				nil, 1596153600000, 0.0001056, 6, nil, -0.01381344, nil, nil,
   116  				0.7664, nil, nil, 246502.09001551, nil, nil, nil, nil, 0.3,
   117  			},
   118  			err: func(t *testing.T, got error) {
   119  				assert.Nil(t, got)
   120  			},
   121  			success: func(t *testing.T, got interface{}) {
   122  				assert.Equal(t, got, &status.Derivative{
   123  					Symbol:               "tBTCF0:USTF0",
   124  					MTS:                  1596124822000,
   125  					Price:                0.896,
   126  					SpotPrice:            0.771995,
   127  					InsuranceFundBalance: 1396531.67460709,
   128  					FundingEventMTS:      1596153600000,
   129  					FundingAccrued:       0.0001056,
   130  					FundingStep:          6,
   131  					CurrentFunding:       -0.01381344,
   132  					MarkPrice:            0.7664,
   133  					OpenInterest:         246502.09001551,
   134  					ClampMIN:             0,
   135  					ClampMAX:             0.3,
   136  				})
   137  			},
   138  		},
   139  		"liquidation snapshot valid pld": {
   140  			key: "liq:global",
   141  			data: []interface{}{
   142  				[]interface{}{
   143  					"pos", 145400868, 1609144352338, nil, "tETHF0:USTF0",
   144  					-1.67288094, 730.96, nil, 1, 1, nil, 736.13,
   145  				},
   146  			},
   147  			err: func(t *testing.T, got error) {
   148  				assert.Nil(t, got)
   149  			},
   150  			success: func(t *testing.T, got interface{}) {
   151  				assert.Equal(t, got, &status.LiquidationsSnapshot{
   152  					Snapshot: []*status.Liquidation{
   153  						{
   154  							Symbol:        "tETHF0:USTF0",
   155  							PositionID:    145400868,
   156  							MTS:           1609144352338,
   157  							Amount:        -1.67288094,
   158  							BasePrice:     730.96,
   159  							IsMatch:       1,
   160  							IsMarketSold:  1,
   161  							PriceAcquired: 736.13,
   162  						},
   163  					},
   164  				})
   165  			},
   166  		},
   167  		"liquidation raw valid pld": {
   168  			key: "liq:global",
   169  			data: []interface{}{
   170  				"pos", 145400868, 1609144352338, nil, "tETHF0:USTF0",
   171  				-1.67288094, 730.96, nil, 1, 1, nil, 736.13,
   172  			},
   173  			err: func(t *testing.T, got error) {
   174  				assert.Nil(t, got)
   175  			},
   176  			success: func(t *testing.T, got interface{}) {
   177  				assert.Equal(t, got, &status.Liquidation{
   178  					Symbol:        "tETHF0:USTF0",
   179  					PositionID:    145400868,
   180  					MTS:           1609144352338,
   181  					Amount:        -1.67288094,
   182  					BasePrice:     730.96,
   183  					IsMatch:       1,
   184  					IsMarketSold:  1,
   185  					PriceAcquired: 736.13,
   186  				})
   187  			},
   188  		},
   189  	}
   190  
   191  	for k, v := range testCases {
   192  		t.Run(k, func(t *testing.T) {
   193  			got, err := status.FromWSRaw(v.key, v.data)
   194  			v.err(t, err)
   195  			v.success(t, got)
   196  		})
   197  	}
   198  }