github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/fundingcredit/fundingcredit_test.go (about) 1 package fundingcredit_test 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/fundingcredit" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestFromRaw(t *testing.T) { 12 cases := map[string]struct { 13 pld []interface{} 14 expected *fundingcredit.Credit 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 funding credits item": { 25 pld: []interface{}{ 26 26222883, "fUST", 1, 1574013661000, 1574079687000, 350, nil, "ACTIVE", "FIXED", nil, 27 nil, 0.0024, 2, 1574013661000, 1574078487000, 0, nil, nil, 0, nil, 0, "tBTCUST", 28 }, 29 expected: &fundingcredit.Credit{ 30 ID: 26222883, 31 Symbol: "fUST", 32 Side: 1, 33 MTSCreated: 1574013661000, 34 MTSUpdated: 1574079687000, 35 Amount: 350, 36 Status: "ACTIVE", 37 RateType: "FIXED", 38 Rate: 0.0024, 39 Period: 2, 40 MTSOpened: 1574013661000, 41 MTSLastPayout: 1574078487000, 42 Notify: false, 43 Hidden: false, 44 Insure: false, 45 Renew: false, 46 RateReal: 0, 47 NoClose: false, 48 PositionPair: "tBTCUST", 49 }, 50 err: func(t *testing.T, err error) { 51 assert.Nil(t, err) 52 }, 53 }, 54 "rest funding credits history item": { 55 pld: []interface{}{ 56 171988300, "fUSD", 1, 1574230085000, 1574402835000, 50.70511182, nil, 57 "CLOSED (expired)", "FIXED", nil, nil, 0.00024799, 2, 1574230085000, 58 1574403364000, nil, 0, nil, 0, nil, 0, "tEOSUSD", 59 }, 60 expected: &fundingcredit.Credit{ 61 ID: 171988300, 62 Symbol: "fUSD", 63 Side: 1, 64 MTSCreated: 1574230085000, 65 MTSUpdated: 1574402835000, 66 Amount: 50.70511182, 67 Status: "CLOSED (expired)", 68 RateType: "FIXED", 69 Rate: 0.00024799, 70 Period: 2, 71 MTSOpened: 1574230085000, 72 MTSLastPayout: 1574403364000, 73 Notify: false, 74 Hidden: false, 75 Insure: false, 76 Renew: false, 77 RateReal: 0, 78 NoClose: false, 79 PositionPair: "tEOSUSD", 80 }, 81 err: func(t *testing.T, err error) { 82 assert.Nil(t, err) 83 }, 84 }, 85 "ws fcs item": { 86 pld: []interface{}{ 87 26223578, "fUST", 1, 1575052261000, 1575296187000, 350, 0, "ACTIVE", nil, nil, 88 nil, 0, 30, 1575052261000, 1575293487000, 0, 0, nil, 0, nil, 0, "tBTCUST", 89 }, 90 expected: &fundingcredit.Credit{ 91 ID: 26223578, 92 Symbol: "fUST", 93 Side: 1, 94 MTSCreated: 1575052261000, 95 MTSUpdated: 1575296187000, 96 Amount: 350, 97 Status: "ACTIVE", 98 Rate: 0, 99 Period: 30, 100 MTSOpened: 1575052261000, 101 MTSLastPayout: 1575293487000, 102 Notify: false, 103 Hidden: false, 104 Insure: false, 105 Renew: false, 106 RateReal: 0, 107 NoClose: false, 108 PositionPair: "tBTCUST", 109 }, 110 err: func(t *testing.T, err error) { 111 assert.Nil(t, err) 112 }, 113 }, 114 } 115 116 for k, v := range cases { 117 t.Run(k, func(t *testing.T) { 118 got, err := fundingcredit.FromRaw(v.pld) 119 v.err(t, err) 120 assert.Equal(t, v.expected, got) 121 }) 122 } 123 } 124 125 func TestSnapshotFromRaw(t *testing.T) { 126 cases := map[string]struct { 127 pld []interface{} 128 expected *fundingcredit.Snapshot 129 err func(*testing.T, error) 130 }{ 131 "invalid pld": { 132 pld: []interface{}{}, 133 expected: nil, 134 err: func(t *testing.T, err error) { 135 assert.NotNil(t, err) 136 }, 137 }, 138 "rest funding credits": { 139 pld: []interface{}{ 140 []interface{}{ 141 26222883, "fUST", 1, 1574013661000, 1574079687000, 350, nil, "ACTIVE", "FIXED", nil, 142 nil, 0.0024, 2, 1574013661000, 1574078487000, 0, nil, nil, 0, nil, 0, "tBTCUST", 143 }, 144 }, 145 expected: &fundingcredit.Snapshot{ 146 Snapshot: []*fundingcredit.Credit{ 147 { 148 ID: 26222883, 149 Symbol: "fUST", 150 Side: 1, 151 MTSCreated: 1574013661000, 152 MTSUpdated: 1574079687000, 153 Amount: 350, 154 Status: "ACTIVE", 155 RateType: "FIXED", 156 Rate: 0.0024, 157 Period: 2, 158 MTSOpened: 1574013661000, 159 MTSLastPayout: 1574078487000, 160 Notify: false, 161 Hidden: false, 162 Insure: false, 163 Renew: false, 164 RateReal: 0, 165 NoClose: false, 166 PositionPair: "tBTCUST", 167 }, 168 }, 169 }, 170 err: func(t *testing.T, err error) { 171 assert.Nil(t, err) 172 }, 173 }, 174 "rest funding credits history": { 175 pld: []interface{}{ 176 []interface{}{ 177 171988300, "fUSD", 1, 1574230085000, 1574402835000, 50.70511182, nil, 178 "CLOSED (expired)", "FIXED", nil, nil, 0.00024799, 2, 1574230085000, 179 1574403364000, nil, 0, nil, 0, nil, 0, "tEOSUSD", 180 }, 181 }, 182 expected: &fundingcredit.Snapshot{ 183 Snapshot: []*fundingcredit.Credit{ 184 { 185 ID: 171988300, 186 Symbol: "fUSD", 187 Side: 1, 188 MTSCreated: 1574230085000, 189 MTSUpdated: 1574402835000, 190 Amount: 50.70511182, 191 Status: "CLOSED (expired)", 192 RateType: "FIXED", 193 Rate: 0.00024799, 194 Period: 2, 195 MTSOpened: 1574230085000, 196 MTSLastPayout: 1574403364000, 197 Notify: false, 198 Hidden: false, 199 Insure: false, 200 Renew: false, 201 RateReal: 0, 202 NoClose: false, 203 PositionPair: "tEOSUSD", 204 }, 205 }, 206 }, 207 err: func(t *testing.T, err error) { 208 assert.Nil(t, err) 209 }, 210 }, 211 "ws fcs": { 212 pld: []interface{}{ 213 []interface{}{ 214 26223578, "fUST", 1, 1575052261000, 1575296187000, 350, 0, "ACTIVE", nil, nil, 215 nil, 0, 30, 1575052261000, 1575293487000, 0, 0, nil, 0, nil, 0, "tBTCUST", 216 }, 217 }, 218 expected: &fundingcredit.Snapshot{ 219 Snapshot: []*fundingcredit.Credit{ 220 { 221 ID: 26223578, 222 Symbol: "fUST", 223 Side: 1, 224 MTSCreated: 1575052261000, 225 MTSUpdated: 1575296187000, 226 Amount: 350, 227 Status: "ACTIVE", 228 Rate: 0, 229 Period: 30, 230 MTSOpened: 1575052261000, 231 MTSLastPayout: 1575293487000, 232 Notify: false, 233 Hidden: false, 234 Insure: false, 235 Renew: false, 236 RateReal: 0, 237 NoClose: false, 238 PositionPair: "tBTCUST", 239 }, 240 }, 241 }, 242 err: func(t *testing.T, err error) { 243 assert.Nil(t, err) 244 }, 245 }, 246 } 247 248 for k, v := range cases { 249 t.Run(k, func(t *testing.T) { 250 got, err := fundingcredit.SnapshotFromRaw(v.pld) 251 v.err(t, err) 252 assert.Equal(t, v.expected, got) 253 }) 254 } 255 } 256 257 func TestNewFromRaw(t *testing.T) { 258 pld := []interface{}{ 259 26222883, "fUST", 1, 1574013661000, 1574079687000, 350, nil, "ACTIVE", nil, nil, 260 nil, 0.0024, 2, 1574013661000, 1574078487000, 1, nil, nil, 0, nil, 1, "tBTCUST", 261 } 262 263 expected := "fundingcredit.New" 264 o, err := fundingcredit.NewFromRaw(pld) 265 assert.Nil(t, err) 266 267 got := reflect.TypeOf(o).String() 268 assert.Equal(t, expected, got) 269 } 270 271 func TestUpdateFromRaw(t *testing.T) { 272 pld := []interface{}{ 273 26222883, "fUST", 1, 1574013661000, 1574079687000, 350, nil, "ACTIVE", nil, nil, 274 nil, 0.0024, 2, 1574013661000, 1574078487000, 1, nil, nil, 0, nil, 1, "tBTCUST", 275 } 276 277 expected := "fundingcredit.Update" 278 o, err := fundingcredit.UpdateFromRaw(pld) 279 assert.Nil(t, err) 280 281 got := reflect.TypeOf(o).String() 282 assert.Equal(t, expected, got) 283 } 284 285 func TestCancelFromRaw(t *testing.T) { 286 pld := []interface{}{ 287 26222883, "fUST", 1, 1574013661000, 1574079687000, 350, nil, "ACTIVE", nil, nil, 288 nil, 0.0024, 2, 1574013661000, 1574078487000, 1, nil, nil, 0, nil, 1, "tBTCUST", 289 } 290 291 expected := "fundingcredit.Cancel" 292 o, err := fundingcredit.CancelFromRaw(pld) 293 assert.Nil(t, err) 294 295 got := reflect.TypeOf(o).String() 296 assert.Equal(t, expected, got) 297 }