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

     1  package wallet_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/wallet"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestUpdateFromRaw(t *testing.T) {
    11  	cases := map[string]struct {
    12  		pld      []interface{}
    13  		expected *wallet.Wallet
    14  		err      func(*testing.T, error)
    15  	}{
    16  		"invalid pld": {
    17  			pld:      []interface{}{"exchange"},
    18  			expected: nil,
    19  			err: func(t *testing.T, err error) {
    20  				assert.NotNil(t, err)
    21  			},
    22  		},
    23  		"valid rest pld with meta": {
    24  			pld: []interface{}{
    25  				"exchange", "UST", 19788.6529257, 0, 19788.6529257,
    26  				"Exchange 2.0 UST for USD @ 11.696",
    27  				map[string]interface{}{
    28  					"reason":        "TRADE",
    29  					"order_id":      1189740779,
    30  					"order_id_oppo": 1189785673,
    31  					"trade_price":   "11.696",
    32  					"trade_amount":  "-2.0",
    33  					"order_cid":     1598516362757,
    34  					"order_gid":     1598516362629,
    35  				},
    36  			},
    37  			expected: &wallet.Wallet{
    38  				Type:              "exchange",
    39  				Currency:          "UST",
    40  				Balance:           19788.6529257,
    41  				UnsettledInterest: 0,
    42  				BalanceAvailable:  19788.6529257,
    43  				LastChange:        "Exchange 2.0 UST for USD @ 11.696",
    44  				TradeDetails: map[string]interface{}{
    45  					"order_cid":     1598516362757,
    46  					"order_gid":     1598516362629,
    47  					"order_id":      1189740779,
    48  					"order_id_oppo": 1189785673,
    49  					"reason":        "TRADE",
    50  					"trade_amount":  "-2.0",
    51  					"trade_price":   "11.696",
    52  				},
    53  			},
    54  			err: func(t *testing.T, err error) {
    55  				assert.Nil(t, err)
    56  			},
    57  		},
    58  		"valid rest pld no meta": {
    59  			pld: []interface{}{
    60  				"exchange", "UST", 19788.6529257, 0, 19788.6529257,
    61  				"Exchange 2.0 UST for USD @ 11.696", nil,
    62  			},
    63  			expected: &wallet.Wallet{
    64  				Type:              "exchange",
    65  				Currency:          "UST",
    66  				Balance:           19788.6529257,
    67  				UnsettledInterest: 0,
    68  				BalanceAvailable:  19788.6529257,
    69  				LastChange:        "Exchange 2.0 UST for USD @ 11.696",
    70  			},
    71  			err: func(t *testing.T, err error) {
    72  				assert.Nil(t, err)
    73  			},
    74  		},
    75  		"valid ws wallet with meta": {
    76  			pld: []interface{}{
    77  				"exchange", "BTC", 1.61169184, 0, nil,
    78  				"Exchange 0.01 BTC for USD @ 7804.6",
    79  				map[string]interface{}{
    80  					"reason":        "TRADE",
    81  					"order_id":      34988418651,
    82  					"order_id_oppo": 34990541044,
    83  					"trade_price":   "7804.6",
    84  					"trade_amount":  "0.01",
    85  				},
    86  			},
    87  			expected: &wallet.Wallet{
    88  				Type:              "exchange",
    89  				Currency:          "BTC",
    90  				Balance:           1.61169184,
    91  				UnsettledInterest: 0,
    92  				BalanceAvailable:  0,
    93  				LastChange:        "Exchange 0.01 BTC for USD @ 7804.6",
    94  				TradeDetails: map[string]interface{}{
    95  					"order_id":      34988418651,
    96  					"order_id_oppo": 34990541044,
    97  					"reason":        "TRADE",
    98  					"trade_amount":  "0.01",
    99  					"trade_price":   "7804.6",
   100  				},
   101  			},
   102  			err: func(t *testing.T, err error) {
   103  				assert.Nil(t, err)
   104  			},
   105  		},
   106  		"valid ws wallet no meta": {
   107  			pld: []interface{}{
   108  				"exchange", "BTC", 1.61169184, 0, nil,
   109  				"Exchange 0.01 BTC for USD @ 7804.6", nil,
   110  			},
   111  			expected: &wallet.Wallet{
   112  				Type:              "exchange",
   113  				Currency:          "BTC",
   114  				Balance:           1.61169184,
   115  				UnsettledInterest: 0,
   116  				BalanceAvailable:  0,
   117  				LastChange:        "Exchange 0.01 BTC for USD @ 7804.6",
   118  			},
   119  			err: func(t *testing.T, err error) {
   120  				assert.Nil(t, err)
   121  			},
   122  		},
   123  	}
   124  
   125  	for k, v := range cases {
   126  		t.Run(k, func(t *testing.T) {
   127  			got, err := wallet.FromRaw(v.pld)
   128  			v.err(t, err)
   129  			assert.Equal(t, v.expected, got)
   130  		})
   131  	}
   132  }
   133  
   134  func TestSnapshotFromRaw(t *testing.T) {
   135  	cases := map[string]struct {
   136  		pld      []interface{}
   137  		expected *wallet.Snapshot
   138  		err      func(*testing.T, error)
   139  	}{
   140  		"invalid pld": {
   141  			pld:      []interface{}{},
   142  			expected: nil,
   143  			err: func(t *testing.T, err error) {
   144  				assert.NotNil(t, err)
   145  			},
   146  		},
   147  		"valid rest pld": {
   148  			pld: []interface{}{
   149  				[]interface{}{
   150  					"exchange", "UST", 19788.6529257, 0, 19788.6529257,
   151  					"Exchange 2.0 UST for USD @ 11.696",
   152  					map[string]interface{}{
   153  						"reason":        "TRADE",
   154  						"order_id":      1189740779,
   155  						"order_id_oppo": 1189785673,
   156  						"trade_price":   "11.696",
   157  						"trade_amount":  "-2.0",
   158  						"order_cid":     1598516362757,
   159  						"order_gid":     1598516362629,
   160  					},
   161  				},
   162  				[]interface{}{
   163  					"exchange", "UST", 19788.6529257, 0, 19788.6529257,
   164  					"Exchange 2.0 UST for USD @ 11.696", nil,
   165  				},
   166  			},
   167  			expected: &wallet.Snapshot{
   168  				Snapshot: []*wallet.Wallet{
   169  					{
   170  						Type:              "exchange",
   171  						Currency:          "UST",
   172  						Balance:           19788.6529257,
   173  						UnsettledInterest: 0,
   174  						BalanceAvailable:  19788.6529257,
   175  						LastChange:        "Exchange 2.0 UST for USD @ 11.696",
   176  						TradeDetails: map[string]interface{}{
   177  							"order_cid":     1598516362757,
   178  							"order_gid":     1598516362629,
   179  							"order_id":      1189740779,
   180  							"order_id_oppo": 1189785673,
   181  							"reason":        "TRADE",
   182  							"trade_amount":  "-2.0",
   183  							"trade_price":   "11.696",
   184  						},
   185  					},
   186  					{
   187  						Type:              "exchange",
   188  						Currency:          "UST",
   189  						Balance:           19788.6529257,
   190  						UnsettledInterest: 0,
   191  						BalanceAvailable:  19788.6529257,
   192  						LastChange:        "Exchange 2.0 UST for USD @ 11.696",
   193  					},
   194  				},
   195  			},
   196  			err: func(t *testing.T, err error) {
   197  				assert.Nil(t, err)
   198  			},
   199  		},
   200  		"valid ws pld": {
   201  			pld: []interface{}{
   202  				[]interface{}{"exchange", "SAN", 19.76, 0, nil, nil, nil},
   203  			},
   204  			expected: &wallet.Snapshot{
   205  				Snapshot: []*wallet.Wallet{
   206  					{
   207  						Type:              "exchange",
   208  						Currency:          "SAN",
   209  						Balance:           19.76,
   210  						UnsettledInterest: 0,
   211  						BalanceAvailable:  0,
   212  					},
   213  				},
   214  			},
   215  			err: func(t *testing.T, err error) {
   216  				assert.Nil(t, err)
   217  			},
   218  		},
   219  	}
   220  
   221  	for k, v := range cases {
   222  		t.Run(k, func(t *testing.T) {
   223  			got, err := wallet.SnapshotFromRaw(v.pld)
   224  			v.err(t, err)
   225  			assert.Equal(t, v.expected, got)
   226  		})
   227  	}
   228  }