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

     1  package tickerhist_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/tickerhist"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestTickerHistFromRaw(t *testing.T) {
    11  	cases := map[string]struct {
    12  		pld      []interface{}
    13  		expected tickerhist.TickerHist
    14  		err      func(*testing.T, error)
    15  	}{
    16  		"invalid payload": {
    17  			pld:      []interface{}{402088407},
    18  			expected: tickerhist.TickerHist{},
    19  			err: func(t *testing.T, err error) {
    20  				assert.Error(t, err)
    21  			},
    22  		},
    23  		"valid payload": {
    24  			pld: []interface{}{
    25  				"tBTCUSD", 54281, nil, 54282, nil, nil, nil, nil, nil, nil, nil, nil, 1619769715000,
    26  			},
    27  			expected: tickerhist.TickerHist{
    28  				Symbol: "tBTCUSD",
    29  				Bid:    54281,
    30  				Ask:    54282,
    31  				MTS:    1619769715000,
    32  			},
    33  			err: func(t *testing.T, err error) {
    34  				assert.NoError(t, err)
    35  			},
    36  		},
    37  	}
    38  
    39  	for k, v := range cases {
    40  		t.Run(k, func(t *testing.T) {
    41  			got, err := tickerhist.FromRaw(v.pld)
    42  			v.err(t, err)
    43  			assert.Equal(t, v.expected, got)
    44  		})
    45  	}
    46  }
    47  
    48  func TestTickerHistSnapshotFromRaw(t *testing.T) {
    49  	cases := map[string]struct {
    50  		pld      [][]interface{}
    51  		expected tickerhist.Snapshot
    52  	}{
    53  		"invalid payload": {
    54  			pld:      [][]interface{}{},
    55  			expected: tickerhist.Snapshot{},
    56  		},
    57  		"valid payload": {
    58  			pld: [][]interface{}{
    59  				{"tBTCUSD", 54281, nil, 54282, nil, nil, nil, nil, nil, nil, nil, nil, 1619769715000},
    60  				{"tLTCUSD", 264.66, nil, 264.9, nil, nil, nil, nil, nil, nil, nil, nil, 1619770205000},
    61  			},
    62  			expected: tickerhist.Snapshot{
    63  				Snapshot: []tickerhist.TickerHist{
    64  					{
    65  						Symbol: "tBTCUSD",
    66  						Bid:    54281,
    67  						Ask:    54282,
    68  						MTS:    1619769715000,
    69  					},
    70  					{
    71  						Symbol: "tLTCUSD",
    72  						Bid:    264.66,
    73  						Ask:    264.9,
    74  						MTS:    1619770205000,
    75  					},
    76  				},
    77  			},
    78  		},
    79  	}
    80  
    81  	for k, v := range cases {
    82  		t.Run(k, func(t *testing.T) {
    83  			got := tickerhist.SnapshotFromRaw(v.pld)
    84  			assert.Equal(t, v.expected, got)
    85  		})
    86  	}
    87  }