Skip to content

Commit

Permalink
Rewrite tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Sep 20, 2024
1 parent 175e027 commit ec5e61c
Showing 1 changed file with 62 additions and 75 deletions.
137 changes: 62 additions & 75 deletions lib/parquetutil/parse_values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,84 +13,71 @@ import (
)

func TestParseValue(t *testing.T) {
eDecimal := typing.NewDecimalDetailsFromTemplate(typing.EDecimal, decimal.NewDetails(30, 5))

eTime := typing.ETime
eTime.ExtendedTimeDetails = &ext.Time

eDate := typing.ETime
eDate.ExtendedTimeDetails = &ext.Date

eDateTime := typing.ETime
eDateTime.ExtendedTimeDetails = &ext.TimestampTz

testCases := []struct {
name string
colVal any
colKind columns.Column

expectedValue any
}{
{
name: "nil value",
colVal: nil,
expectedValue: nil,
},
{
name: "string value",
colVal: "test",
colKind: columns.NewColumn("", typing.String),
expectedValue: "test",
},
{
name: "struct value",
colVal: map[string]any{
"foo": "bar",
},
colKind: columns.NewColumn("", typing.Struct),
expectedValue: `{"foo":"bar"}`,
},
{
name: "array (numbers - converted to string)",
colVal: []any{123, 456},
colKind: columns.NewColumn("", typing.Array),
expectedValue: []string{"123", "456"},
},
{
name: "array (boolean - converted to string)",
colVal: []any{true, false, true},
colKind: columns.NewColumn("", typing.Array),
expectedValue: []string{"true", "false", "true"},
},
{
name: "decimal",
colVal: decimal.NewDecimalWithPrecision(numbers.MustParseDecimal("5000.22320"), 30),
colKind: columns.NewColumn("", eDecimal),
expectedValue: "5000.22320",
},
{
name: "time",
colVal: "03:15:00",
colKind: columns.NewColumn("", eTime),
expectedValue: "03:15:00+00",
},
{
// Nil
value, err := ParseValue(nil, columns.Column{})
assert.NoError(t, err)
assert.Nil(t, value)
}
{
// String
value, err := ParseValue("test", columns.NewColumn("", typing.String))
assert.NoError(t, err)
assert.Equal(t, "test", value)
}
{
// Struct
value, err := ParseValue(map[string]any{"foo": "bar"}, columns.NewColumn("", typing.Struct))
assert.NoError(t, err)
assert.Equal(t, `{"foo":"bar"}`, value)
}
{
// Arrays
{
name: "date",
colVal: "2022-12-25",
colKind: columns.NewColumn("", eDate),
expectedValue: "2022-12-25",
},
// Arrays (numbers - converted to string)
value, err := ParseValue([]any{123, 456}, columns.NewColumn("", typing.Array))
assert.NoError(t, err)
assert.Equal(t, []string{"123", "456"}, value)
}
{
name: "datetime",
colVal: "2023-04-24T17:29:05.69944Z",
colKind: columns.NewColumn("", eDateTime),
expectedValue: int64(1682357345699),
},
// Arrays (booleans - converted to string)
value, err := ParseValue([]any{false, true, false}, columns.NewColumn("", typing.Array))
assert.NoError(t, err)
assert.Equal(t, []string{"false", "true", "false"}, value)
}
}
{
// Decimal
value, err := ParseValue(decimal.NewDecimalWithPrecision(
numbers.MustParseDecimal("5000.22320"), 30),
columns.NewColumn("", typing.NewDecimalDetailsFromTemplate(typing.EDecimal, decimal.NewDetails(30, 5))),
)

for _, tc := range testCases {
actualValue, actualErr := ParseValue(tc.colVal, tc.colKind)
assert.NoError(t, actualErr, tc.name)
assert.Equal(t, tc.expectedValue, actualValue, tc.name)
assert.NoError(t, err)
assert.Equal(t, "5000.22320", value)
}
{
// Time
eTime := typing.ETime
eTime.ExtendedTimeDetails = &ext.Time
value, err := ParseValue("03:15:00", columns.NewColumn("", eTime))
assert.NoError(t, err)
assert.Equal(t, "03:15:00+00", value)
}
{
// Date
eDate := typing.ETime
eDate.ExtendedTimeDetails = &ext.Date
value, err := ParseValue("2022-12-25", columns.NewColumn("", eDate))
assert.NoError(t, err)
assert.Equal(t, "2022-12-25", value)
}
{
// Timestamp TZ
eDateTime := typing.ETime
eDateTime.ExtendedTimeDetails = &ext.TimestampTz
value, err := ParseValue("2023-04-24T17:29:05.69944Z", columns.NewColumn("", eDateTime))
assert.NoError(t, err)
assert.Equal(t, int64(1682357345699), value)
}
}

0 comments on commit ec5e61c

Please sign in to comment.