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

     1  package ledger_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/ledger"
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestNewLedgerFromRaw(t *testing.T) {
    12  	t.Run("insufficient arguments", func(t *testing.T) {
    13  		payload := []interface{}{138797990}
    14  
    15  		w, err := ledger.FromRaw(payload)
    16  		require.NotNil(t, err)
    17  		require.Nil(t, w)
    18  	})
    19  
    20  	t.Run("valid arguments", func(t *testing.T) {
    21  		payload := []interface{}{
    22  			138797990,
    23  			"BTC",
    24  			nil,
    25  			1.5948918e+12,
    26  			nil,
    27  			0.001,
    28  			30.002,
    29  			nil,
    30  			"Transfer of 0.001 BTC from wallet Exchange to Trading on wallet margin",
    31  		}
    32  
    33  		w, err := ledger.FromRaw(payload)
    34  		require.Nil(t, err)
    35  
    36  		expected := &ledger.Ledger{
    37  			ID:          138797990,
    38  			Currency:    "BTC",
    39  			MTS:         1594891800000,
    40  			Amount:      0.001,
    41  			Balance:     30.002,
    42  			Description: "Transfer of 0.001 BTC from wallet Exchange to Trading on wallet margin",
    43  		}
    44  
    45  		assert.Equal(t, expected, w)
    46  	})
    47  }
    48  
    49  func TestLedgerSnapshotFromRaw(t *testing.T) {
    50  	t.Run("rest success", func(t *testing.T) {
    51  		payload := []interface{}{
    52  			[]interface{}{
    53  				138797990,
    54  				"BTC",
    55  				nil,
    56  				1.5948918e+12,
    57  				nil,
    58  				0.001,
    59  				30.002,
    60  				nil,
    61  				"Transfer of 0.001 BTC from wallet Exchange to Trading on wallet margin",
    62  			},
    63  			[]interface{}{
    64  				138797710,
    65  				"BTC",
    66  				nil,
    67  				1.5948919e+12,
    68  				nil,
    69  				-0.001,
    70  				39.9988,
    71  				nil,
    72  				"Transfer of 0.001 BTC from wallet Exchange to Trading on wallet exchange",
    73  			},
    74  		}
    75  
    76  		got, err := ledger.SnapshotFromRaw(payload, ledger.FromRaw)
    77  		require.Nil(t, err)
    78  
    79  		expected := &ledger.Snapshot{
    80  			Snapshot: []*ledger.Ledger{
    81  				{
    82  					ID:          138797990,
    83  					Currency:    "BTC",
    84  					MTS:         1594891800000,
    85  					Amount:      0.001,
    86  					Balance:     30.002,
    87  					Description: "Transfer of 0.001 BTC from wallet Exchange to Trading on wallet margin",
    88  				},
    89  				{
    90  					ID:          138797710,
    91  					Currency:    "BTC",
    92  					MTS:         1594891900000,
    93  					Amount:      -0.001,
    94  					Balance:     39.9988,
    95  					Description: "Transfer of 0.001 BTC from wallet Exchange to Trading on wallet exchange",
    96  				},
    97  			},
    98  		}
    99  
   100  		assert.Equal(t, expected, got)
   101  	})
   102  
   103  	t.Run("rest fail", func(t *testing.T) {
   104  		payload := []interface{}{
   105  			[]interface{}{138797990},
   106  			[]interface{}{
   107  				138797710,
   108  				"BTC",
   109  				nil,
   110  				1.5948919e+12,
   111  				nil,
   112  				-0.001,
   113  				39.9988,
   114  				nil,
   115  				"Transfer of 0.001 BTC from wallet Exchange to Trading on wallet exchange",
   116  			},
   117  		}
   118  
   119  		got, err := ledger.SnapshotFromRaw(payload, ledger.FromRaw)
   120  		require.NotNil(t, err)
   121  		require.Nil(t, got)
   122  	})
   123  }