Skip to content

Commit

Permalink
Clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Dec 18, 2024
1 parent de3270c commit 4cb0b91
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/typing/converters/string_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 78 additions & 3 deletions lib/typing/converters/string_converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
}
}
}

0 comments on commit 4cb0b91

Please sign in to comment.