Skip to content

Commit

Permalink
feat(test): add unit test for cast function (#723)
Browse files Browse the repository at this point in the history
* feat(test): add unit test for cast function

* feat(test): add unit test for cast function

* feat(test): add unit test for cast time function

* feat(test): add unit test for ceil function

* feat(test): add unit test for ceil function

* feat(test): add unit test for char length function

* feat(test): add unit test for concat
  function
  • Loading branch information
dongzl authored Jul 27, 2023
1 parent 17b2133 commit a6636b7
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 9 deletions.
68 changes: 68 additions & 0 deletions pkg/runtime/function/cast_decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,71 @@ func TestCastDecimal(t *testing.T) {
})
}
}

func TestCastDecimal_Inputs_length_Error(t *testing.T) {
fn := proto.MustGetFunc(FuncCastDecimal)
assert.Equal(t, 3, fn.NumInput())

type tt struct {
inFirst proto.Value
inSecond proto.Value
intThird proto.Value
out string
}

for _, it := range []tt{
{proto.NewValueInt64(15), proto.NewValueInt64(4), proto.NewValueInt64(2), "15.00"},
} {
t.Run(it.out, func(t *testing.T) {
out, err := fn.Apply(context.Background(), proto.ToValuer(it.inFirst), proto.ToValuer(it.inSecond))
assert.Error(t, err)
assert.Nil(t, out)
})
}
}

func TestCastDecimal_Inputs_0_Error(t *testing.T) {
fn := proto.MustGetFunc(FuncCastDecimal)
assert.Equal(t, 3, fn.NumInput())

type tt struct {
inFirst proto.Value
inSecond proto.Value
intThird proto.Value
out string
}

for _, it := range []tt{
{proto.NewValueString("a"), proto.NewValueInt64(4), proto.NewValueInt64(2), "0"},
} {
t.Run(it.out, func(t *testing.T) {
out, err := fn.Apply(context.Background(), proto.ToValuer(it.inFirst), proto.ToValuer(it.inSecond), proto.ToValuer(it.intThird))
assert.NoError(t, err)
assert.Equal(t, it.out, fmt.Sprint(out))
})
}
}

func TestCastDecimal_Inputs_Error(t *testing.T) {
fn := proto.MustGetFunc(FuncCastDecimal)
assert.Equal(t, 3, fn.NumInput())

type tt struct {
inFirst proto.Value
inSecond proto.Value
intThird proto.Value
out string
}

for _, it := range []tt{
{proto.NewValueInt64(15), proto.NewValueString("a"), proto.NewValueInt64(2), "0"},
{proto.NewValueInt64(15), proto.NewValueInt64(4), proto.NewValueString("a"), "0"},
{proto.NewValueInt64(15), proto.NewValueInt64(4), proto.NewValueInt64(5), "0"},
} {
t.Run(it.out, func(t *testing.T) {
out, err := fn.Apply(context.Background(), proto.ToValuer(it.inFirst), proto.ToValuer(it.inSecond), proto.ToValuer(it.intThird))
assert.Error(t, err)
assert.Nil(t, out)
})
}
}
7 changes: 0 additions & 7 deletions pkg/runtime/function/cast_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,6 @@ func (a castTimeFunc) splitTimeWithoutSep(timeArgs string) (hour, minutes, secon
return timeHour, timeMinutes, timeSecond
}

func (a castTimeFunc) IsDayValid(day int) bool {
if day >= 0 && day <= 34 {
return true
}
return false
}

func (a castTimeFunc) IsHourValid(hour int) bool {
if hour >= 0 && hour <= 838 {
return true
Expand Down
4 changes: 4 additions & 0 deletions pkg/runtime/function/cast_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestFuncCastTime(t *testing.T) {
{"3 1", "73:00:00"},
{"-34 22", "-838:00:00"},
{"35 1", "838:59:59"},
{"+35 1", "838:59:59"},
{"-838:59:59", "-838:59:59"},
{"1:2:3", "01:02:03"},
{"1:1", "01:01:00"},
Expand All @@ -60,6 +61,9 @@ func TestFuncCastTime(t *testing.T) {
{"17ab10", "00:00:00"},
{"8381211", "838:12:11"},
{"8391211", "838:59:59"},
{"", "00:00:00"},
{"2 1:2+599999", "00:00:00"},
{"2 1:59:59.599999", "50:00:00"},
} {
t.Run(v.want, func(t *testing.T) {
out, err := fn.Apply(context.Background(), proto.ToValuer(proto.NewValueString(v.inFirst)))
Expand Down
20 changes: 20 additions & 0 deletions pkg/runtime/function/ceil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,23 @@ func TestCeil(t *testing.T) {
})
}
}

func TestCeil_Error(t *testing.T) {
fn := proto.MustGetFunc(FuncCeil)
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)
})
}
}
18 changes: 18 additions & 0 deletions pkg/runtime/function/char_length_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,21 @@ func TestCharLength(t *testing.T) {
})
}
}

func TestCharLength_Error(t *testing.T) {
fn := proto.MustGetFunc(FuncCharLength)
assert.Equal(t, 1, fn.NumInput())
type tt struct {
name string
inFirst proto.Value
}
for _, v := range []tt{
{"Test_nil", nil},
} {
t.Run(v.name, func(t *testing.T) {
out, err := fn.Apply(context.Background(), proto.ToValuer(v.inFirst))
assert.Nil(t, err)
assert.Nil(t, out)
})
}
}
5 changes: 3 additions & 2 deletions pkg/runtime/function/concat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import (
)

func TestConcat(t *testing.T) {
f := proto.MustGetFunc(FuncConcat)
fn := proto.MustGetFunc(FuncConcat)
assert.Equal(t, 1, fn.NumInput())

type tt struct {
inputs []proto.Value
Expand Down Expand Up @@ -75,7 +76,7 @@ func TestConcat(t *testing.T) {
for i := range next.inputs {
inputs = append(inputs, proto.ToValuer(next.inputs[i]))
}
out, err := f.Apply(context.Background(), inputs...)
out, err := fn.Apply(context.Background(), inputs...)
assert.NoError(t, err)

var actual string
Expand Down

0 comments on commit a6636b7

Please sign in to comment.