github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/derivatives/derivatives_test.go (about) 1 package derivatives_test 2 3 import ( 4 "testing" 5 6 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/derivatives" 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestNewDerivativeStatusFromWsRaw(t *testing.T) { 12 t.Run("insufficient arguments", func(t *testing.T) { 13 payload := []interface{}{1591614631576} 14 15 d, err := derivatives.FromWsRaw("tBTCF0:USTF0", payload) 16 require.NotNil(t, err) 17 require.Nil(t, d) 18 }) 19 20 t.Run("valid arguments", func(t *testing.T) { 21 payload := []interface{}{ 22 1591614631576, 23 nil, 24 9271.1234567, 25 9275.3, 26 nil, 27 1391472.27686063, 28 nil, 29 1594656000000, 30 -0.00011968, 31 3144, 32 nil, 33 0, 34 nil, 35 nil, 36 9276.06, 37 nil, 38 nil, 39 3813.72957182, 40 } 41 42 d, err := derivatives.FromWsRaw("tBTCF0:USTF0", payload) 43 require.Nil(t, err) 44 45 expected := &derivatives.DerivativeStatus{ 46 Symbol: "tBTCF0:USTF0", 47 MTS: 1591614631576, 48 Price: 9271.1234567, 49 SpotPrice: 9275.3, 50 InsuranceFundBalance: 1.39147227686063e+06, 51 FundingAccrued: -0.00011968, 52 FundingStep: 3144, 53 FundingEventMTS: 1594656000000, 54 MarkPrice: 9276.06, 55 OpenInterest: 3813.72957182, 56 } 57 assert.Equal(t, expected, d) 58 }) 59 } 60 61 func TestNewDerivativeStatusFromRaw(t *testing.T) { 62 t.Run("insufficient arguments", func(t *testing.T) { 63 payload := []interface{}{"tBTCF0:USTF0"} 64 65 d, err := derivatives.FromRaw(payload) 66 require.NotNil(t, err) 67 require.Nil(t, d) 68 }) 69 70 t.Run("valid arguments", func(t *testing.T) { 71 payload := []interface{}{ 72 "tBTCF0:USTF0", 73 1591614631576, 74 nil, 75 9271.1234567, 76 9275.3, 77 nil, 78 1391472.27686063, 79 nil, 80 1594656000000, 81 -0.00011968, 82 3144, 83 nil, 84 0, 85 nil, 86 nil, 87 9276.06, 88 nil, 89 nil, 90 3813.72957182, 91 } 92 93 d, err := derivatives.FromRaw(payload) 94 require.Nil(t, err) 95 96 expected := &derivatives.DerivativeStatus{ 97 Symbol: "tBTCF0:USTF0", 98 MTS: 1591614631576, 99 Price: 9271.1234567, 100 SpotPrice: 9275.3, 101 InsuranceFundBalance: 1.39147227686063e+06, 102 FundingAccrued: -0.00011968, 103 FundingEventMTS: 1594656000000, 104 FundingStep: 3144, 105 MarkPrice: 9276.06, 106 OpenInterest: 3813.72957182, 107 } 108 assert.Equal(t, expected, d) 109 }) 110 } 111 112 func TestSnapshotFromRaw(t *testing.T) { 113 t.Run("invalid arguments", func(t *testing.T) { 114 payload := [][]interface{}{{"tBTCF0:USTF0"}} 115 ss, err := derivatives.SnapshotFromRaw(payload) 116 require.NotNil(t, err) 117 require.Nil(t, ss) 118 }) 119 120 t.Run("valid arguments", func(t *testing.T) { 121 payload := [][]interface{}{ 122 { 123 "tBTCF0:USTF0", 124 1591614631576, 125 nil, 126 9271.1234567, 127 9275.3, 128 nil, 129 1391472.27686063, 130 nil, 131 1594656000000, 132 -0.00011968, 133 3144, 134 nil, 135 0, 136 nil, 137 nil, 138 9276.06, 139 nil, 140 nil, 141 3813.72957182, 142 }, 143 { 144 "tBTCF0:USTF0", 145 1591614631576, 146 nil, 147 9271.1234567, 148 9275.3, 149 nil, 150 1391472.27686063, 151 nil, 152 1594656000000, 153 -0.00011968, 154 3200, 155 nil, 156 0, 157 nil, 158 nil, 159 9276.06, 160 nil, 161 nil, 162 3813.72957182, 163 }, 164 } 165 ss, err := derivatives.SnapshotFromRaw(payload) 166 require.Nil(t, err) 167 168 expected := &derivatives.Snapshot{ 169 Snapshot: []*derivatives.DerivativeStatus{ 170 { 171 Symbol: "tBTCF0:USTF0", 172 MTS: 1591614631576, 173 Price: 9271.1234567, 174 SpotPrice: 9275.3, 175 InsuranceFundBalance: 1.39147227686063e+06, 176 FundingAccrued: -0.00011968, 177 FundingStep: 3144, 178 FundingEventMTS: 1594656000000, 179 MarkPrice: 9276.06, 180 OpenInterest: 3813.72957182, 181 }, 182 { 183 Symbol: "tBTCF0:USTF0", 184 MTS: 1591614631576, 185 Price: 9271.1234567, 186 SpotPrice: 9275.3, 187 InsuranceFundBalance: 1.39147227686063e+06, 188 FundingAccrued: -0.00011968, 189 FundingStep: 3200, 190 FundingEventMTS: 1594656000000, 191 MarkPrice: 9276.06, 192 OpenInterest: 3813.72957182, 193 }, 194 }, 195 } 196 197 assert.Equal(t, expected, ss) 198 }) 199 }