Skip to content

Commit

Permalink
[Test] add func unit test (#720)
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
  • Loading branch information
dongzl authored Jul 25, 2023
1 parent 41a2fbd commit 3cd5cd1
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 13 deletions.
15 changes: 15 additions & 0 deletions pkg/runtime/function/cast_char_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@ func TestFuncCastChar(t *testing.T) {
}
for _, v := range []tt{
{"Hello", len("Hello"), "ASCII", "Hello"},
{"Hello", -1, "ASCII", "Hello"},
{"Hello", len("Hello"), "unicode", "\x00H\x00e\x00l\x00l\x00o"},
{"Hello世界", len("Hello世界"), "CHARACTER SET GBK", "Hello\xca\xc0\xbd\xe7"},
{"Hello世界", len("Hello世界"), "CHARACTER SET gb18030", "Hello\xca\xc0\xbd\xe7"},
{"Hello世界", len("Hello世界"), "", "Hello世界"},
{"Hello世界", 5, "CHARACTER SET latin2", "Hello"},
{"Hello世界", 5, "CHARACTER SET latin5", "Hello"},
{"Hello世界", 5, "CHARACTER SET greek", "Hello"},
{"Hello世界", 5, "CHARACTER SET hebrew", "Hello"},
{"Hello世界", 5, "CHARACTER SET latin7", "Hello"},
} {
t.Run(v.want, func(t *testing.T) {
out, err := fn.Apply(
Expand All @@ -58,5 +63,15 @@ func TestFuncCastChar(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, v.want, fmt.Sprint(out))
})

t.Run(v.want, func(t *testing.T) {
out, err := fn.Apply(
context.Background(),
proto.ToValuer(proto.NewValueString(v.inFirst)),
proto.ToValuer(proto.NewValueInt64(int64(v.inSecond))),
)
assert.NoError(t, err)
assert.Equal(t, v.inFirst, fmt.Sprint(out))
})
}
}
14 changes: 3 additions & 11 deletions pkg/runtime/function/cast_date.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (a castDateFunc) splitDateWithSep(dateArgs string) (year, month, day int) {
dateYearStr := dateLeft

dateYear, _ := strconv.Atoi(dateYearStr)
dateYear = a.amend4DigtalYear(dateYear)
dateYear = a.amend4DigitalYear(dateYear)
dateMonth, _ := strconv.Atoi(dateMonthStr)
dateDay, _ := strconv.Atoi(dateDayStr)

Expand All @@ -155,14 +155,14 @@ func (a castDateFunc) splitDateWithoutSep(dateArgs string) (year, month, day int
dateYearStr := dateLeft

dateYear, _ := strconv.Atoi(dateYearStr)
dateYear = a.amend4DigtalYear(dateYear)
dateYear = a.amend4DigitalYear(dateYear)
dateMonth, _ := strconv.Atoi(dateMonthStr)
dateDay, _ := strconv.Atoi(dateDayStr)

return dateYear, dateMonth, dateDay
}

func (a castDateFunc) amend4DigtalYear(year int) int {
func (a castDateFunc) amend4DigitalYear(year int) int {
if year >= 0 && year <= 69 {
year += 2000
}
Expand Down Expand Up @@ -224,14 +224,6 @@ func (a castDateFunc) IsDayValid(year, month, day int) bool {
return false
}

func (a castDateFunc) MaxDateValue() proto.Value {
return proto.NewValueString("9999-12-31")
}

func (a castDateFunc) MinDateValue() proto.Value {
return proto.NewValueString("0000-01-01")
}

func (a castDateFunc) DefaultDateValue() proto.Value {
return proto.NewValueString("0000-00-00")
}
6 changes: 6 additions & 0 deletions pkg/runtime/function/cast_date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ func TestFuncCastDate(t *testing.T) {
{"1991202", "0199-12-02"},
{"20051202", "2005-12-02"},
{"20051234", "0000-00-00"},
{"20051314", "0000-00-00"},
{"20050628", "2005-06-28"},
{"20050631", "0000-00-00"},
{"20000229", "2000-02-29"},
{"20000230", "0000-00-00"},
{"999990224", "0000-00-00"},
} {
t.Run(v.want, func(t *testing.T) {
out, err := fn.Apply(context.Background(), proto.ToValuer(proto.NewValueString(v.inFirst)))
Expand Down
4 changes: 2 additions & 2 deletions pkg/runtime/function/cast_datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (a castDatetimeFunc) Apply(ctx context.Context, inputs ...proto.Valuer) (pr
}
datetimeArrReplace := _datetimeReplace.ReplaceAllStringFunc(datetimeArr[0], func(s string) string { return "-" })
year, month, day := castDate.splitDateWithSep(datetimeArrReplace)
year = castDate.amend4DigtalYear(year)
year = castDate.amend4DigitalYear(year)

match = _datetimeMatchLowerString.MatchString(datetimeArr[1])
if !match {
Expand All @@ -125,7 +125,7 @@ func (a castDatetimeFunc) Apply(ctx context.Context, inputs ...proto.Valuer) (pr
datetimeLen := len(datetimeArgs)
dateArgs := datetimeArgs[0 : datetimeLen-6]
year, month, day := castDate.splitDateWithoutSep(dateArgs)
year = castDate.amend4DigtalYear(year)
year = castDate.amend4DigitalYear(year)

timeArgs := datetimeArgs[datetimeLen-6 : datetimeLen]
hour, minutes, second := a.splitDatetimeWithoutSep(timeArgs)
Expand Down
2 changes: 2 additions & 0 deletions pkg/runtime/function/cast_datetime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestFuncCastDatetime(t *testing.T) {
for _, v := range []tt{
{"99-12-2 1:2:3", "1999-12-02 01:02:03"},
{"99-12-20T11:2:33", "1999-12-20 11:02:33"},
{"99-12-20A11:2:33", "0000-00-00 00:00:00"},
{"5#2?2 17%33!24.486762", "2005-02-02 17:33:24"},
{"199#2?2T23#16+44", "0199-02-02 23:16:44"},
{"12=2+29 23=59+59.587425", "2012-03-01 00:00:00"},
Expand All @@ -56,6 +57,7 @@ func TestFuncCastDatetime(t *testing.T) {
{"20051202123459.172124", "2005-12-02 12:34:59"},
{"20051234193247", "0000-00-00 00:00:00"},
{"20051234561324", "0000-00-00 00:00:00"},
{"20051220561324", "0000-00-00 00:00:00"},
{"00000000000000", "0000-00-00 00:00:00"},
} {
t.Run(v.want, func(t *testing.T) {
Expand Down
42 changes: 42 additions & 0 deletions pkg/runtime/function/cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,45 @@ func TestCastUnsigned(t *testing.T) {
})
}
}

func TestCastSign_Error(t *testing.T) {
fn := proto.MustGetFunc(FuncCastSign)
assert.Equal(t, 1, fn.NumInput())

type tt struct {
desc string
in proto.Value
out int64
}

for _, it := range []tt{
{"CAST(a AS SIGNED)", proto.NewValueString("a"), 0},
} {
t.Run(it.desc, func(t *testing.T) {
out, err := fn.Apply(context.Background(), proto.ToValuer(it.in))
assert.Error(t, err)
assert.Nil(t, out)
})
}
}

func TestCastSign_Nil(t *testing.T) {
fn := proto.MustGetFunc(FuncCastSign)
assert.Equal(t, 1, fn.NumInput())

type tt struct {
desc string
in proto.Value
out int64
}

for _, it := range []tt{
{"CAST(nil AS SIGNED)", nil, 0},
} {
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)
})
}
}

0 comments on commit 3cd5cd1

Please sign in to comment.