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

     1  package margin_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/margin"
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestNewMarginFromRaw(t *testing.T) {
    12  	t.Run("invalid arguments", func(t *testing.T) {
    13  		payload := []interface{}{636040}
    14  
    15  		got, err := margin.FromRaw(payload)
    16  		require.NotNil(t, err)
    17  		require.Nil(t, got)
    18  	})
    19  
    20  	t.Run("invalid type", func(t *testing.T) {
    21  		payload := []interface{}{"foo", []interface{}{}}
    22  
    23  		got, err := margin.FromRaw(payload)
    24  		require.NotNil(t, err)
    25  		require.Nil(t, got)
    26  	})
    27  
    28  	t.Run("invalid 'base' type payload", func(t *testing.T) {
    29  		payload := []interface{}{"base", []interface{}{}}
    30  
    31  		got, err := margin.FromRaw(payload)
    32  		require.NotNil(t, err)
    33  		require.Nil(t, got)
    34  	})
    35  
    36  	t.Run("valid 'base' type payload", func(t *testing.T) {
    37  		payload := []interface{}{
    38  			"base",
    39  			[]interface{}{-13.014640000000007, 0, 49331.70267297, 49318.68803297, 27},
    40  		}
    41  
    42  		got, err := margin.FromRaw(payload)
    43  		require.Nil(t, err)
    44  
    45  		expected := &margin.InfoBase{
    46  			UserProfitLoss: -13.014640000000007,
    47  			UserSwaps:      0,
    48  			MarginBalance:  49331.70267297,
    49  			MarginNet:      49318.68803297,
    50  			MarginRequired: 27,
    51  		}
    52  
    53  		assert.Equal(t, expected, got)
    54  	})
    55  
    56  	t.Run("invalid 'sym' type payload #1", func(t *testing.T) {
    57  		payload := []interface{}{"sym", []interface{}{}}
    58  
    59  		got, err := margin.FromRaw(payload)
    60  		require.NotNil(t, err)
    61  		require.Nil(t, got)
    62  	})
    63  
    64  	t.Run("invalid 'sym' type payload #2", func(t *testing.T) {
    65  		payload := []interface{}{"sym", "tETHUSD", []interface{}{}}
    66  
    67  		got, err := margin.FromRaw(payload)
    68  		require.NotNil(t, err)
    69  		require.Nil(t, got)
    70  	})
    71  
    72  	t.Run("valid 'sym' type payload", func(t *testing.T) {
    73  		payload := []interface{}{
    74  			"sym",
    75  			"tETHUSD",
    76  			[]interface{}{
    77  				149361.09689202666, 149639.26293509, 830.0182168075556,
    78  				895.0658432466332, nil, nil, nil, nil,
    79  			},
    80  		}
    81  
    82  		got, err := margin.FromRaw(payload)
    83  		require.Nil(t, err)
    84  
    85  		expected := &margin.InfoUpdate{
    86  			Symbol:          "tETHUSD",
    87  			TradableBalance: 149361.09689202666,
    88  			GrossBalance:    149639.26293509,
    89  			Buy:             830.0182168075556,
    90  			Sell:            895.0658432466332,
    91  		}
    92  
    93  		assert.Equal(t, expected, got)
    94  	})
    95  }