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

     1  package order_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/order"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestFromRaw(t *testing.T) {
    12  	cases := map[string]struct {
    13  		pld      []interface{}
    14  		expected *order.Order
    15  		err      func(*testing.T, error)
    16  	}{
    17  		"invalid pld": {
    18  			pld:      []interface{}{"exchange"},
    19  			expected: nil,
    20  			err: func(t *testing.T, err error) {
    21  				assert.NotNil(t, err)
    22  			},
    23  		},
    24  		"rest retire orders": {
    25  			pld: []interface{}{
    26  				33950998275, nil, 1573476747887, "tETHUSD", 1573476748000, 1573476748000,
    27  				-0.5, -0.5, "LIMIT", nil, nil, nil, 0, "ACTIVE", nil, nil, 220, 0, 0, 0, nil, nil,
    28  				nil, 0, 0, nil, nil, nil, "BFX", nil, nil, nil,
    29  			},
    30  			expected: &order.Order{
    31  				ID:         33950998275,
    32  				CID:        1573476747887,
    33  				Symbol:     "tETHUSD",
    34  				MTSCreated: 1573476748000,
    35  				MTSUpdated: 1573476748000,
    36  				Amount:     -0.5,
    37  				AmountOrig: -0.5,
    38  				Type:       "LIMIT",
    39  				Status:     "ACTIVE",
    40  				Price:      220,
    41  				Notify:     false,
    42  				Hidden:     false,
    43  				Routing:    "BFX",
    44  			},
    45  			err: func(t *testing.T, err error) {
    46  				assert.Nil(t, err)
    47  			},
    48  		},
    49  		"rest submit order": {
    50  			pld: []interface{}{
    51  				30630788061, nil, 1567590617439, "tBTCUSD", 1567590617439, 1567590617439,
    52  				0.001, 0.001, "LIMIT", nil, nil, nil, 0, "ACTIVE", nil, nil, 15, 0, 0, 0, nil, nil,
    53  				nil, 0, nil, nil, nil, nil, "API>BFX", nil, nil, nil,
    54  			},
    55  			expected: &order.Order{
    56  				ID:         30630788061,
    57  				CID:        1567590617439,
    58  				Symbol:     "tBTCUSD",
    59  				MTSCreated: 1567590617439,
    60  				MTSUpdated: 1567590617439,
    61  				Amount:     0.001,
    62  				AmountOrig: 0.001,
    63  				Type:       "LIMIT",
    64  				Status:     "ACTIVE",
    65  				Price:      15,
    66  				Notify:     false,
    67  				Hidden:     false,
    68  				Routing:    "API>BFX",
    69  			},
    70  			err: func(t *testing.T, err error) {
    71  				assert.Nil(t, err)
    72  			},
    73  		},
    74  		"rest update order": {
    75  			pld: []interface{}{
    76  				30854813589, nil, 1568109670135, "tBTCUSD", 1568109673000, 1568109866000,
    77  				0.002, 0.002, "LIMIT", "LIMIT", nil, nil, 0, "ACTIVE", nil, nil, 20, 0, 0,
    78  				0, nil, nil, nil, 0, 0, nil, nil, nil, "API>BFX", nil, nil, nil,
    79  			},
    80  			expected: &order.Order{
    81  				ID:         30854813589,
    82  				CID:        1568109670135,
    83  				Symbol:     "tBTCUSD",
    84  				MTSCreated: 1568109673000,
    85  				MTSUpdated: 1568109866000,
    86  				Amount:     0.002,
    87  				AmountOrig: 0.002,
    88  				Type:       "LIMIT",
    89  				TypePrev:   "LIMIT",
    90  				Status:     "ACTIVE",
    91  				Price:      20,
    92  				Notify:     false,
    93  				Hidden:     false,
    94  				Routing:    "API>BFX",
    95  			},
    96  			err: func(t *testing.T, err error) {
    97  				assert.Nil(t, err)
    98  			},
    99  		},
   100  		"rest cancel order": {
   101  			pld: []interface{}{
   102  				30937950333, nil, 1568298279766, "tBTCUSD", 1568298281000,
   103  				1568298281000, 0.001, 0.001, "LIMIT", nil, nil, nil, 0,
   104  				"ACTIVE", nil, nil, 15, 0, 0, 0, nil, nil, nil, 0, 0, nil,
   105  				nil, nil, "API>BFX", nil, nil, nil,
   106  			},
   107  			expected: &order.Order{
   108  				ID:         30937950333,
   109  				CID:        1568298279766,
   110  				Symbol:     "tBTCUSD",
   111  				MTSCreated: 1568298281000,
   112  				MTSUpdated: 1568298281000,
   113  				Amount:     0.001,
   114  				AmountOrig: 0.001,
   115  				Type:       "LIMIT",
   116  				Status:     "ACTIVE",
   117  				Price:      15,
   118  				Notify:     false,
   119  				Hidden:     false,
   120  				Routing:    "API>BFX",
   121  			},
   122  			err: func(t *testing.T, err error) {
   123  				assert.Nil(t, err)
   124  			},
   125  		},
   126  		"rest order multi op": {
   127  			pld: []interface{}{
   128  				31492970019, nil, 1569347312970, "tBTCUSD", 1569347312971, 1569347312971, 0.001,
   129  				0.001, "LIMIT", nil, nil, nil, 4096, "ACTIVE", nil, nil, 15, 0, 0, 0, nil, nil,
   130  				nil, 0, nil, nil, nil, nil, "API>BFX", nil, nil, map[string]interface{}{"$F7": 1},
   131  			},
   132  			expected: &order.Order{
   133  				ID:         31492970019,
   134  				CID:        1569347312970,
   135  				Symbol:     "tBTCUSD",
   136  				MTSCreated: 1569347312971,
   137  				MTSUpdated: 1569347312971,
   138  				Amount:     0.001,
   139  				AmountOrig: 0.001,
   140  				Type:       "LIMIT",
   141  				Flags:      4096,
   142  				Status:     "ACTIVE",
   143  				Price:      15,
   144  				Notify:     false,
   145  				Hidden:     false,
   146  				Routing:    "API>BFX",
   147  				Meta:       map[string]interface{}{"$F7": 1},
   148  			},
   149  			err: func(t *testing.T, err error) {
   150  				assert.Nil(t, err)
   151  			},
   152  		},
   153  		"rest cancel order multi": {
   154  			pld: []interface{}{
   155  				31123704044, nil, 1568711144715, "tBTCUSD", 1568711147000, 1568711147000,
   156  				0.001, 0.001, "LIMIT", nil, nil, nil, 0, "ACTIVE", nil, nil, 15, 0, 0, 0, nil, nil,
   157  				nil, 0, 0, nil, nil, nil, "API>BFX", nil, nil, nil,
   158  			},
   159  			expected: &order.Order{
   160  				ID:         31123704044,
   161  				CID:        1568711144715,
   162  				Symbol:     "tBTCUSD",
   163  				MTSCreated: 1568711147000,
   164  				MTSUpdated: 1568711147000,
   165  				Amount:     0.001,
   166  				AmountOrig: 0.001,
   167  				Type:       "LIMIT",
   168  				Status:     "ACTIVE",
   169  				Price:      15,
   170  				Notify:     false,
   171  				Hidden:     false,
   172  				Routing:    "API>BFX",
   173  			},
   174  			err: func(t *testing.T, err error) {
   175  				assert.Nil(t, err)
   176  			},
   177  		},
   178  		"rest orders history item": {
   179  			pld: []interface{}{
   180  				33961681942, "1227", 1337, "tBTCUSD", 1573482478000, 1573485373000,
   181  				0.001, 0.001, "EXCHANGE LIMIT", nil, nil, nil, "0", "CANCELED", nil, nil,
   182  				15, 0, 0, 0, nil, nil, nil, 0, 0, nil, nil, nil, "API>BFX", nil, nil, nil,
   183  			},
   184  			expected: &order.Order{
   185  				ID:         33961681942,
   186  				CID:        1337,
   187  				Symbol:     "tBTCUSD",
   188  				MTSCreated: 1573482478000,
   189  				MTSUpdated: 1573485373000,
   190  				Amount:     0.001,
   191  				AmountOrig: 0.001,
   192  				Type:       "EXCHANGE LIMIT",
   193  				Status:     "CANCELED",
   194  				Price:      15,
   195  				Notify:     false,
   196  				Hidden:     false,
   197  				Routing:    "API>BFX",
   198  			},
   199  			err: func(t *testing.T, err error) {
   200  				assert.Nil(t, err)
   201  			},
   202  		},
   203  		"ws os item": {
   204  			pld: []interface{}{
   205  				34930659963, nil, 1574955083558, "tETHUSD", 1574955083558, 1574955083573,
   206  				0.201104, 0.201104, "EXCHANGE LIMIT", nil, nil, nil, 0, "ACTIVE", nil, nil, 120,
   207  				0, 0, 0, nil, nil, nil, 0, 0, nil, nil, nil, "BFX", nil, nil, nil,
   208  			},
   209  			expected: &order.Order{
   210  				ID:         34930659963,
   211  				CID:        1574955083558,
   212  				Symbol:     "tETHUSD",
   213  				MTSCreated: 1574955083558,
   214  				MTSUpdated: 1574955083573,
   215  				Amount:     0.201104,
   216  				AmountOrig: 0.201104,
   217  				Type:       "EXCHANGE LIMIT",
   218  				Status:     "ACTIVE",
   219  				Price:      120,
   220  				Notify:     false,
   221  				Hidden:     false,
   222  				Routing:    "BFX",
   223  			},
   224  			err: func(t *testing.T, err error) {
   225  				assert.Nil(t, err)
   226  			},
   227  		},
   228  		"ws on ou oc": {
   229  			pld: []interface{}{
   230  				34930659963, nil, 1574955083558, "tETHUSD", 1574955083558, 1574955354487,
   231  				0.201104, 0.201104, "EXCHANGE LIMIT", nil, nil, nil, 0, "CANCELED", nil, nil,
   232  				120, 0, 0, 0, nil, nil, nil, 0, 0, nil, nil, nil, "BFX", nil, nil, nil,
   233  			},
   234  			expected: &order.Order{
   235  				ID:         34930659963,
   236  				CID:        1574955083558,
   237  				Symbol:     "tETHUSD",
   238  				MTSCreated: 1574955083558,
   239  				MTSUpdated: 1574955354487,
   240  				Amount:     0.201104,
   241  				AmountOrig: 0.201104,
   242  				Type:       "EXCHANGE LIMIT",
   243  				Status:     "CANCELED",
   244  				Price:      120,
   245  				Notify:     false,
   246  				Hidden:     false,
   247  				Routing:    "BFX",
   248  			},
   249  			err: func(t *testing.T, err error) {
   250  				assert.Nil(t, err)
   251  			},
   252  		},
   253  	}
   254  
   255  	for k, v := range cases {
   256  		t.Run(k, func(t *testing.T) {
   257  			got, err := order.FromRaw(v.pld)
   258  			v.err(t, err)
   259  			assert.Equal(t, v.expected, got)
   260  		})
   261  	}
   262  }
   263  
   264  func TestSnapshotFromRaw(t *testing.T) {
   265  	cases := map[string]struct {
   266  		pld      []interface{}
   267  		expected *order.Snapshot
   268  		err      func(*testing.T, error)
   269  	}{
   270  		"invalid pld": {
   271  			pld:      []interface{}{},
   272  			expected: nil,
   273  			err: func(t *testing.T, err error) {
   274  				assert.NotNil(t, err)
   275  			},
   276  		},
   277  		"rest orders hist": {
   278  			pld: []interface{}{
   279  				[]interface{}{
   280  					33961681942, "1227", 1337, "tBTCUSD", 1573482478000, 1573485373000,
   281  					0.001, 0.001, "EXCHANGE LIMIT", nil, nil, nil, "0", "CANCELED", nil, nil,
   282  					15, 0, 0, 0, nil, nil, nil, 0, 0, nil, nil, nil, "API>BFX", nil, nil, nil,
   283  				},
   284  			},
   285  			expected: &order.Snapshot{
   286  				Snapshot: []*order.Order{
   287  					{
   288  						ID:         33961681942,
   289  						CID:        1337,
   290  						Symbol:     "tBTCUSD",
   291  						MTSCreated: 1573482478000,
   292  						MTSUpdated: 1573485373000,
   293  						Amount:     0.001,
   294  						AmountOrig: 0.001,
   295  						Type:       "EXCHANGE LIMIT",
   296  						Status:     "CANCELED",
   297  						Price:      15,
   298  						Notify:     false,
   299  						Hidden:     false,
   300  						Routing:    "API>BFX",
   301  					},
   302  				},
   303  			},
   304  			err: func(t *testing.T, err error) {
   305  				assert.Nil(t, err)
   306  			},
   307  		},
   308  		"ws orders os": {
   309  			pld: []interface{}{
   310  				[]interface{}{
   311  					34930659963, nil, 1574955083558, "tETHUSD", 1574955083558, 1574955083573,
   312  					0.201104, 0.201104, "EXCHANGE LIMIT", nil, nil, nil, 0, "ACTIVE", nil, nil,
   313  					120, 0, 0, 0, nil, nil, nil, 0, 0, nil, nil, nil, "BFX", nil, nil, nil,
   314  				},
   315  			},
   316  			expected: &order.Snapshot{
   317  				Snapshot: []*order.Order{
   318  					{
   319  						ID:         34930659963,
   320  						CID:        1574955083558,
   321  						Symbol:     "tETHUSD",
   322  						MTSCreated: 1574955083558,
   323  						MTSUpdated: 1574955083573,
   324  						Amount:     0.201104,
   325  						AmountOrig: 0.201104,
   326  						Type:       "EXCHANGE LIMIT",
   327  						Status:     "ACTIVE",
   328  						Price:      120,
   329  						Notify:     false,
   330  						Hidden:     false,
   331  						Routing:    "BFX",
   332  					},
   333  				},
   334  			},
   335  			err: func(t *testing.T, err error) {
   336  				assert.Nil(t, err)
   337  			},
   338  		},
   339  	}
   340  
   341  	for k, v := range cases {
   342  		t.Run(k, func(t *testing.T) {
   343  			got, err := order.SnapshotFromRaw(v.pld)
   344  			v.err(t, err)
   345  			assert.Equal(t, v.expected, got)
   346  		})
   347  	}
   348  }
   349  
   350  func TestNewFromRaw(t *testing.T) {
   351  	pld := []interface{}{
   352  		33950998276, nil, 1573476747887, "tETHUSD", 1573476748000, 1573476748000, -0.5,
   353  		-0.5, "LIMIT", nil, nil, nil, 0, "ACTIVE", nil, nil, 220, 0, 0, 0, nil, nil,
   354  		nil, 0, 1, nil, nil, nil, "BFX", nil, nil, nil,
   355  	}
   356  	expected := "order.New"
   357  	o, err := order.NewFromRaw(pld)
   358  	assert.Nil(t, err)
   359  
   360  	got := reflect.TypeOf(o).String()
   361  	assert.Equal(t, expected, got)
   362  }
   363  
   364  func TestUpdateFromRaw(t *testing.T) {
   365  	pld := []interface{}{
   366  		33950998276, nil, 1573476747887, "tETHUSD", 1573476748000, 1573476748000, -0.5,
   367  		-0.5, "LIMIT", nil, nil, nil, 0, "ACTIVE", nil, nil, 220, 0, 0, 0, nil, nil,
   368  		nil, 0, 1, nil, nil, nil, "BFX", nil, nil, nil,
   369  	}
   370  	expected := "order.Update"
   371  	o, err := order.UpdateFromRaw(pld)
   372  	assert.Nil(t, err)
   373  
   374  	got := reflect.TypeOf(o).String()
   375  	assert.Equal(t, expected, got)
   376  }
   377  
   378  func TestCancelFromRaw(t *testing.T) {
   379  	pld := []interface{}{
   380  		33950998276, nil, 1573476747887, "tETHUSD", 1573476748000, 1573476748000, -0.5,
   381  		-0.5, "LIMIT", nil, nil, nil, 0, "ACTIVE", nil, nil, 220, 0, 0, 0, nil, nil,
   382  		nil, 0, 1, nil, nil, nil, "BFX", nil, nil, nil,
   383  	}
   384  	expected := "order.Cancel"
   385  	o, err := order.CancelFromRaw(pld)
   386  	assert.Nil(t, err)
   387  
   388  	got := reflect.TypeOf(o).String()
   389  	assert.Equal(t, expected, got)
   390  }