From 33f2907827d0e66b3cbe0ba310bea6057b770e27 Mon Sep 17 00:00:00 2001 From: Zonglei Dong Date: Wed, 2 Aug 2023 16:26:34 +0800 Subject: [PATCH] feat(test): add unit test for function (#726) --- pkg/runtime/function/exp_test.go | 21 +++++++++ pkg/runtime/function/first_value_test.go | 49 ++++++++++++++++++++ pkg/runtime/function/floor_test.go | 20 +++++++++ pkg/runtime/function/format_bytes_test.go | 55 +++++++++++++++++++++++ 4 files changed, 145 insertions(+) diff --git a/pkg/runtime/function/exp_test.go b/pkg/runtime/function/exp_test.go index 7fd42976..27a10d66 100644 --- a/pkg/runtime/function/exp_test.go +++ b/pkg/runtime/function/exp_test.go @@ -59,3 +59,24 @@ func TestExp(t *testing.T) { }) } } + +func TestExp_Error(t *testing.T) { + fn := proto.MustGetFunc(FuncExp) + assert.Equal(t, 1, fn.NumInput()) + + type tt struct { + desc string + in proto.Value + out float64 + } + + for _, it := range []tt{ + {"EXP(Nil)", nil, 1}, + } { + t.Run(it.desc, func(t *testing.T) { + out, err := fn.Apply(context.Background(), proto.ToValuer(it.in)) + assert.Nil(t, err) + assert.Nil(t, out) + }) + } +} diff --git a/pkg/runtime/function/first_value_test.go b/pkg/runtime/function/first_value_test.go index 41852c7d..728e10d1 100644 --- a/pkg/runtime/function/first_value_test.go +++ b/pkg/runtime/function/first_value_test.go @@ -33,6 +33,8 @@ import ( func TestFuncFirstValue(t *testing.T) { fn := proto.MustGetFunc(FuncFirstValue) + assert.Equal(t, 0, fn.NumInput()) + type tt struct { inputs [][]proto.Value want string @@ -182,6 +184,19 @@ func TestFuncFirstValue(t *testing.T) { }, "0", }, + { + [][]proto.Value{ + {proto.NewValueString("08:00:00"), proto.NewValueString("xh458")}, + }, + "", + }, + { + [][]proto.Value{ + // order column, partition column, value column + {proto.NewValueString("07:00:00"), proto.NewValueString("st113"), proto.NewValueFloat64(10)}, + }, + "10", + }, } { t.Run(v.want, func(t *testing.T) { var inputs []proto.Valuer @@ -196,3 +211,37 @@ func TestFuncFirstValue(t *testing.T) { }) } } + +func TestFuncFirstValue_Error(t *testing.T) { + fn := proto.MustGetFunc(FuncFirstValue) + assert.Equal(t, 0, fn.NumInput()) + + type tt struct { + inputs []proto.Value + want string + } + for _, v := range []tt{ + { + []proto.Value{ + proto.NewValueString("07:00:00"), nil, proto.NewValueFloat64(10), + }, + "", + }, + { + []proto.Value{ + proto.NewValueString("07:00:00"), proto.NewValueString("st113"), nil, + }, + "", + }, + } { + t.Run(v.want, func(t *testing.T) { + var inputs []proto.Valuer + for i := range v.inputs { + inputs = append(inputs, proto.ToValuer(v.inputs[i])) + } + out, err := fn.Apply(context.Background(), inputs...) + assert.NoError(t, err) + assert.Nil(t, out) + }) + } +} diff --git a/pkg/runtime/function/floor_test.go b/pkg/runtime/function/floor_test.go index eedbd61b..2170467c 100644 --- a/pkg/runtime/function/floor_test.go +++ b/pkg/runtime/function/floor_test.go @@ -58,3 +58,23 @@ func TestFloor(t *testing.T) { }) } } + +func TestFloor_Error(t *testing.T) { + fn := proto.MustGetFunc(FuncFloor) + assert.Equal(t, 1, fn.NumInput()) + + type tt struct { + in proto.Value + out string + } + + for _, it := range []tt{ + {nil, ""}, + } { + t.Run(it.out, func(t *testing.T) { + out, err := fn.Apply(context.Background(), proto.ToValuer(it.in)) + assert.NoError(t, err) + assert.Nil(t, out) + }) + } +} diff --git a/pkg/runtime/function/format_bytes_test.go b/pkg/runtime/function/format_bytes_test.go index 032e2db8..1a889b10 100644 --- a/pkg/runtime/function/format_bytes_test.go +++ b/pkg/runtime/function/format_bytes_test.go @@ -60,3 +60,58 @@ func TestFormatBytes(t *testing.T) { }) } } + +func TestFormatBytes_Nil(t *testing.T) { + fn := proto.MustGetFunc(FuncFormatBytes) + assert.Equal(t, 1, fn.NumInput()) + + out, err := fn.Apply(context.TODO(), proto.ToValuer(nil)) + assert.NoError(t, err) + assert.Nil(t, out) + + type tt struct { + name string + input interface{} + output string + } + + for _, next := range []tt{ + {"Test_Nil", nil, "512 bytes"}, + } { + t.Run(fmt.Sprint(next.name), func(t *testing.T) { + val, err := proto.NewValue(next.input) + assert.NoError(t, err) + actual, err := fn.Apply(context.TODO(), proto.ToValuer(val)) + assert.NoError(t, err) + assert.Nil(t, actual) + }) + } +} + +func TestFormatBytes_Error(t *testing.T) { + fn := proto.MustGetFunc(FuncFormatBytes) + assert.Equal(t, 1, fn.NumInput()) + + out, err := fn.Apply(context.TODO(), proto.ToValuer(nil)) + assert.NoError(t, err) + assert.Nil(t, out) + + type tt struct { + name string + input interface{} + output string + } + + for _, next := range []tt{ + {"Test_Error", "a", "0 bytes"}, + {"Test_Error", "0", "0 bytes"}, + } { + t.Run(fmt.Sprint(next.name), func(t *testing.T) { + val, err := proto.NewValue(next.input) + assert.NoError(t, err) + actual, err := fn.Apply(context.TODO(), proto.ToValuer(val)) + assert.NoError(t, err) + assert.Equal(t, next.output, actual.String()) + }) + } +}