github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/trades/trade_test.go (about) 1 package trades_test 2 3 import ( 4 "testing" 5 6 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/trades" 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestTradeFromRaw(t *testing.T) { 11 cases := map[string]struct { 12 pld []interface{} 13 expected trades.Trade 14 err func(*testing.T, error) 15 }{ 16 "invalid payload": { 17 pld: []interface{}{401597393}, 18 expected: trades.Trade{}, 19 err: func(t *testing.T, err error) { 20 assert.Error(t, err) 21 }, 22 }, 23 "valid payload": { 24 pld: []interface{}{401597393, 1574694475039, 0.005, 7244.9}, 25 expected: trades.Trade{ 26 Pair: "tBTCUSD", 27 ID: 401597393, 28 MTS: 1574694475039, 29 Amount: 0.005, 30 Price: 7244.9, 31 }, 32 err: func(t *testing.T, err error) { 33 assert.NoError(t, err) 34 }, 35 }, 36 } 37 38 for k, v := range cases { 39 t.Run(k, func(t *testing.T) { 40 got, err := trades.TFromRaw("tBTCUSD", v.pld) 41 v.err(t, err) 42 assert.Equal(t, v.expected, got) 43 }) 44 } 45 } 46 47 func TestTradeExecutionFromRaw(t *testing.T) { 48 cases := map[string]struct { 49 pld []interface{} 50 expected trades.TradeExecuted 51 err func(*testing.T, error) 52 }{ 53 "invalid payload": { 54 pld: []interface{}{401597395}, 55 expected: trades.TradeExecuted{}, 56 err: func(t *testing.T, err error) { 57 assert.Error(t, err) 58 }, 59 }, 60 "valid payload": { 61 pld: []interface{}{401597395, 1574694478808, 0.005, 7245.3}, 62 expected: trades.TradeExecuted{ 63 Pair: "tBTCUSD", 64 ID: 401597395, 65 MTS: 1574694478808, 66 Amount: 0.005, 67 Price: 7245.3, 68 }, 69 err: func(t *testing.T, err error) { 70 assert.NoError(t, err) 71 }, 72 }, 73 } 74 75 for k, v := range cases { 76 t.Run(k, func(t *testing.T) { 77 got, err := trades.TEFromRaw("tBTCUSD", v.pld) 78 v.err(t, err) 79 assert.Equal(t, v.expected, got) 80 }) 81 } 82 } 83 84 func TestTradeExecutionUpdateFromRaw(t *testing.T) { 85 cases := map[string]struct { 86 pld []interface{} 87 expected trades.TradeExecutionUpdate 88 err func(*testing.T, error) 89 }{ 90 "invalid payload": { 91 pld: []interface{}{401597395}, 92 expected: trades.TradeExecutionUpdate{}, 93 err: func(t *testing.T, err error) { 94 assert.Error(t, err) 95 }, 96 }, 97 "valid payload": { 98 pld: []interface{}{401597395, 1574694478808, 0.005, 7245.3}, 99 expected: trades.TradeExecutionUpdate{ 100 Pair: "tBTCUSD", 101 ID: 401597395, 102 MTS: 1574694478808, 103 Amount: 0.005, 104 Price: 7245.3, 105 }, 106 err: func(t *testing.T, err error) { 107 assert.NoError(t, err) 108 }, 109 }, 110 } 111 112 for k, v := range cases { 113 t.Run(k, func(t *testing.T) { 114 got, err := trades.TEUFromRaw("tBTCUSD", v.pld) 115 v.err(t, err) 116 assert.Equal(t, v.expected, got) 117 }) 118 } 119 } 120 121 func TestTradeSnapshotFromRaw(t *testing.T) { 122 cases := map[string]struct { 123 pld [][]interface{} 124 expected trades.TradeSnapshot 125 err func(*testing.T, error) 126 }{ 127 "invalid payload": { 128 pld: [][]interface{}{}, 129 expected: trades.TradeSnapshot{}, 130 err: func(t *testing.T, err error) { 131 assert.Error(t, err) 132 }, 133 }, 134 "valid payload": { 135 pld: [][]interface{}{{401597395, 1574694478808, 0.005, 7245.3}}, 136 expected: trades.TradeSnapshot{ 137 Snapshot: []trades.Trade{ 138 { 139 Pair: "tBTCUSD", 140 ID: 401597395, 141 MTS: 1574694478808, 142 Amount: 0.005, 143 Price: 7245.3, 144 }, 145 }, 146 }, 147 err: func(t *testing.T, err error) { 148 assert.NoError(t, err) 149 }, 150 }, 151 } 152 153 for k, v := range cases { 154 t.Run(k, func(t *testing.T) { 155 got, err := trades.TSnapshotFromRaw("tBTCUSD", v.pld) 156 v.err(t, err) 157 assert.Equal(t, v.expected, got) 158 }) 159 } 160 }