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

     1  package notification_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/fundingoffer"
     8  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/notification"
     9  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/order"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestNotificationMapping(t *testing.T) {
    15  	cases := map[string]struct {
    16  		pld      []byte
    17  		expected interface{}
    18  		err      func(*testing.T, error)
    19  	}{
    20  		"on-req": {
    21  			pld: []byte(`[
    22  				0,
    23  				"n",
    24  				[
    25  					1611922089,"on-req",null,null,
    26  					[
    27  						1201469553,0,788,"tBTCUSD",1611922089073,1611922089073,0.001,0.001,"EXCHANGE LIMIT",
    28  						null,null,null,0,"ACTIVE",null,null,33,0,0,0,null,null,null,0,0,null,null,null,
    29  						"API>BFX",null,null,null
    30  					],
    31  					null,"SUCCESS","Submitting exchange limit buy order for 0.001 BTC."
    32  				]
    33  			]`),
    34  			expected: &notification.Notification{
    35  				MTS:       1611922089,
    36  				Type:      "on-req",
    37  				MessageID: 0,
    38  				NotifyInfo: order.New{
    39  					ID:            1201469553,
    40  					GID:           0,
    41  					CID:           788,
    42  					Symbol:        "tBTCUSD",
    43  					MTSCreated:    1611922089073,
    44  					MTSUpdated:    1611922089073,
    45  					Amount:        0.001,
    46  					AmountOrig:    0.001,
    47  					Type:          "EXCHANGE LIMIT",
    48  					TypePrev:      "",
    49  					MTSTif:        0,
    50  					Flags:         0,
    51  					Status:        "ACTIVE",
    52  					Price:         33,
    53  					PriceAvg:      0,
    54  					PriceTrailing: 0,
    55  					PriceAuxLimit: 0,
    56  					Notify:        false,
    57  					Hidden:        false,
    58  					PlacedID:      0,
    59  					Routing:       "API>BFX",
    60  					Meta:          nil,
    61  				},
    62  				Code:   0,
    63  				Status: "SUCCESS",
    64  				Text:   "Submitting exchange limit buy order for 0.001 BTC.",
    65  			},
    66  			err: func(t *testing.T, err error) {
    67  				assert.NoError(t, err)
    68  			},
    69  		},
    70  		"on-req snapshot": {
    71  			pld: []byte(`[
    72  				0,
    73  				"n",
    74  				[
    75  					1611922089,"on-req",null,null,
    76  					[[
    77  						1201469553,0,788,"tBTCUSD",1611922089073,1611922089073,0.001,0.001,"EXCHANGE LIMIT",
    78  						null,null,null,0,"ACTIVE",null,null,33,0,0,0,null,null,null,0,0,null,null,null,
    79  						"API>BFX",null,null,null
    80  					]],
    81  					null,"SUCCESS","Submitting exchange limit buy order for 0.001 BTC."
    82  				]
    83  			]`),
    84  			expected: &notification.Notification{
    85  				MTS:       1611922089,
    86  				Type:      "on-req",
    87  				MessageID: 0,
    88  				NotifyInfo: &order.Snapshot{
    89  					Snapshot: []*order.Order{
    90  						{
    91  							ID:            1201469553,
    92  							GID:           0,
    93  							CID:           788,
    94  							Symbol:        "tBTCUSD",
    95  							MTSCreated:    1611922089073,
    96  							MTSUpdated:    1611922089073,
    97  							Amount:        0.001,
    98  							AmountOrig:    0.001,
    99  							Type:          "EXCHANGE LIMIT",
   100  							TypePrev:      "",
   101  							MTSTif:        0,
   102  							Flags:         0,
   103  							Status:        "ACTIVE",
   104  							Price:         33,
   105  							PriceAvg:      0,
   106  							PriceTrailing: 0,
   107  							PriceAuxLimit: 0,
   108  							Notify:        false,
   109  							Hidden:        false,
   110  							PlacedID:      0,
   111  							Routing:       "API>BFX",
   112  							Meta:          nil,
   113  						},
   114  					},
   115  				},
   116  				Code:   0,
   117  				Status: "SUCCESS",
   118  				Text:   "Submitting exchange limit buy order for 0.001 BTC.",
   119  			},
   120  			err: func(t *testing.T, err error) {
   121  				assert.NoError(t, err)
   122  			},
   123  		},
   124  		"ou-req": {
   125  			pld: []byte(`[
   126  				0,
   127  				"n",
   128  				[
   129  					1575289447641,"ou-req",null,null,
   130  					[
   131  						1185815100,null,1575289350475,"tETHUSD",1575289351944,1575289351948,-3,
   132  						-3,"LIMIT",null,null,null,0,"ACTIVE",null,null,240,0,0,0,null,null,null,
   133  						0,0,null,null,null,"API>BFX",null,null,null
   134  					],
   135  					null,"SUCCESS","Submitting update to limit sell order for 3 ETH."
   136  				]
   137  			]`),
   138  			expected: &notification.Notification{
   139  				MTS:       1575289447641,
   140  				Type:      "ou-req",
   141  				MessageID: 0,
   142  				NotifyInfo: order.Update{
   143  					ID:            1185815100,
   144  					GID:           0,
   145  					CID:           1575289350475,
   146  					Symbol:        "tETHUSD",
   147  					MTSCreated:    1575289351944,
   148  					MTSUpdated:    1575289351948,
   149  					Amount:        -3,
   150  					AmountOrig:    -3,
   151  					Type:          "LIMIT",
   152  					TypePrev:      "",
   153  					MTSTif:        0,
   154  					Flags:         0,
   155  					Status:        "ACTIVE",
   156  					Price:         240,
   157  					PriceAvg:      0,
   158  					PriceTrailing: 0,
   159  					PriceAuxLimit: 0,
   160  					Notify:        false,
   161  					Hidden:        false,
   162  					PlacedID:      0,
   163  					Routing:       "API>BFX",
   164  					Meta:          nil,
   165  				},
   166  				Code:   0,
   167  				Status: "SUCCESS",
   168  				Text:   "Submitting update to limit sell order for 3 ETH.",
   169  			},
   170  			err: func(t *testing.T, err error) {
   171  				assert.NoError(t, err)
   172  			},
   173  		},
   174  		"fon-req": {
   175  			pld: []byte(`[
   176  				0,
   177  				"n",
   178  				[
   179  					1575282446099,"fon-req",null,null,
   180  					[
   181  						41238905,null,null,null,-1000,null,null,null,null,null,
   182  						null,null,null,null,0.002,2,null,null,null,null,null
   183  					],
   184  					null,"SUCCESS","Submitting funding bid of 1000.0 USD at 0.2000 for 2 days."
   185  				]
   186  			]`),
   187  			expected: &notification.Notification{
   188  				MTS:       1575282446099,
   189  				Type:      "fon-req",
   190  				MessageID: 0,
   191  				NotifyInfo: fundingoffer.New{
   192  					ID:         41238905,
   193  					Symbol:     "",
   194  					MTSCreated: 0,
   195  					MTSUpdated: 0,
   196  					Amount:     -1000,
   197  					AmountOrig: 0,
   198  					Type:       "",
   199  					Flags:      map[string]interface{}(nil),
   200  					Status:     "",
   201  					Rate:       0.002,
   202  					Period:     2,
   203  					Notify:     false,
   204  					Hidden:     false,
   205  					Insure:     false,
   206  					Renew:      false,
   207  					RateReal:   0,
   208  				},
   209  				Code:   0,
   210  				Status: "SUCCESS",
   211  				Text:   "Submitting funding bid of 1000.0 USD at 0.2000 for 2 days.",
   212  			},
   213  			err: func(t *testing.T, err error) {
   214  				assert.NoError(t, err)
   215  			},
   216  		},
   217  	}
   218  
   219  	for k, v := range cases {
   220  		t.Run(k, func(t *testing.T) {
   221  			var raw []interface{}
   222  			err := json.Unmarshal(v.pld, &raw)
   223  			require.Nil(t, err)
   224  			pldRaw := raw[len(raw)-1]
   225  			pld := pldRaw.([]interface{})
   226  
   227  			got, err := notification.FromRaw(pld)
   228  			v.err(t, err)
   229  			assert.Equal(t, v.expected, got)
   230  		})
   231  	}
   232  }