github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/fundingoffer/fundingoffer_test.go (about) 1 package fundingoffer_test 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/fundingoffer" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestFromRaw(t *testing.T) { 12 cases := map[string]struct { 13 pld []interface{} 14 expected *fundingoffer.Offer 15 err func(*testing.T, error) 16 }{ 17 "invalid pld": { 18 pld: []interface{}{"exchange"}, 19 expected: nil, 20 err: func(t *testing.T, err error) { 21 assert.NotNil(t, err) 22 }, 23 }, 24 "rest active funding offer": { 25 pld: []interface{}{ 26 652606505, "fETH", 1574000611000, 1574000611000, 0.29797676, 0.29797676, 27 "LIMIT", nil, nil, 0, "ACTIVE", nil, nil, nil, 0.0002, 2, 0, nil, nil, 0, nil, 28 }, 29 expected: &fundingoffer.Offer{ 30 ID: 652606505, 31 Symbol: "fETH", 32 MTSCreated: 1574000611000, 33 MTSUpdated: 1574000611000, 34 Amount: 0.29797676, 35 AmountOrig: 0.29797676, 36 Type: "LIMIT", 37 Status: "ACTIVE", 38 Rate: 0.0002, 39 Period: 2, 40 Notify: false, 41 Hidden: false, 42 Insure: false, 43 Renew: false, 44 RateReal: 0, 45 }, 46 err: func(t *testing.T, err error) { 47 assert.Nil(t, err) 48 }, 49 }, 50 "rest submit funding offer": { 51 pld: []interface{}{ 52 604366339, "fUSD", 1568713496502, 1568713496502, 50, 50, "LIMIT", nil, 53 nil, nil, "ACTIVE", nil, nil, nil, 0.00002, 2, false, nil, nil, false, nil, 54 }, 55 expected: &fundingoffer.Offer{ 56 ID: 604366339, 57 Symbol: "fUSD", 58 MTSCreated: 1568713496502, 59 MTSUpdated: 1568713496502, 60 Amount: 50, 61 AmountOrig: 50, 62 Type: "LIMIT", 63 Status: "ACTIVE", 64 Rate: 2e-05, 65 Period: 2, 66 Notify: false, 67 Hidden: false, 68 Insure: false, 69 Renew: false, 70 RateReal: 0, 71 }, 72 err: func(t *testing.T, err error) { 73 assert.Nil(t, err) 74 }, 75 }, 76 "rest cancel funding offer": { 77 pld: []interface{}{ 78 604393839, "fUSD", 1568716545000, 1568716545000, 50, 50, "LIMIT", nil, 79 nil, nil, "ACTIVE", nil, nil, nil, 0.06, 2, false, nil, nil, false, nil, 80 }, 81 expected: &fundingoffer.Offer{ 82 ID: 604393839, 83 Symbol: "fUSD", 84 MTSCreated: 1568716545000, 85 MTSUpdated: 1568716545000, 86 Amount: 50, 87 AmountOrig: 50, 88 Type: "LIMIT", 89 Status: "ACTIVE", 90 Rate: 0.06, 91 Period: 2, 92 Notify: false, 93 Hidden: false, 94 Insure: false, 95 Renew: false, 96 RateReal: 0, 97 }, 98 err: func(t *testing.T, err error) { 99 assert.Nil(t, err) 100 }, 101 }, 102 "rest funding offer hist item": { 103 pld: []interface{}{ 104 653170899, "fUSD", 1574072620000, 1574072620000, 0, 105 -57.9, nil, nil, nil, nil, "EXECUTED at 0.0368% (57.9)", 106 nil, nil, nil, 0.000369, 2, 0, 0, nil, nil, nil, 107 }, 108 expected: &fundingoffer.Offer{ 109 ID: 653170899, 110 Symbol: "fUSD", 111 MTSCreated: 1574072620000, 112 MTSUpdated: 1574072620000, 113 Amount: 0, 114 AmountOrig: -57.9, 115 Status: "EXECUTED at 0.0368% (57.9)", 116 Rate: 0.000369, 117 Period: 2, 118 Notify: false, 119 Hidden: false, 120 Insure: false, 121 Renew: false, 122 RateReal: 0, 123 }, 124 err: func(t *testing.T, err error) { 125 assert.Nil(t, err) 126 }, 127 }, 128 "ws fon fou foc": { 129 pld: []interface{}{ 130 41238747, "fUST", 1575026670000, 1575026670000, 5000, 5000, "LIMIT", nil, 131 nil, 0, "ACTIVE", nil, nil, nil, 0.006000000000000001, 30, 0, 0, nil, 0, nil, 132 }, 133 expected: &fundingoffer.Offer{ 134 ID: 41238747, 135 Symbol: "fUST", 136 MTSCreated: 1575026670000, 137 MTSUpdated: 1575026670000, 138 Amount: 5000, 139 AmountOrig: 5000, 140 Type: "LIMIT", 141 Status: "ACTIVE", 142 Rate: 0.006000000000000001, 143 Period: 30, 144 Notify: false, 145 Hidden: false, 146 Insure: false, 147 Renew: false, 148 RateReal: 0, 149 }, 150 err: func(t *testing.T, err error) { 151 assert.Nil(t, err) 152 }, 153 }, 154 "ws fos item": { 155 pld: []interface{}{ 156 41237920, "fETH", 1573912039000, 1573912039000, 0.5, 0.5, "LIMIT", 157 nil, nil, 0, "ACTIVE", nil, nil, nil, 0.0024, 2, 0, 0, nil, 0, nil, 158 }, 159 expected: &fundingoffer.Offer{ 160 ID: 41237920, 161 Symbol: "fETH", 162 MTSCreated: 1573912039000, 163 MTSUpdated: 1573912039000, 164 Amount: 0.5, 165 AmountOrig: 0.5, 166 Type: "LIMIT", 167 Status: "ACTIVE", 168 Rate: 0.0024, 169 Period: 2, 170 Notify: false, 171 Hidden: false, 172 Insure: false, 173 Renew: false, 174 RateReal: 0, 175 }, 176 err: func(t *testing.T, err error) { 177 assert.Nil(t, err) 178 }, 179 }, 180 } 181 182 for k, v := range cases { 183 t.Run(k, func(t *testing.T) { 184 got, err := fundingoffer.FromRaw(v.pld) 185 v.err(t, err) 186 assert.Equal(t, v.expected, got) 187 }) 188 } 189 } 190 191 func TestSnapshotFromRaw(t *testing.T) { 192 cases := map[string]struct { 193 pld []interface{} 194 expected *fundingoffer.Snapshot 195 err func(*testing.T, error) 196 }{ 197 "invalid pld": { 198 pld: []interface{}{}, 199 expected: nil, 200 err: func(t *testing.T, err error) { 201 assert.NotNil(t, err) 202 }, 203 }, 204 "rest funding offer hist": { 205 pld: []interface{}{ 206 []interface{}{ 207 653170899, "fUSD", 1574072620000, 1574072620000, 0, -57.9, nil, nil, nil, nil, 208 "EXECUTED at 0.0368% (57.9)", nil, nil, nil, 0.000369, 2, 0, 0, nil, nil, nil, 209 }, 210 }, 211 expected: &fundingoffer.Snapshot{ 212 Snapshot: []*fundingoffer.Offer{ 213 { 214 ID: 653170899, 215 Symbol: "fUSD", 216 MTSCreated: 1574072620000, 217 MTSUpdated: 1574072620000, 218 Amount: 0, 219 AmountOrig: -57.9, 220 Status: "EXECUTED at 0.0368% (57.9)", 221 Rate: 0.000369, 222 Period: 2, 223 Notify: false, 224 Hidden: false, 225 Insure: false, 226 Renew: false, 227 RateReal: 0, 228 }, 229 }, 230 }, 231 err: func(t *testing.T, err error) { 232 assert.Nil(t, err) 233 }, 234 }, 235 "ws fos": { 236 pld: []interface{}{ 237 []interface{}{ 238 41237920, "fETH", 1573912039000, 1573912039000, 0.5, 0.5, "LIMIT", 239 nil, nil, 0, "ACTIVE", nil, nil, nil, 0.0024, 2, 0, 0, nil, 0, nil, 240 }, 241 }, 242 expected: &fundingoffer.Snapshot{ 243 Snapshot: []*fundingoffer.Offer{ 244 { 245 ID: 41237920, 246 Symbol: "fETH", 247 MTSCreated: 1573912039000, 248 MTSUpdated: 1573912039000, 249 Amount: 0.5, 250 AmountOrig: 0.5, 251 Type: "LIMIT", 252 Status: "ACTIVE", 253 Rate: 0.0024, 254 Period: 2, 255 Notify: false, 256 Hidden: false, 257 Insure: false, 258 Renew: false, 259 RateReal: 0, 260 }, 261 }, 262 }, 263 err: func(t *testing.T, err error) { 264 assert.Nil(t, err) 265 }, 266 }, 267 } 268 269 for k, v := range cases { 270 t.Run(k, func(t *testing.T) { 271 got, err := fundingoffer.SnapshotFromRaw(v.pld) 272 v.err(t, err) 273 assert.Equal(t, v.expected, got) 274 }) 275 } 276 } 277 278 func TestCancelFromRaw(t *testing.T) { 279 pld := []interface{}{ 280 652606505, "fETH", 1574000611000, 1574000611000, 0.29797676, 0.29797676, "LIMIT", 281 nil, nil, 0, "ACTIVE", nil, nil, nil, 0.0002, 2, 1, nil, nil, 0, nil, 282 } 283 284 expected := "fundingoffer.Cancel" 285 o, err := fundingoffer.CancelFromRaw(pld) 286 assert.Nil(t, err) 287 288 got := reflect.TypeOf(o).String() 289 assert.Equal(t, expected, got) 290 } 291 292 func TestNewFromRaw(t *testing.T) { 293 pld := []interface{}{ 294 652606505, "fETH", 1574000611000, 1574000611000, 0.29797676, 0.29797676, "LIMIT", 295 nil, nil, 0, "ACTIVE", nil, nil, nil, 0.0002, 2, 1, nil, nil, 0, nil, 296 } 297 298 expected := "fundingoffer.New" 299 o, err := fundingoffer.NewFromRaw(pld) 300 assert.Nil(t, err) 301 302 got := reflect.TypeOf(o).String() 303 assert.Equal(t, expected, got) 304 } 305 306 func TestUpdateFromRaw(t *testing.T) { 307 pld := []interface{}{ 308 652606505, "fETH", 1574000611000, 1574000611000, 0.29797676, 0.29797676, "LIMIT", 309 nil, nil, 0, "ACTIVE", nil, nil, nil, 0.0002, 2, 1, nil, nil, 0, nil, 310 } 311 312 expected := "fundingoffer.Update" 313 o, err := fundingoffer.UpdateFromRaw(pld) 314 assert.Nil(t, err) 315 316 got := reflect.TypeOf(o).String() 317 assert.Equal(t, expected, got) 318 }