Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(test): add unit test for function #726

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions pkg/runtime/function/exp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
49 changes: 49 additions & 0 deletions pkg/runtime/function/first_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
})
}
}
20 changes: 20 additions & 0 deletions pkg/runtime/function/floor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
55 changes: 55 additions & 0 deletions pkg/runtime/function/format_bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
}
}
Loading