github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/convert/convert_test.go (about) 1 package convert_test 2 3 import ( 4 "testing" 5 6 "github.com/bitfinexcom/bitfinex-api-go/pkg/convert" 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestItfToStrSlice(t *testing.T) { 12 t.Run("invalid slice arguments", func(t *testing.T) { 13 payload := []interface{}{123, 234, 345} 14 got, err := convert.ItfToStrSlice(payload) 15 require.NotNil(t, err) 16 require.Nil(t, got) 17 }) 18 19 t.Run("non slice arguments", func(t *testing.T) { 20 payload := "123" 21 got, err := convert.ItfToStrSlice(payload) 22 require.Nil(t, err) 23 assert.Equal(t, []string{}, got) 24 }) 25 26 t.Run("valid arguments", func(t *testing.T) { 27 payload := []interface{}{"foo", "bar", "baz"} 28 got, err := convert.ItfToStrSlice(payload) 29 expected := []string{"foo", "bar", "baz"} 30 require.Nil(t, err) 31 assert.Equal(t, expected, got) 32 }) 33 } 34 35 func TestToInt(t *testing.T) { 36 t.Run("valid int argument", func(t *testing.T) { 37 payload := 1234 38 expected := 1234 39 got := convert.ToInt(payload) 40 assert.Equal(t, expected, got) 41 }) 42 43 t.Run("valid string int", func(t *testing.T) { 44 payload := "1" 45 expected := 1 46 got := convert.ToInt(payload) 47 assert.Equal(t, expected, got) 48 }) 49 50 t.Run("float64", func(t *testing.T) { 51 var payload float64 = 1234 52 expected := 1234 53 got := convert.ToInt(payload) 54 assert.Equal(t, expected, got) 55 }) 56 57 t.Run("invalid string int", func(t *testing.T) { 58 payload := "foo" 59 expected := 0 60 got := convert.ToInt(payload) 61 assert.Equal(t, expected, got) 62 }) 63 } 64 65 func TestToInterface(t *testing.T) { 66 payload := []float64{1.1234, 2.1234} 67 expected := []interface{}{1.1234, 2.1234} 68 got := convert.ToInterface(payload) 69 assert.Equal(t, expected, got) 70 } 71 72 func TestF64ValOrZero(t *testing.T) { 73 t.Run("converts int to float64", func(t *testing.T) { 74 var expected float64 = 910 75 got := convert.F64ValOrZero(910) 76 assert.Equal(t, expected, got) 77 }) 78 79 t.Run("converts float64 to float64", func(t *testing.T) { 80 var expected float64 = 910.1234 81 got := convert.F64ValOrZero(float64(910.1234)) 82 assert.Equal(t, expected, got) 83 }) 84 } 85 86 func TestI64ValOrZero(t *testing.T) { 87 t.Run("converts int to int64", func(t *testing.T) { 88 var expected int64 = 910 89 got := convert.I64ValOrZero(910) 90 assert.Equal(t, expected, got) 91 }) 92 93 t.Run("converts float64 to int64", func(t *testing.T) { 94 var expected int64 = 1594891800000 95 got := convert.I64ValOrZero(1.5948918e+12) 96 assert.Equal(t, expected, got) 97 }) 98 } 99 100 func TestBValOrFalse(t *testing.T) { 101 t.Run("converts to truthy bool", func(t *testing.T) { 102 expected := true 103 got := convert.BValOrFalse(true) 104 assert.Equal(t, expected, got) 105 }) 106 107 t.Run("converts to falsy bool", func(t *testing.T) { 108 expected := false 109 got := convert.BValOrFalse(false) 110 assert.Equal(t, expected, got) 111 }) 112 113 t.Run("converts 1 to truthy bool", func(t *testing.T) { 114 expected := true 115 got := convert.BValOrFalse(1) 116 assert.Equal(t, expected, got) 117 }) 118 119 t.Run("converts \"1\" to truthy bool", func(t *testing.T) { 120 expected := true 121 got := convert.BValOrFalse("1") 122 assert.Equal(t, expected, got) 123 }) 124 125 t.Run("converts 0 to falsy bool", func(t *testing.T) { 126 expected := false 127 got := convert.BValOrFalse(0) 128 assert.Equal(t, expected, got) 129 }) 130 131 t.Run("converts \"0\" to falsy bool", func(t *testing.T) { 132 expected := false 133 got := convert.BValOrFalse("0") 134 assert.Equal(t, expected, got) 135 }) 136 }