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

     1  package position_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/position"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestFromRaw(t *testing.T) {
    12  	cases := map[string]struct {
    13  		pld      []interface{}
    14  		expected *position.Position
    15  		err      func(*testing.T, error)
    16  	}{
    17  		"invalid pld": {
    18  			pld:      []interface{}{},
    19  			expected: nil,
    20  			err: func(t *testing.T, err error) {
    21  				assert.NotNil(t, err)
    22  			},
    23  		},
    24  		"rest retrieve positions": {
    25  			pld: []interface{}{
    26  				"tBTCUSD", "ACTIVE", 0.0195, 8565.0267019, 0, 0, -0.33455568705000516, -0.0003117550117425625,
    27  				7045.876419249083, 3.0673001895895604, nil, 142355652, 1574002216000, 1574002216000, nil, 0, nil, 0, 0,
    28  				map[string]interface{}{
    29  					"reason":        "TRADE",
    30  					"order_id":      34271018124,
    31  					"liq_stage":     nil,
    32  					"trade_price":   "8565.0267019",
    33  					"trade_amount":  "0.0195",
    34  					"order_id_oppo": 34277498022,
    35  				},
    36  			},
    37  			expected: &position.Position{
    38  				Id:                   142355652,
    39  				Symbol:               "tBTCUSD",
    40  				Status:               "ACTIVE",
    41  				Amount:               0.0195,
    42  				BasePrice:            8565.0267019,
    43  				MarginFunding:        0,
    44  				MarginFundingType:    0,
    45  				ProfitLoss:           -0.33455568705000516,
    46  				ProfitLossPercentage: -0.0003117550117425625,
    47  				LiquidationPrice:     7045.876419249083,
    48  				Leverage:             3.0673001895895604,
    49  				Flag:                 nil,
    50  				MtsCreate:            1574002216000,
    51  				MtsUpdate:            1574002216000,
    52  				Type:                 "",
    53  				Collateral:           0,
    54  				CollateralMin:        0,
    55  				Meta: map[string]interface{}{
    56  					"liq_stage":     nil,
    57  					"order_id":      34271018124,
    58  					"order_id_oppo": 34277498022,
    59  					"reason":        "TRADE",
    60  					"trade_amount":  "0.0195",
    61  					"trade_price":   "8565.0267019",
    62  				},
    63  			},
    64  			err: func(t *testing.T, err error) {
    65  				assert.Nil(t, err)
    66  			},
    67  		},
    68  		"rest claim position": {
    69  			pld: []interface{}{
    70  				"tBTCUSD", "ACTIVE", -0.001, 10119, 0, 0, nil, nil, nil, nil, nil, 142031891,
    71  				1568650294000, 1568650294000, nil, 0, nil, 0, nil,
    72  				map[string]interface{}{
    73  					"reason":        "TRADE",
    74  					"order_id":      31089337681,
    75  					"liq_stage":     nil,
    76  					"trade_price":   "10119.0",
    77  					"trade_amount":  "-0.001",
    78  					"user_id_oppo":  183923,
    79  					"order_id_oppo": 31089692490,
    80  				},
    81  			},
    82  			expected: &position.Position{
    83  				Id:                   142031891,
    84  				Symbol:               "tBTCUSD",
    85  				Status:               "ACTIVE",
    86  				Amount:               -0.001,
    87  				BasePrice:            10119,
    88  				MarginFunding:        0,
    89  				MarginFundingType:    0,
    90  				ProfitLoss:           0,
    91  				ProfitLossPercentage: 0,
    92  				LiquidationPrice:     0,
    93  				Leverage:             0,
    94  				Flag:                 nil,
    95  				MtsCreate:            1568650294000,
    96  				MtsUpdate:            1568650294000,
    97  				Type:                 "",
    98  				Collateral:           0,
    99  				CollateralMin:        0,
   100  				Meta: map[string]interface{}{
   101  					"liq_stage":     nil,
   102  					"order_id":      31089337681,
   103  					"order_id_oppo": 31089692490,
   104  					"reason":        "TRADE",
   105  					"trade_amount":  "-0.001",
   106  					"trade_price":   "10119.0",
   107  					"user_id_oppo":  183923,
   108  				},
   109  			},
   110  			err: func(t *testing.T, err error) {
   111  				assert.Nil(t, err)
   112  			},
   113  		},
   114  		"rest position history item": {
   115  			pld: []interface{}{
   116  				"tBTCUSD", "CLOSED", 0, 8639.50216298, 0, 0, nil, nil, nil, nil, nil,
   117  				142355652, 1574002216000, 1574003099000, nil, nil, nil, nil, nil, nil,
   118  			},
   119  			expected: &position.Position{
   120  				Id:                   142355652,
   121  				Symbol:               "tBTCUSD",
   122  				Status:               "CLOSED",
   123  				Amount:               0,
   124  				BasePrice:            8639.50216298,
   125  				MarginFunding:        0,
   126  				MarginFundingType:    0,
   127  				ProfitLoss:           0,
   128  				ProfitLossPercentage: 0,
   129  				LiquidationPrice:     0,
   130  				Leverage:             0,
   131  				Flag:                 nil,
   132  				MtsCreate:            1574002216000,
   133  				MtsUpdate:            1574003099000,
   134  				Type:                 "",
   135  				Collateral:           0,
   136  				CollateralMin:        0,
   137  				Meta:                 nil,
   138  			},
   139  			err: func(t *testing.T, err error) {
   140  				assert.Nil(t, err)
   141  			},
   142  		},
   143  		"rest position audit": {
   144  			pld: []interface{}{
   145  				"tETHUSD", "ACTIVE", 0.9682, 182.76, 0, 0, nil, nil, nil, nil, nil,
   146  				142358085, 1574088504000, 1574088504000, nil, nil, nil, 0, nil,
   147  				map[string]interface{}{
   148  					"reason":        "TRADE",
   149  					"order_id":      34319724780,
   150  					"liq_stage":     nil,
   151  					"trade_price":   "182.76",
   152  					"trade_amount":  "0.9682",
   153  					"user_id_oppo":  411107,
   154  					"order_id_oppo": 34326729830,
   155  				},
   156  			},
   157  			expected: &position.Position{
   158  				Id:                   142358085,
   159  				Symbol:               "tETHUSD",
   160  				Status:               "ACTIVE",
   161  				Amount:               0.9682,
   162  				BasePrice:            182.76,
   163  				MarginFunding:        0,
   164  				MarginFundingType:    0,
   165  				ProfitLoss:           0,
   166  				ProfitLossPercentage: 0,
   167  				LiquidationPrice:     0,
   168  				Leverage:             0,
   169  				Flag:                 nil,
   170  				MtsCreate:            1574088504000,
   171  				MtsUpdate:            1574088504000,
   172  				Type:                 "",
   173  				Collateral:           0,
   174  				CollateralMin:        0,
   175  				Meta: map[string]interface{}{
   176  					"liq_stage":     nil,
   177  					"order_id":      34319724780,
   178  					"order_id_oppo": 34326729830,
   179  					"reason":        "TRADE",
   180  					"trade_amount":  "0.9682",
   181  					"trade_price":   "182.76",
   182  					"user_id_oppo":  411107,
   183  				},
   184  			},
   185  			err: func(t *testing.T, err error) {
   186  				assert.Nil(t, err)
   187  			},
   188  		},
   189  		"ws pn pu pc": {
   190  			pld: []interface{}{
   191  				"tETHUST", "ACTIVE", 0.2, 153.71, 0, 0, -0.07944800000000068, -0.05855181835925015,
   192  				67.52755254906451, 1.409288545397275, nil, 142420429, nil, nil, nil, 0, nil, 0, 0,
   193  				map[string]interface{}{
   194  					"reason":        "TRADE",
   195  					"order_id":      34934099168,
   196  					"order_id_oppo": 34934090814,
   197  					"liq_stage":     nil,
   198  					"trade_price":   "153.71",
   199  					"trade_amount":  "0.2",
   200  				},
   201  			},
   202  			expected: &position.Position{
   203  				Id:                   142420429,
   204  				Symbol:               "tETHUST",
   205  				Status:               "ACTIVE",
   206  				Amount:               0.2,
   207  				BasePrice:            153.71,
   208  				MarginFunding:        0,
   209  				MarginFundingType:    0,
   210  				ProfitLoss:           -0.07944800000000068,
   211  				ProfitLossPercentage: -0.05855181835925015,
   212  				LiquidationPrice:     67.52755254906451,
   213  				Leverage:             1.409288545397275,
   214  				Flag:                 nil,
   215  				MtsCreate:            0,
   216  				MtsUpdate:            0,
   217  				Type:                 "",
   218  				Collateral:           0,
   219  				CollateralMin:        0,
   220  				Meta: map[string]interface{}{
   221  					"liq_stage":     nil,
   222  					"order_id":      34934099168,
   223  					"order_id_oppo": 34934090814,
   224  					"reason":        "TRADE",
   225  					"trade_amount":  "0.2",
   226  					"trade_price":   "153.71",
   227  				},
   228  			},
   229  			err: func(t *testing.T, err error) {
   230  				assert.Nil(t, err)
   231  			},
   232  		},
   233  	}
   234  
   235  	for k, v := range cases {
   236  		t.Run(k, func(t *testing.T) {
   237  			got, err := position.FromRaw(v.pld)
   238  			v.err(t, err)
   239  			assert.Equal(t, v.expected, got)
   240  		})
   241  	}
   242  }
   243  
   244  func TestSnapshotFromRaw(t *testing.T) {
   245  	cases := map[string]struct {
   246  		pld      []interface{}
   247  		expected *position.Snapshot
   248  		err      func(*testing.T, error)
   249  	}{
   250  		"invalid pld": {
   251  			pld:      []interface{}{},
   252  			expected: nil,
   253  			err: func(t *testing.T, err error) {
   254  				assert.NotNil(t, err)
   255  			},
   256  		},
   257  		"rest positions snapshot": {
   258  			pld: []interface{}{
   259  				[]interface{}{
   260  					"tETHUSD", "ACTIVE", -0.2, 167.01, 0, 0, nil, nil, nil, nil, nil,
   261  					142661142, 1579552390000, 1579552390000, nil, nil, nil, nil, nil, nil,
   262  				},
   263  				[]interface{}{
   264  					"tETHUSD", "ACTIVE", -0.2, 167.01, 0, 0, nil, nil, nil, nil, nil,
   265  					142661143, 1579552390000, 1579552390000, nil, nil, nil, nil, nil, nil,
   266  				},
   267  			},
   268  			expected: &position.Snapshot{
   269  				Snapshot: []*position.Position{
   270  					{
   271  						Id:        142661142,
   272  						Symbol:    "tETHUSD",
   273  						Status:    "ACTIVE",
   274  						Amount:    -0.2,
   275  						BasePrice: 167.01,
   276  						MtsCreate: 1579552390000,
   277  						MtsUpdate: 1579552390000,
   278  					},
   279  					{
   280  						Id:        142661143,
   281  						Symbol:    "tETHUSD",
   282  						Status:    "ACTIVE",
   283  						Amount:    -0.2,
   284  						BasePrice: 167.01,
   285  						MtsCreate: 1579552390000,
   286  						MtsUpdate: 1579552390000,
   287  					},
   288  				},
   289  			},
   290  			err: func(t *testing.T, err error) {
   291  				assert.Nil(t, err)
   292  			},
   293  		},
   294  		"ws positions snapshot": {
   295  			pld: []interface{}{
   296  				[]interface{}{
   297  					"tETHUST", "ACTIVE", 0.2, 153.71, 0, 0, nil, nil, nil,
   298  					nil, nil, 142420429, nil, nil, nil, 0, nil, 0, nil,
   299  					map[string]interface{}{
   300  						"reason":        "TRADE",
   301  						"order_id":      34934099168,
   302  						"order_id_oppo": 34934090814,
   303  						"liq_stage":     nil,
   304  						"trade_price":   "153.71",
   305  						"trade_amount":  "0.2",
   306  					},
   307  				},
   308  			},
   309  			expected: &position.Snapshot{
   310  				Snapshot: []*position.Position{
   311  					{
   312  						Id:        142420429,
   313  						Symbol:    "tETHUST",
   314  						Status:    "ACTIVE",
   315  						Amount:    0.2,
   316  						BasePrice: 153.71,
   317  						Meta: map[string]interface{}{
   318  							"reason":        "TRADE",
   319  							"order_id":      34934099168,
   320  							"order_id_oppo": 34934090814,
   321  							"liq_stage":     nil,
   322  							"trade_price":   "153.71",
   323  							"trade_amount":  "0.2",
   324  						},
   325  					},
   326  				},
   327  			},
   328  			err: func(t *testing.T, err error) {
   329  				assert.Nil(t, err)
   330  			},
   331  		},
   332  	}
   333  
   334  	for k, v := range cases {
   335  		t.Run(k, func(t *testing.T) {
   336  			got, err := position.SnapshotFromRaw(v.pld)
   337  			v.err(t, err)
   338  			assert.Equal(t, v.expected, got)
   339  		})
   340  	}
   341  }
   342  
   343  func TestNewFromRaw(t *testing.T) {
   344  	pld := []interface{}{
   345  		"tETHUST", "ACTIVE", 0.2, 153.71, 0, 0, -0.07944800000000068, -0.05855181835925015,
   346  		67.52755254906451, 1.409288545397275, nil, 142420429, nil, nil, nil, 0, nil, 0, 0,
   347  		map[string]interface{}{
   348  			"reason":        "TRADE",
   349  			"order_id":      34934099168,
   350  			"order_id_oppo": 34934090814,
   351  			"liq_stage":     nil,
   352  			"trade_price":   "153.71",
   353  			"trade_amount":  "0.2",
   354  		},
   355  	}
   356  
   357  	expected := "position.New"
   358  	p, err := position.NewFromRaw(pld)
   359  	assert.Nil(t, err)
   360  
   361  	got := reflect.TypeOf(p).String()
   362  	assert.Equal(t, expected, got)
   363  	assert.Equal(t, "pn", p.Type)
   364  }
   365  
   366  func TestUpdateFromRaw(t *testing.T) {
   367  	pld := []interface{}{
   368  		"tETHUST", "ACTIVE", 0.2, 153.71, 0, 0, -0.07944800000000068, -0.05855181835925015,
   369  		67.52755254906451, 1.409288545397275, nil, 142420429, nil, nil, nil, 0, nil, 0, 0,
   370  		map[string]interface{}{
   371  			"reason":        "TRADE",
   372  			"order_id":      34934099168,
   373  			"order_id_oppo": 34934090814,
   374  			"liq_stage":     nil,
   375  			"trade_price":   "153.71",
   376  			"trade_amount":  "0.2",
   377  		},
   378  	}
   379  
   380  	expected := "position.Update"
   381  	p, err := position.UpdateFromRaw(pld)
   382  	assert.Nil(t, err)
   383  
   384  	got := reflect.TypeOf(p).String()
   385  	assert.Equal(t, expected, got)
   386  	assert.Equal(t, "pu", p.Type)
   387  }
   388  
   389  func TestCancelFromRaw(t *testing.T) {
   390  	pld := []interface{}{
   391  		"tETHUST", "ACTIVE", 0.2, 153.71, 0, 0, -0.07944800000000068, -0.05855181835925015,
   392  		67.52755254906451, 1.409288545397275, nil, 142420429, nil, nil, nil, 0, nil, 0, 0,
   393  		map[string]interface{}{
   394  			"reason":        "TRADE",
   395  			"order_id":      34934099168,
   396  			"order_id_oppo": 34934090814,
   397  			"liq_stage":     nil,
   398  			"trade_price":   "153.71",
   399  			"trade_amount":  "0.2",
   400  		},
   401  	}
   402  
   403  	expected := "position.Cancel"
   404  	p, err := position.CancelFromRaw(pld)
   405  	assert.Nil(t, err)
   406  
   407  	got := reflect.TypeOf(p).String()
   408  	assert.Equal(t, expected, got)
   409  	assert.Equal(t, "pc", p.Type)
   410  }