github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/trades/fundingtrades_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 TestFundingTradeFromRaw(t *testing.T) { 11 cases := map[string]struct { 12 pld []interface{} 13 expected trades.FundingTrade 14 err func(*testing.T, error) 15 }{ 16 "invalid payload": { 17 pld: []interface{}{401597393}, 18 expected: trades.FundingTrade{}, 19 err: func(t *testing.T, err error) { 20 assert.Error(t, err) 21 }, 22 }, 23 "valid payload": { 24 pld: []interface{}{133323072, 1574694245478, -258.7458086, 0.0002587, 2}, 25 expected: trades.FundingTrade{ 26 Symbol: "fUSD", 27 ID: 133323072, 28 MTS: 1574694245478, 29 Amount: -258.7458086, 30 Rate: 0.0002587, 31 Period: 2, 32 }, 33 err: func(t *testing.T, err error) { 34 assert.NoError(t, err) 35 }, 36 }, 37 } 38 39 for k, v := range cases { 40 t.Run(k, func(t *testing.T) { 41 got, err := trades.FTFromRaw("fUSD", v.pld) 42 v.err(t, err) 43 assert.Equal(t, v.expected, got) 44 }) 45 } 46 } 47 48 func TestFundingTradeExecutionFromRaw(t *testing.T) { 49 cases := map[string]struct { 50 pld []interface{} 51 expected trades.FundingTradeExecuted 52 err func(*testing.T, error) 53 }{ 54 "invalid payload": { 55 pld: []interface{}{401597393}, 56 expected: trades.FundingTradeExecuted{}, 57 err: func(t *testing.T, err error) { 58 assert.Error(t, err) 59 }, 60 }, 61 "valid payload": { 62 pld: []interface{}{133323543, 1574694605000, -59.84, 0.00023647, 2}, 63 expected: trades.FundingTradeExecuted{ 64 Symbol: "fUSD", 65 ID: 133323543, 66 MTS: 1574694605000, 67 Amount: -59.84, 68 Rate: 0.00023647, 69 Period: 2, 70 }, 71 err: func(t *testing.T, err error) { 72 assert.NoError(t, err) 73 }, 74 }, 75 } 76 77 for k, v := range cases { 78 t.Run(k, func(t *testing.T) { 79 got, err := trades.FTEFromRaw("fUSD", v.pld) 80 v.err(t, err) 81 assert.Equal(t, v.expected, got) 82 }) 83 } 84 } 85 86 func TestFundingTradeExecutionUpdateFromRaw(t *testing.T) { 87 cases := map[string]struct { 88 pld []interface{} 89 expected trades.FundingTradeExecutionUpdate 90 err func(*testing.T, error) 91 }{ 92 "invalid payload": { 93 pld: []interface{}{401597393}, 94 expected: trades.FundingTradeExecutionUpdate{}, 95 err: func(t *testing.T, err error) { 96 assert.Error(t, err) 97 }, 98 }, 99 "valid payload": { 100 pld: []interface{}{133323543, 1574694605000, -59.84, 0.00023647, 2}, 101 expected: trades.FundingTradeExecutionUpdate{ 102 Symbol: "fUSD", 103 ID: 133323543, 104 MTS: 1574694605000, 105 Amount: -59.84, 106 Rate: 0.00023647, 107 Period: 2, 108 }, 109 err: func(t *testing.T, err error) { 110 assert.NoError(t, err) 111 }, 112 }, 113 } 114 115 for k, v := range cases { 116 t.Run(k, func(t *testing.T) { 117 got, err := trades.FTEUFromRaw("fUSD", v.pld) 118 v.err(t, err) 119 assert.Equal(t, v.expected, got) 120 }) 121 } 122 } 123 124 func TestFundingTradeSnapshotFromRaw(t *testing.T) { 125 cases := map[string]struct { 126 pld [][]interface{} 127 expected trades.FundingTradeSnapshot 128 err func(*testing.T, error) 129 }{ 130 "invalid payload": { 131 pld: [][]interface{}{}, 132 expected: trades.FundingTradeSnapshot{}, 133 err: func(t *testing.T, err error) { 134 assert.Error(t, err) 135 }, 136 }, 137 "valid payload": { 138 pld: [][]interface{}{ 139 {133323543, 1574694605000, -59.84, 0.00023647, 2}, 140 }, 141 expected: trades.FundingTradeSnapshot{ 142 Snapshot: []trades.FundingTrade{ 143 { 144 Symbol: "fUSD", 145 ID: 133323543, 146 MTS: 1574694605000, 147 Amount: -59.84, 148 Rate: 0.00023647, 149 Period: 2, 150 }, 151 }, 152 }, 153 err: func(t *testing.T, err error) { 154 assert.NoError(t, err) 155 }, 156 }, 157 } 158 159 for k, v := range cases { 160 t.Run(k, func(t *testing.T) { 161 got, err := trades.FTSnapshotFromRaw("fUSD", v.pld) 162 v.err(t, err) 163 assert.Equal(t, v.expected, got) 164 }) 165 } 166 }