From 4cb0b9199244a0cf1b9a6150d6a996cdf522131a Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Tue, 17 Dec 2024 22:04:37 -0800 Subject: [PATCH] Clean up. --- lib/typing/converters/string_converter.go | 2 +- .../converters/string_converter_test.go | 81 ++++++++++++++++++- 2 files changed, 79 insertions(+), 4 deletions(-) diff --git a/lib/typing/converters/string_converter.go b/lib/typing/converters/string_converter.go index 4bc21758..0f2f8cd0 100644 --- a/lib/typing/converters/string_converter.go +++ b/lib/typing/converters/string_converter.go @@ -145,7 +145,7 @@ func (DecimalConverter) Convert(value any) (string, error) { return Float32ToString(castedColVal), nil case float64: return Float64ToString(castedColVal), nil - case int64, int32: + case int, int8, int16, int32, int64: return fmt.Sprint(castedColVal), nil case string: return castedColVal, nil diff --git a/lib/typing/converters/string_converter_test.go b/lib/typing/converters/string_converter_test.go index 731b40a9..2bf7d078 100644 --- a/lib/typing/converters/string_converter_test.go +++ b/lib/typing/converters/string_converter_test.go @@ -3,6 +3,10 @@ package converters import ( "testing" + "github.com/artie-labs/transfer/lib/numbers" + + "github.com/artie-labs/transfer/lib/typing/decimal" + "github.com/artie-labs/transfer/lib/config/constants" "github.com/stretchr/testify/assert" ) @@ -23,10 +27,81 @@ func TestArrayConverter_Convert(t *testing.T) { } } +func TestFloatConverter_Convert(t *testing.T) { + { + // Unexpected type + _, err := FloatConverter{}.Convert("foo") + assert.ErrorContains(t, err, `unexpected value: 'foo', type: string`) + } + { + // Float32 + val, err := FloatConverter{}.Convert(float32(123.45)) + assert.NoError(t, err) + assert.Equal(t, "123.45", val) + } + { + // Float64 + val, err := FloatConverter{}.Convert(float64(123.45)) + assert.NoError(t, err) + assert.Equal(t, "123.45", val) + } + { + // Integers + for _, input := range []any{42, int8(42), int16(42), int32(42), int64(42), float32(42), float64(42)} { + val, err := FloatConverter{}.Convert(input) + assert.NoError(t, err) + assert.Equal(t, "42", val) + } + } +} + func TestIntegerConverter_Convert(t *testing.T) { - for _, val := range []any{42, int8(42), int16(42), int32(42), int64(42)} { - parsedVal, err := IntegerConverter{}.Convert(val) + { + // Various numbers + for _, val := range []any{42, int8(42), int16(42), int32(42), int64(42), float32(42), float64(42)} { + parsedVal, err := IntegerConverter{}.Convert(val) + assert.NoError(t, err) + assert.Equal(t, "42", parsedVal) + } + } + { + // Booleans + { + // True + val, err := IntegerConverter{}.Convert(true) + assert.NoError(t, err) + assert.Equal(t, "1", val) + } + { + // False + val, err := IntegerConverter{}.Convert(false) + assert.NoError(t, err) + assert.Equal(t, "0", val) + } + } +} + +func TestDecimalConverter_Convert(t *testing.T) { + { + // Extended decimal + val, err := DecimalConverter{}.Convert(decimal.NewDecimal(numbers.MustParseDecimal("123.45"))) assert.NoError(t, err) - assert.Equal(t, "42", parsedVal) + assert.Equal(t, "123.45", val) + } + { + // Floats + for _, input := range []any{float32(123.45), float64(123.45)} { + val, err := DecimalConverter{}.Convert(input) + assert.NoError(t, err) + assert.Equal(t, "123.45", val) + } + } + { + // Integers + for _, input := range []any{42, int8(42), int16(42), int32(42), int64(42), float32(42), float64(42)} { + val, err := DecimalConverter{}.Convert(input) + assert.NoError(t, err) + assert.Equal(t, "42", val) + } } }