github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/book/book_test.go (about) 1 package book_test 2 3 import ( 4 "testing" 5 6 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/book" 7 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/common" 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestNewBookFromRaw(t *testing.T) { 13 t.Run("insufficient arguments", func(t *testing.T) { 14 payload := []interface{}{1591614631576} 15 rawNums := []interface{}{1.12345} 16 17 b, err := book.FromRaw("tBTCUSD", "P0", payload, rawNums) 18 require.NotNil(t, err) 19 require.Nil(t, b) 20 }) 21 22 t.Run("valid trading arguments", func(t *testing.T) { 23 payload := []interface{}{ 24 98169.99541156, 2, 0.000202, 25 } 26 27 rawNums := []interface{}{98169.99541156, 2, 0.000202} 28 29 b, err := book.FromRaw("tBTCUSD", "P0", payload, rawNums) 30 require.Nil(t, err) 31 32 expected := &book.Book{ 33 Symbol: "tBTCUSD", 34 Price: 98169.99541156, 35 PriceJsNum: "98169.99541156", 36 Count: 2, 37 Amount: 0.000202, 38 AmountJsNum: "0.000202", 39 Side: common.Bid, 40 Action: book.BookEntry, 41 } 42 assert.Equal(t, expected, b) 43 }) 44 45 t.Run("valid raw trading arguments", func(t *testing.T) { 46 payload := []interface{}{ 47 34006738527, 8744.9, 0.25603413, 48 } 49 50 rawNums := []interface{}{34006738527, 8744.9, 0.25603413} 51 52 b, err := book.FromRaw("tBTCUSD", "R0", payload, rawNums) 53 require.Nil(t, err) 54 55 expected := &book.Book{ 56 ID: 34006738527, 57 Symbol: "tBTCUSD", 58 Price: 8744.9, 59 PriceJsNum: "8744.9", 60 Amount: 0.25603413, 61 AmountJsNum: "0.25603413", 62 Side: common.Bid, 63 Action: book.BookEntry, 64 } 65 assert.Equal(t, expected, b) 66 }) 67 68 t.Run("valid funding arguments", func(t *testing.T) { 69 payload := []interface{}{ 70 0.0003301, 30, 1, -3862.874, 71 } 72 73 rawNums := []interface{}{0.0003301, 30, 1, -3862.874} 74 75 b, err := book.FromRaw("fUSD", "P0", payload, rawNums) 76 require.Nil(t, err) 77 78 expected := &book.Book{ 79 Symbol: "fUSD", 80 Count: 1, 81 Period: 30, 82 Amount: -3862.874, 83 Rate: 0.0003301, 84 AmountJsNum: "-3862.874", 85 } 86 assert.Equal(t, expected, b) 87 }) 88 89 t.Run("valid raw funding arguments", func(t *testing.T) { 90 payload := []interface{}{ 91 645902785, 30, 0.0003301, -3862.874, 92 } 93 94 rawNums := []interface{}{645902785, 30, 0.0003301, -3862.874} 95 96 b, err := book.FromRaw("fUSD", "R0", payload, rawNums) 97 require.Nil(t, err) 98 99 expected := &book.Book{ 100 Symbol: "fUSD", 101 ID: 645902785, 102 Period: 30, 103 Amount: -3862.874, 104 Rate: 0.0003301, 105 AmountJsNum: "-3862.874", 106 } 107 108 assert.Equal(t, expected, b) 109 }) 110 }