github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/ticker/ticker_test.go (about) 1 package ticker_test 2 3 import ( 4 "testing" 5 6 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/ticker" 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestNewTickerFromRestRaw(t *testing.T) { 12 t.Run("invalid arguments", func(t *testing.T) { 13 payload := []interface{}{"tBTCUSD"} 14 15 got, err := ticker.FromRestRaw(payload) 16 require.NotNil(t, err) 17 require.Nil(t, got) 18 }) 19 20 t.Run("valid arguments", func(t *testing.T) { 21 payload := []interface{}{ 22 "tBTCUSD", 23 10654, 24 53.62425959, 25 10655, 26 76.68743116, 27 745.1, 28 0.0752, 29 10655, 30 14420.34271146, 31 10766, 32 9889.1449809, 33 } 34 35 got, err := ticker.FromRestRaw(payload) 36 require.Nil(t, err) 37 38 expected := &ticker.Ticker{ 39 Symbol: "tBTCUSD", 40 Frr: 0, 41 Bid: 10654, 42 BidPeriod: 0, 43 BidSize: 53.62425959, 44 Ask: 10655, 45 AskPeriod: 0, 46 AskSize: 76.68743116, 47 DailyChange: 745.1, 48 DailyChangePerc: 0.0752, 49 LastPrice: 10655, 50 Volume: 14420.34271146, 51 High: 10766, 52 Low: 9889.1449809, 53 } 54 55 assert.Equal(t, expected, got) 56 }) 57 } 58 59 func TestFromRaw(t *testing.T) { 60 t.Run("invalid arguments", func(t *testing.T) { 61 payload := []interface{}{7616.5, 31.89055171} 62 63 got, err := ticker.FromRaw("tBTCUSD", payload) 64 require.NotNil(t, err) 65 require.Nil(t, got) 66 }) 67 68 t.Run("trading pair update / snapshot ticker", func(t *testing.T) { 69 payload := []interface{}{ 70 7616.5, 71 31.89055171, 72 7617.5, 73 43.358118629999986, 74 -550.8, 75 -0.0674, 76 7617.1, 77 8314.71200815, 78 8257.8, 79 7500, 80 } 81 82 got, err := ticker.FromRaw("tBTCUSD", payload) 83 require.Nil(t, err) 84 85 expected := &ticker.Ticker{ 86 Symbol: "tBTCUSD", 87 Bid: 7616.5, 88 BidSize: 31.89055171, 89 Ask: 7617.5, 90 AskSize: 43.358118629999986, 91 DailyChange: -550.8, 92 DailyChangePerc: -0.0674, 93 LastPrice: 7617.1, 94 Volume: 8314.71200815, 95 High: 8257.8, 96 Low: 7500, 97 } 98 99 assert.Equal(t, expected, got) 100 }) 101 102 t.Run("funding pair snapshot ticker", func(t *testing.T) { 103 payload := []interface{}{ 104 0.0003447013698630137, 105 0.000316, 106 30, 107 1682003.0922634401, 108 0.00031783, 109 4, 110 23336.958053110004, 111 0.00000707, 112 0.0209, 113 0.0003446, 114 156123478.78447533, 115 0.000347, 116 0.00009, 117 nil, 118 nil, 119 146247919.52883354, 120 } 121 122 got, err := ticker.FromRaw("fUSD", payload) 123 require.Nil(t, err) 124 125 expected := &ticker.Ticker{ 126 Symbol: "fUSD", 127 Frr: 0.0003447013698630137, 128 Bid: 0.000316, 129 BidPeriod: 30, 130 BidSize: 1682003.0922634401, 131 Ask: 0.00031783, 132 AskPeriod: 4, 133 AskSize: 23336.958053110004, 134 DailyChange: 0.00000707, 135 DailyChangePerc: 0.0209, 136 LastPrice: 0.0003446, 137 Volume: 156123478.78447533, 138 High: 0.000347, 139 Low: 0.00009, 140 FrrAmountAvailable: 146247919.52883354, 141 } 142 143 assert.Equal(t, expected, got) 144 }) 145 146 t.Run("funding pair update ticker", func(t *testing.T) { 147 payload := []interface{}{ 148 0.0003447013698630137, 149 0.000316, 150 30, 151 1682003.0922634401, 152 0.00031783, 153 4, 154 23336.958053110004, 155 0.00000707, 156 0.0209, 157 0.0003446, 158 156123478.78447533, 159 0.000347, 160 0.00009, 161 } 162 163 got, err := ticker.FromRaw("fUSD", payload) 164 require.Nil(t, err) 165 166 expected := &ticker.Ticker{ 167 Symbol: "fUSD", 168 Frr: 0.0003447013698630137, 169 Bid: 0.000316, 170 BidPeriod: 30, 171 BidSize: 1682003.0922634401, 172 Ask: 0.00031783, 173 AskPeriod: 4, 174 AskSize: 23336.958053110004, 175 DailyChange: 0.00000707, 176 DailyChangePerc: 0.0209, 177 LastPrice: 0.0003446, 178 Volume: 156123478.78447533, 179 High: 0.000347, 180 Low: 0.00009, 181 } 182 183 assert.Equal(t, expected, got) 184 }) 185 } 186 187 func TestSnapshotFromRaw(t *testing.T) { 188 t.Run("invalid arguments", func(t *testing.T) { 189 payload := [][]interface{}{} 190 got, err := ticker.SnapshotFromRaw("tBTCUSD", payload) 191 require.NotNil(t, err) 192 require.Nil(t, got) 193 }) 194 195 t.Run("partially valid arguments", func(t *testing.T) { 196 payload := [][]interface{}{ 197 { 198 9206.8, 199 21.038892079999997, 200 9205.9, 201 31.41755015, 202 38.97852525, 203 0.0043, 204 9205.87852525, 205 1815.68824558, 206 9299, 207 9111.8, 208 }, 209 { 210 9206.8, 211 }, 212 } 213 got, err := ticker.SnapshotFromRaw("tBTCUSD", payload) 214 require.NotNil(t, err) 215 require.Nil(t, got) 216 }) 217 218 t.Run("trading pair snapshot", func(t *testing.T) { 219 payload := [][]interface{}{ 220 { 221 9206.8, 222 21.038892079999997, 223 9205.9, 224 31.41755015, 225 38.97852525, 226 0.0043, 227 9205.87852525, 228 1815.68824558, 229 9299, 230 9111.8, 231 }, 232 } 233 234 got, err := ticker.SnapshotFromRaw("tBTCUSD", payload) 235 require.Nil(t, err) 236 237 expected := &ticker.Snapshot{ 238 Snapshot: []*ticker.Ticker{ 239 { 240 Symbol: "tBTCUSD", 241 Bid: 9206.8, 242 BidSize: 21.038892079999997, 243 Ask: 9205.9, 244 AskSize: 31.41755015, 245 DailyChange: 38.97852525, 246 DailyChangePerc: 0.0043, 247 LastPrice: 9205.87852525, 248 Volume: 1815.68824558, 249 High: 9299, 250 Low: 9111.8, 251 }, 252 }, 253 } 254 255 assert.Equal(t, expected, got) 256 }) 257 } 258 259 func TestFromWSRaw(t *testing.T) { 260 t.Run("invalid arguments", func(t *testing.T) { 261 payload := []interface{}{7616.5, 31.89055171} 262 got, err := ticker.FromWSRaw("tBTCUSD", payload) 263 require.NotNil(t, err) 264 require.Nil(t, got) 265 }) 266 267 t.Run("missing arguments", func(t *testing.T) { 268 payload := []interface{}{} 269 got, err := ticker.FromWSRaw("tBTCUSD", payload) 270 require.NotNil(t, err) 271 require.Nil(t, got) 272 }) 273 274 t.Run("trading pair ticker", func(t *testing.T) { 275 payload := []interface{}{ 276 7616.5, 277 31.89055171, 278 7617.5, 279 43.358118629999986, 280 -550.8, 281 -0.0674, 282 7617.1, 283 8314.71200815, 284 8257.8, 285 7500, 286 } 287 288 got, err := ticker.FromWSRaw("tBTCUSD", payload) 289 require.Nil(t, err) 290 291 expected := &ticker.Ticker{ 292 Symbol: "tBTCUSD", 293 Bid: 7616.5, 294 BidSize: 31.89055171, 295 Ask: 7617.5, 296 AskSize: 43.358118629999986, 297 DailyChange: -550.8, 298 DailyChangePerc: -0.0674, 299 LastPrice: 7617.1, 300 Volume: 8314.71200815, 301 High: 8257.8, 302 Low: 7500, 303 } 304 305 assert.Equal(t, expected, got) 306 }) 307 308 t.Run("valid trading pair ticker snapshot", func(t *testing.T) { 309 payload := []interface{}{ 310 []interface{}{ 311 9206.8, 312 21.038892079999997, 313 9205.9, 314 31.41755015, 315 38.97852525, 316 0.0043, 317 9205.87852525, 318 1815.68824558, 319 9299, 320 9111.8, 321 }, 322 } 323 324 got, err := ticker.FromWSRaw("tBTCUSD", payload) 325 require.Nil(t, err) 326 327 expected := &ticker.Snapshot{ 328 Snapshot: []*ticker.Ticker{ 329 { 330 Symbol: "tBTCUSD", 331 Bid: 9206.8, 332 BidSize: 21.038892079999997, 333 Ask: 9205.9, 334 AskSize: 31.41755015, 335 DailyChange: 38.97852525, 336 DailyChangePerc: 0.0043, 337 LastPrice: 9205.87852525, 338 Volume: 1815.68824558, 339 High: 9299, 340 Low: 9111.8, 341 }, 342 }, 343 } 344 345 assert.Equal(t, expected, got) 346 }) 347 }