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

     1  package fundingloan_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/fundingloan"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestFromRaw(t *testing.T) {
    12  	cases := map[string]struct {
    13  		pld      []interface{}
    14  		expected *fundingloan.Loan
    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 funding loans item": {
    25  			pld: []interface{}{
    26  				2995368, "fUST", 0, 1574077517000, 1574077517000, 100, nil, "ACTIVE", "FIXED",
    27  				nil, nil, 0.0024, 2, 1574077517000, 1574077517000, 0, nil, nil, 0, nil, 0,
    28  			},
    29  			expected: &fundingloan.Loan{
    30  				ID:            2995368,
    31  				Symbol:        "fUST",
    32  				Side:          0,
    33  				MTSCreated:    1574077517000,
    34  				MTSUpdated:    1574077517000,
    35  				Amount:        100,
    36  				Status:        "ACTIVE",
    37  				RateType:      "FIXED",
    38  				Rate:          0.0024,
    39  				Period:        2,
    40  				MTSOpened:     1574077517000,
    41  				MTSLastPayout: 1574077517000,
    42  				Notify:        false,
    43  				Hidden:        false,
    44  				Insure:        false,
    45  				Renew:         false,
    46  				RateReal:      0,
    47  				NoClose:       false,
    48  			},
    49  			err: func(t *testing.T, err error) {
    50  				assert.Nil(t, err)
    51  			},
    52  		},
    53  		"rest funding loans history item": {
    54  			pld: []interface{}{
    55  				13683223, "fBTC", -1, 1575446268000, 1575446644000, 0.02, nil, "CLOSED (used)", "FIXED",
    56  				nil, nil, 0, 7, 1575446268000, 1575446643000, nil, 0, nil, 0, nil, 0,
    57  			},
    58  			expected: &fundingloan.Loan{
    59  				ID:            13683223,
    60  				Symbol:        "fBTC",
    61  				Side:          -1,
    62  				MTSCreated:    1575446268000,
    63  				MTSUpdated:    1575446644000,
    64  				Amount:        0.02,
    65  				Status:        "CLOSED (used)",
    66  				RateType:      "FIXED",
    67  				Rate:          0,
    68  				Period:        7,
    69  				MTSOpened:     1575446268000,
    70  				MTSLastPayout: 1575446643000,
    71  				Notify:        false,
    72  				Hidden:        false,
    73  				Insure:        false,
    74  				Renew:         false,
    75  				RateReal:      0,
    76  				NoClose:       false,
    77  			},
    78  			err: func(t *testing.T, err error) {
    79  				assert.Nil(t, err)
    80  			},
    81  		},
    82  		"ws fls item": {
    83  			pld: []interface{}{
    84  				2995442, "fUSD", -1, 1575291961000, 1575295850000, 820, 0, "ACTIVE", nil,
    85  				nil, nil, 0.002, 7, 1575282446000, 1575295850000, 0, 0, nil, 0, nil, 0,
    86  			},
    87  			expected: &fundingloan.Loan{
    88  				ID:            2995442,
    89  				Symbol:        "fUSD",
    90  				Side:          -1,
    91  				MTSCreated:    1575291961000,
    92  				MTSUpdated:    1575295850000,
    93  				Amount:        820,
    94  				Status:        "ACTIVE",
    95  				Rate:          0.002,
    96  				Period:        7,
    97  				MTSOpened:     1575282446000,
    98  				MTSLastPayout: 1575295850000,
    99  				Notify:        false,
   100  				Hidden:        false,
   101  				Insure:        false,
   102  				Renew:         false,
   103  				RateReal:      0,
   104  				NoClose:       false,
   105  			},
   106  			err: func(t *testing.T, err error) {
   107  				assert.Nil(t, err)
   108  			},
   109  		},
   110  	}
   111  
   112  	for k, v := range cases {
   113  		t.Run(k, func(t *testing.T) {
   114  			got, err := fundingloan.FromRaw(v.pld)
   115  			v.err(t, err)
   116  			assert.Equal(t, v.expected, got)
   117  		})
   118  	}
   119  }
   120  
   121  func TestSnapshotFromRaw(t *testing.T) {
   122  	cases := map[string]struct {
   123  		pld      []interface{}
   124  		expected *fundingloan.Snapshot
   125  		err      func(*testing.T, error)
   126  	}{
   127  		"invalid pld": {
   128  			pld:      []interface{}{},
   129  			expected: nil,
   130  			err: func(t *testing.T, err error) {
   131  				assert.NotNil(t, err)
   132  			},
   133  		},
   134  		"rest funding loans": {
   135  			pld: []interface{}{
   136  				[]interface{}{
   137  					2995368, "fUST", 0, 1574077517000, 1574077517000, 100, nil, "ACTIVE", "FIXED",
   138  					nil, nil, 0.0024, 2, 1574077517000, 1574077517000, 0, nil, nil, 0, nil, 0,
   139  				},
   140  			},
   141  			expected: &fundingloan.Snapshot{
   142  				Snapshot: []*fundingloan.Loan{
   143  					{
   144  						ID:            2995368,
   145  						Symbol:        "fUST",
   146  						Side:          0,
   147  						MTSCreated:    1574077517000,
   148  						MTSUpdated:    1574077517000,
   149  						Amount:        100,
   150  						Status:        "ACTIVE",
   151  						RateType:      "FIXED",
   152  						Rate:          0.0024,
   153  						Period:        2,
   154  						MTSOpened:     1574077517000,
   155  						MTSLastPayout: 1574077517000,
   156  						Notify:        false,
   157  						Hidden:        false,
   158  						Insure:        false,
   159  						Renew:         false,
   160  						RateReal:      0,
   161  						NoClose:       false,
   162  					},
   163  				},
   164  			},
   165  			err: func(t *testing.T, err error) {
   166  				assert.Nil(t, err)
   167  			},
   168  		},
   169  		"rest funding loan history": {
   170  			pld: []interface{}{
   171  				[]interface{}{
   172  					13683223, "fBTC", -1, 1575446268000, 1575446644000, 0.02, nil, "CLOSED (used)", "FIXED",
   173  					nil, nil, 0, 7, 1575446268000, 1575446643000, nil, 0, nil, 0, nil, 0,
   174  				},
   175  			},
   176  			expected: &fundingloan.Snapshot{
   177  				Snapshot: []*fundingloan.Loan{
   178  					{
   179  						ID:            13683223,
   180  						Symbol:        "fBTC",
   181  						Side:          -1,
   182  						MTSCreated:    1575446268000,
   183  						MTSUpdated:    1575446644000,
   184  						Amount:        0.02,
   185  						Status:        "CLOSED (used)",
   186  						RateType:      "FIXED",
   187  						Rate:          0,
   188  						Period:        7,
   189  						MTSOpened:     1575446268000,
   190  						MTSLastPayout: 1575446643000,
   191  						Notify:        false,
   192  						Hidden:        false,
   193  						Insure:        false,
   194  						Renew:         false,
   195  						RateReal:      0,
   196  						NoClose:       false,
   197  					},
   198  				},
   199  			},
   200  			err: func(t *testing.T, err error) {
   201  				assert.Nil(t, err)
   202  			},
   203  		},
   204  		"ws fls": {
   205  			pld: []interface{}{
   206  				[]interface{}{
   207  					2995442, "fUSD", -1, 1575291961000, 1575295850000, 820, 0, "ACTIVE", nil,
   208  					nil, nil, 0.002, 7, 1575282446000, 1575295850000, 0, 0, nil, 0, nil, 0,
   209  				},
   210  			},
   211  			expected: &fundingloan.Snapshot{
   212  				Snapshot: []*fundingloan.Loan{
   213  					{
   214  						ID:            2995442,
   215  						Symbol:        "fUSD",
   216  						Side:          -1,
   217  						MTSCreated:    1575291961000,
   218  						MTSUpdated:    1575295850000,
   219  						Amount:        820,
   220  						Status:        "ACTIVE",
   221  						Rate:          0.002,
   222  						Period:        7,
   223  						MTSOpened:     1575282446000,
   224  						MTSLastPayout: 1575295850000,
   225  						Notify:        false,
   226  						Hidden:        false,
   227  						Insure:        false,
   228  						Renew:         false,
   229  						RateReal:      0,
   230  						NoClose:       false,
   231  					},
   232  				},
   233  			},
   234  			err: func(t *testing.T, err error) {
   235  				assert.Nil(t, err)
   236  			},
   237  		},
   238  	}
   239  
   240  	for k, v := range cases {
   241  		t.Run(k, func(t *testing.T) {
   242  			got, err := fundingloan.SnapshotFromRaw(v.pld)
   243  			v.err(t, err)
   244  			assert.Equal(t, v.expected, got)
   245  		})
   246  	}
   247  }
   248  
   249  func TestNewFromRaw(t *testing.T) {
   250  	pld := []interface{}{
   251  		2995368, "fUST", 0, 1574077517000, 1574077517000, 100, nil, "ACTIVE", nil,
   252  		nil, nil, 0.0024, 2, 1574077517000, 1574077517000, 0, nil, nil, 1, nil, 0,
   253  	}
   254  
   255  	expected := "fundingloan.New"
   256  	o, err := fundingloan.NewFromRaw(pld)
   257  	assert.Nil(t, err)
   258  
   259  	got := reflect.TypeOf(o).String()
   260  	assert.Equal(t, expected, got)
   261  }
   262  
   263  func TestUpdateFromRaw(t *testing.T) {
   264  	pld := []interface{}{
   265  		2995368, "fUST", 0, 1574077517000, 1574077517000, 100, nil, "ACTIVE", nil,
   266  		nil, nil, 0.0024, 2, 1574077517000, 1574077517000, 0, nil, nil, 1, nil, 0,
   267  	}
   268  
   269  	expected := "fundingloan.Update"
   270  	o, err := fundingloan.UpdateFromRaw(pld)
   271  	assert.Nil(t, err)
   272  
   273  	got := reflect.TypeOf(o).String()
   274  	assert.Equal(t, expected, got)
   275  }
   276  
   277  func TestCancelFromRaw(t *testing.T) {
   278  	pld := []interface{}{
   279  		2995368, "fUST", 0, 1574077517000, 1574077517000, 100, nil, "ACTIVE", nil,
   280  		nil, nil, 0.0024, 2, 1574077517000, 1574077517000, 0, nil, nil, 1, nil, 0,
   281  	}
   282  
   283  	expected := "fundingloan.Cancel"
   284  	o, err := fundingloan.CancelFromRaw(pld)
   285  	assert.Nil(t, err)
   286  
   287  	got := reflect.TypeOf(o).String()
   288  	assert.Equal(t, expected, got)
   289  }