github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/trades/trades_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 "github.com/stretchr/testify/require" 9 ) 10 11 func TestTradesFromWSRaw(t *testing.T) { 12 cases := map[string]struct { 13 raw []interface{} 14 pld []interface{} 15 expected interface{} 16 pair string 17 err func(*testing.T, error) 18 }{ 19 "invalid payload": { 20 raw: []interface{}{}, 21 pld: []interface{}{}, 22 expected: nil, 23 err: func(t *testing.T, err error) { 24 assert.Error(t, err) 25 }, 26 }, 27 "valid funding pair snapshot": { 28 raw: []interface{}{}, 29 pld: []interface{}{ 30 []interface{}{133323543, 1574694605000, -59.84, 0.00023647, 2}, 31 }, 32 expected: trades.FundingTradeSnapshot{ 33 Snapshot: []trades.FundingTrade{ 34 { 35 Symbol: "fUSD", 36 ID: 133323543, 37 MTS: 1574694605000, 38 Amount: -59.84, 39 Rate: 0.00023647, 40 Period: 2, 41 }, 42 }, 43 }, 44 pair: "fUSD", 45 err: func(t *testing.T, err error) { 46 require.NoError(t, err) 47 }, 48 }, 49 "invalid funding pair snapshot": { 50 raw: []interface{}{}, 51 pld: []interface{}{ 52 []interface{}{}, 53 }, 54 expected: trades.FundingTradeSnapshot{}, 55 pair: "fUSD", 56 err: func(t *testing.T, err error) { 57 assert.Error(t, err) 58 }, 59 }, 60 "valid trading pair snapshot": { 61 raw: []interface{}{}, 62 pld: []interface{}{ 63 []interface{}{401597395, 1574694478808, 0.005, 7245.3}, 64 }, 65 expected: trades.TradeSnapshot{ 66 Snapshot: []trades.Trade{ 67 { 68 Pair: "tBTCUSD", 69 ID: 401597395, 70 MTS: 1574694478808, 71 Amount: 0.005, 72 Price: 7245.3, 73 }, 74 }, 75 }, 76 pair: "tBTCUSD", 77 err: func(t *testing.T, err error) { 78 require.NoError(t, err) 79 }, 80 }, 81 "invalid trading pair snapshot": { 82 raw: []interface{}{}, 83 pld: []interface{}{ 84 []interface{}{}, 85 }, 86 expected: trades.TradeSnapshot{}, 87 pair: "tBTCUSD", 88 err: func(t *testing.T, err error) { 89 assert.Error(t, err) 90 }, 91 }, 92 "valid trading execution update": { 93 raw: []interface{}{17470, "tu", []interface{}{401597395, 1574694478808, 0.005, 7245.3}}, 94 pld: []interface{}{401597395, 1574694478808, 0.005, 7245.3}, 95 expected: trades.TradeExecutionUpdate{ 96 Pair: "tBTCUSD", 97 ID: 401597395, 98 MTS: 1574694478808, 99 Amount: 0.005, 100 Price: 7245.3, 101 }, 102 pair: "tBTCUSD", 103 err: func(t *testing.T, err error) { 104 require.NoError(t, err) 105 }, 106 }, 107 "invalid trading execution update": { 108 raw: []interface{}{17470, "tu", []interface{}{}}, 109 pld: []interface{}{}, 110 expected: nil, 111 pair: "tBTCUSD", 112 err: func(t *testing.T, err error) { 113 assert.Error(t, err) 114 }, 115 }, 116 "valid trading execution": { 117 raw: []interface{}{17470, "te", []interface{}{401597395, 1574694478808, 0.005, 7245.3}}, 118 pld: []interface{}{401597395, 1574694478808, 0.005, 7245.3}, 119 expected: trades.TradeExecuted{ 120 Pair: "tBTCUSD", 121 ID: 401597395, 122 MTS: 1574694478808, 123 Amount: 0.005, 124 Price: 7245.3, 125 }, 126 pair: "tBTCUSD", 127 err: func(t *testing.T, err error) { 128 require.NoError(t, err) 129 }, 130 }, 131 "invalid trading execution": { 132 raw: []interface{}{17470, "te", []interface{}{}}, 133 pld: []interface{}{}, 134 expected: nil, 135 pair: "tBTCUSD", 136 err: func(t *testing.T, err error) { 137 assert.Error(t, err) 138 }, 139 }, 140 "valid funding trading execution": { 141 raw: []interface{}{337371, "fte", []interface{}{133323543, 1574694605000, -59.84, 0.00023647, 2}}, 142 pld: []interface{}{133323543, 1574694605000, -59.84, 0.00023647, 2}, 143 expected: trades.FundingTradeExecuted{ 144 Symbol: "fUSD", 145 ID: 133323543, 146 MTS: 1574694605000, 147 Amount: -59.84, 148 Rate: 0.00023647, 149 Period: 2, 150 }, 151 pair: "fUSD", 152 err: func(t *testing.T, err error) { 153 require.NoError(t, err) 154 }, 155 }, 156 "ivalid funding trading execution": { 157 raw: []interface{}{337371, "fte", []interface{}{}}, 158 pld: []interface{}{}, 159 expected: nil, 160 pair: "fUSD", 161 err: func(t *testing.T, err error) { 162 assert.Error(t, err) 163 }, 164 }, 165 "valid funding trading execution update": { 166 raw: []interface{}{337371, "ftu", []interface{}{133323543, 1574694605000, -59.84, 0.00023647, 2}}, 167 pld: []interface{}{133323543, 1574694605000, -59.84, 0.00023647, 2}, 168 expected: trades.FundingTradeExecutionUpdate{ 169 Symbol: "fUSD", 170 ID: 133323543, 171 MTS: 1574694605000, 172 Amount: -59.84, 173 Rate: 0.00023647, 174 Period: 2, 175 }, 176 pair: "fUSD", 177 err: func(t *testing.T, err error) { 178 require.NoError(t, err) 179 }, 180 }, 181 "ivalid funding trading execution update": { 182 raw: []interface{}{337371, "ftu", []interface{}{}}, 183 pld: []interface{}{}, 184 expected: nil, 185 pair: "fUSD", 186 err: func(t *testing.T, err error) { 187 assert.Error(t, err) 188 }, 189 }, 190 "valid trade": { 191 raw: []interface{}{17470, []interface{}{401597395, 1574694478808, 0.005, 7245.3}}, 192 pld: []interface{}{401597395, 1574694478808, 0.005, 7245.3}, 193 expected: trades.Trade{ 194 Pair: "tBTCUSD", 195 ID: 401597395, 196 MTS: 1574694478808, 197 Amount: 0.005, 198 Price: 7245.3, 199 }, 200 pair: "tBTCUSD", 201 err: func(t *testing.T, err error) { 202 require.NoError(t, err) 203 }, 204 }, 205 "ivalid trade": { 206 raw: []interface{}{17470, []interface{}{}}, 207 pld: []interface{}{}, 208 expected: nil, 209 pair: "tBTCUSD", 210 err: func(t *testing.T, err error) { 211 assert.Error(t, err) 212 }, 213 }, 214 } 215 216 for k, v := range cases { 217 t.Run(k, func(t *testing.T) { 218 got, err := trades.FromWSRaw(v.pair, v.raw, v.pld) 219 v.err(t, err) 220 assert.Equal(t, v.expected, got) 221 }) 222 } 223 }