Skip to content

Commit

Permalink
tests: rewrite some expression IT, move them to UT (#46597)
Browse files Browse the repository at this point in the history
ref #45961
  • Loading branch information
Defined2014 authored Sep 1, 2023
1 parent 975fc85 commit 60b1019
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 215 deletions.
80 changes: 32 additions & 48 deletions expression/builtin_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,56 +491,32 @@ func TestRight(t *testing.T) {
}

func TestRepeat(t *testing.T) {
cases := []struct {
args []interface{}
isNull bool
res string
}{
{[]interface{}{"a", int64(2)}, false, "aa"},
{[]interface{}{"a", uint64(16777217)}, false, strings.Repeat("a", 16777217)},
{[]interface{}{"a", int64(16777216)}, false, strings.Repeat("a", 16777216)},
{[]interface{}{"a", int64(-1)}, false, ""},
{[]interface{}{"a", int64(0)}, false, ""},
{[]interface{}{"a", uint64(0)}, false, ""},
}

ctx := createContext(t)
args := []interface{}{"a", int64(2)}
fc := funcs[ast.Repeat]
f, err := fc.getFunction(ctx, datumsToConstants(types.MakeDatums(args...)))
require.NoError(t, err)
v, err := evalBuiltinFunc(f, chunk.Row{})
require.NoError(t, err)
require.Equal(t, "aa", v.GetString())

args = []interface{}{"a", uint64(2)}
f, err = fc.getFunction(ctx, datumsToConstants(types.MakeDatums(args...)))
require.NoError(t, err)
v, err = evalBuiltinFunc(f, chunk.Row{})
require.NoError(t, err)
require.Equal(t, "aa", v.GetString())

args = []interface{}{"a", uint64(16777217)}
f, err = fc.getFunction(ctx, datumsToConstants(types.MakeDatums(args...)))
require.NoError(t, err)
v, err = evalBuiltinFunc(f, chunk.Row{})
require.NoError(t, err)
require.False(t, v.IsNull())

args = []interface{}{"a", uint64(16777216)}
f, err = fc.getFunction(ctx, datumsToConstants(types.MakeDatums(args...)))
require.NoError(t, err)
v, err = evalBuiltinFunc(f, chunk.Row{})
require.NoError(t, err)
require.False(t, v.IsNull())

args = []interface{}{"a", int64(-1)}
f, err = fc.getFunction(ctx, datumsToConstants(types.MakeDatums(args...)))
require.NoError(t, err)
v, err = evalBuiltinFunc(f, chunk.Row{})
require.NoError(t, err)
require.Equal(t, "", v.GetString())

args = []interface{}{"a", int64(0)}
f, err = fc.getFunction(ctx, datumsToConstants(types.MakeDatums(args...)))
require.NoError(t, err)
v, err = evalBuiltinFunc(f, chunk.Row{})
require.NoError(t, err)
require.Equal(t, "", v.GetString())

args = []interface{}{"a", uint64(0)}
f, err = fc.getFunction(ctx, datumsToConstants(types.MakeDatums(args...)))
require.NoError(t, err)
v, err = evalBuiltinFunc(f, chunk.Row{})
require.NoError(t, err)
require.Equal(t, "", v.GetString())
for _, c := range cases {
f, err := fc.getFunction(ctx, datumsToConstants(types.MakeDatums(c.args...)))
require.NoError(t, err)
v, err := evalBuiltinFunc(f, chunk.Row{})
require.NoError(t, err)
if c.isNull {
require.True(t, v.IsNull())
} else {
require.Equal(t, v.GetString(), c.res)
}
}
}

func TestRepeatSig(t *testing.T) {
Expand Down Expand Up @@ -1581,6 +1557,10 @@ func TestLpad(t *testing.T) {
{"hi", 5, "", nil},
{"hi", 5, "ab", "abahi"},
{"hi", 6, "ab", "ababhi"},
{"中文", 5, "字符", "字符字中文"},
{"中文", 1, "a", "中"},
{"中文", -5, "字符", nil},
{"中文", 10, "", nil},
}
fc := funcs[ast.Lpad]
for _, test := range tests {
Expand Down Expand Up @@ -1617,6 +1597,10 @@ func TestRpad(t *testing.T) {
{"hi", 5, "", nil},
{"hi", 5, "ab", "hiaba"},
{"hi", 6, "ab", "hiabab"},
{"中文", 5, "字符", "中文字符字"},
{"中文", 1, "a", "中"},
{"中文", -5, "字符", nil},
{"中文", 10, "", nil},
}
fc := funcs[ast.Rpad]
for _, test := range tests {
Expand Down
121 changes: 97 additions & 24 deletions expression/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package expression
import (
"context"
"fmt"
"math"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -230,6 +229,8 @@ func TestDate(t *testing.T) {
{nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil},
{"0000-00-00 00:00:00", 0, 0, nil, 0, nil, nil, nil, nil, nil, nil, nil},
{"0000-00-00", 0, 0, nil, 0, nil, nil, nil, nil, nil, nil, nil},
{"2007-00-03", nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil},
{"2007-02-00", nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil},
}

dtblNil := tblToDtbl(tblNil)
Expand Down Expand Up @@ -1233,21 +1234,24 @@ func builtinDateFormat(ctx sessionctx.Context, args []types.Datum) (d types.Datu
func TestFromUnixTime(t *testing.T) {
ctx := createContext(t)
tbl := []struct {
isDecimal bool
integralPart int64
fractionalPart int64
decimal float64
format string
expect string
isDecimal bool
integralPart int64
decimal float64
format string
expect string
}{
{false, 1451606400, 0, 0, "", "2016-01-01 00:00:00"},
{true, 1451606400, 123456000, 1451606400.123456, "", "2016-01-01 00:00:00.123456"},
{true, 1451606400, 999999000, 1451606400.999999, "", "2016-01-01 00:00:00.999999"},
{true, 1451606400, 999999900, 1451606400.9999999, "", "2016-01-01 00:00:01.000000"},
{false, 1451606400, 0, 0, `%Y %D %M %h:%i:%s %x`, "2016-01-01 00:00:00"},
{true, 1451606400, 123456000, 1451606400.123456, `%Y %D %M %h:%i:%s %x`, "2016-01-01 00:00:00.123456"},
{true, 1451606400, 999999000, 1451606400.999999, `%Y %D %M %h:%i:%s %x`, "2016-01-01 00:00:00.999999"},
{true, 1451606400, 999999900, 1451606400.9999999, `%Y %D %M %h:%i:%s %x`, "2016-01-01 00:00:01.000000"},
{false, 1451606400, 0, "", "2016-01-01 00:00:00"},
{true, 1451606400, 1451606400.123456, "", "2016-01-01 00:00:00.123456"},
{true, 1451606400, 1451606400.999999, "", "2016-01-01 00:00:00.999999"},
{true, 1451606400, 1451606400.9999999, "", "2016-01-01 00:00:01.000000"},
{false, 1451606400, 0, `%Y %D %M %h:%i:%s %x`, "2016-01-01 00:00:00"},
{true, 1451606400, 1451606400.123456, `%Y %D %M %h:%i:%s %x`, "2016-01-01 00:00:00.123456"},
{true, 1451606400, 1451606400.999999, `%Y %D %M %h:%i:%s %x`, "2016-01-01 00:00:00.999999"},
{true, 1451606400, 1451606400.9999999, `%Y %D %M %h:%i:%s %x`, "2016-01-01 00:00:01.000000"},

// TestIssue22206
{false, 5000000000, 0, "", "2128-06-11 08:53:20"},
{true, 32536771199, 32536771199.99999, "", "3001-01-18 23:59:59.999990"},
}
sc := ctx.GetSessionVars().StmtCtx
originTZ := sc.TimeZone
Expand Down Expand Up @@ -1299,9 +1303,10 @@ func TestFromUnixTime(t *testing.T) {
require.NoError(t, err)
require.Equal(t, types.KindNull, v.Kind())

f, err = fc.getFunction(ctx, datumsToConstants(types.MakeDatums(math.MaxInt32+1)))
// TestIssue22206
f, err = fc.getFunction(ctx, datumsToConstants(types.MakeDatums(32536771200)))
require.NoError(t, err)
_, err = evalBuiltinFunc(f, chunk.Row{})
v, err = evalBuiltinFunc(f, chunk.Row{})
require.NoError(t, err)
require.Equal(t, types.KindNull, v.Kind())
}
Expand Down Expand Up @@ -1746,11 +1751,14 @@ func TestTimestampDiff(t *testing.T) {
unit string
t1 string
t2 string
isNull bool
expect int64
}{
{"MONTH", "2003-02-01", "2003-05-01", 3},
{"YEAR", "2002-05-01", "2001-01-01", -1},
{"MINUTE", "2003-02-01", "2003-05-01 12:05:55", 128885},
{"MONTH", "2003-02-01", "2003-05-01", false, 3},
{"YEAR", "2002-05-01", "2001-01-01", false, -1},
{"MINUTE", "2003-02-01", "2003-05-01 12:05:55", false, 128885},
{"MONTH", "2003-00-01", "2003-05-01", true, 0},
{"MONTH", "2003-02-01", "2003-05-00", true, 0},
}

fc := funcs[ast.TimestampDiff]
Expand All @@ -1765,8 +1773,13 @@ func TestTimestampDiff(t *testing.T) {
require.NoError(t, err)
d, err := evalBuiltinFunc(f, chunk.Row{})
require.NoError(t, err)
require.Equal(t, test.expect, d.GetInt64())
if test.isNull {
require.True(t, d.IsNull())
} else {
require.Equal(t, test.expect, d.GetInt64())
}
}

sc := ctx.GetSessionVars().StmtCtx
sc.IgnoreTruncate.Store(true)
sc.IgnoreZeroInDate = true
Expand Down Expand Up @@ -1894,11 +1907,15 @@ func TestDateArithFuncs(t *testing.T) {
{date[1], fcAdd, -1, date[0]},
{date[1], fcAdd, -0.5, date[0]},
{date[1], fcAdd, -1.4, date[0]},
{"1998-10-00", fcAdd, 1, ""},
{"2004-00-01", fcAdd, 1, ""},

{date[1], fcSub, 1, date[0]},
{date[0], fcSub, -1, date[1]},
{date[0], fcSub, -0.5, date[1]},
{date[0], fcSub, -1.4, date[1]},
{"1998-10-00", fcSub, 31, ""},
{"2004-00-01", fcSub, 31, ""},
}
for _, test := range tests {
args := types.MakeDatums(test.inputDate, test.inputDecimal, "DAY")
Expand All @@ -1907,7 +1924,8 @@ func TestDateArithFuncs(t *testing.T) {
require.NotNil(t, f)
v, err := evalBuiltinFunc(f, chunk.Row{})
require.NoError(t, err)
require.Equal(t, test.expect, v.GetString())
s, _ := v.ToString()
require.Equal(t, test.expect, s)
}

args := types.MakeDatums(date[0], nil, "DAY")
Expand All @@ -1926,6 +1944,38 @@ func TestDateArithFuncs(t *testing.T) {
require.NoError(t, err)
require.True(t, v.IsNull())

// TestIssue11645
testHours := []struct {
input string
hours int
isNull bool
expected string
}{
{"1000-01-01 00:00:00", -2, false, "0999-12-31 22:00:00"},
{"1000-01-01 00:00:00", -200, false, "0999-12-23 16:00:00"},
{"0001-01-01 00:00:00", -2, false, "0000-00-00 22:00:00"},
{"0001-01-01 00:00:00", -25, false, "0000-00-00 23:00:00"},
{"0001-01-01 00:00:00", -8784, false, "0000-00-00 00:00:00"},
{"0001-01-01 00:00:00", -8785, true, ""},
{"0001-01-02 00:00:00", -2, false, "0001-01-01 22:00:00"},
{"0001-01-02 00:00:00", -24, false, "0001-01-01 00:00:00"},
{"0001-01-02 00:00:00", -25, false, "0000-00-00 23:00:00"},
{"0001-01-02 00:00:00", -8785, false, "0000-00-00 23:00:00"},
}
for _, test := range testHours {
args := types.MakeDatums(test.input, test.hours, "HOUR")
f, err := fcAdd.getFunction(ctx, datumsToConstants(args))
require.NoError(t, err)
require.NotNil(t, f)
v, err := evalBuiltinFunc(f, chunk.Row{})
require.NoError(t, err)
if test.isNull {
require.True(t, v.IsNull())
} else {
require.Equal(t, test.expected, v.GetString())
}
}

testMonths := []struct {
input string
months int
Expand Down Expand Up @@ -2161,6 +2211,14 @@ func TestTimestamp(t *testing.T) {
{[]types.Datum{types.NewDecimalDatum(types.NewDecFromStringForTest("20170118123950.123"))}, "2017-01-18 12:39:50.123"},
{[]types.Datum{types.NewDecimalDatum(types.NewDecFromStringForTest("20170118123950.999"))}, "2017-01-18 12:39:50.999"},
{[]types.Datum{types.NewDecimalDatum(types.NewDecFromStringForTest("20170118123950.999"))}, "2017-01-18 12:39:50.999"},

// TestIssue25093
{[]types.Datum{types.NewDecimalDatum(types.NewDecFromStringForTest("0.123"))}, "0000-00-00 00:00:00.123"},
{[]types.Datum{types.NewDecimalDatum(types.NewDecFromStringForTest("0.4352"))}, "0000-00-00 00:00:00.4352"},
{[]types.Datum{types.NewDecimalDatum(types.NewDecFromStringForTest("0.12345678"))}, "0000-00-00 00:00:00.123457"},
{[]types.Datum{types.NewDecimalDatum(types.NewDecFromStringForTest("101.234"))}, "2000-01-01 00:00:00.000"},
{[]types.Datum{types.NewDecimalDatum(types.NewDecFromStringForTest("0.9999999"))}, ""},
{[]types.Datum{types.NewDecimalDatum(types.NewDecFromStringForTest("1.234"))}, ""},
}
fc := funcs[ast.Timestamp]
for _, test := range tests {
Expand Down Expand Up @@ -2448,6 +2506,8 @@ func TestToSeconds(t *testing.T) {
"0000-00-00",
"1992-13-00",
"2007-10-07 23:59:61",
"1998-10-00",
"1998-00-11",
123456789}

for _, i := range testsNull {
Expand Down Expand Up @@ -2490,6 +2550,7 @@ func TestToDays(t *testing.T) {
"0000-00-00",
"1992-13-00",
"2007-10-07 23:59:61",
"1998-10-00",
123456789}

for _, i := range testsNull {
Expand Down Expand Up @@ -2693,6 +2754,11 @@ func TestSecToTime(t *testing.T) {
{0, types.NewStringDatum("123.4567891"), "00:02:03.456789"},
{0, types.NewStringDatum("123"), "00:02:03.000000"},
{0, types.NewStringDatum("abc"), "00:00:00.000000"},
// Issue #15613
{0, types.NewStringDatum("1e-4"), "00:00:00.000100"},
{0, types.NewStringDatum("1e-5"), "00:00:00.000010"},
{0, types.NewStringDatum("1e-6"), "00:00:00.000001"},
{0, types.NewStringDatum("1e-7"), "00:00:00.000000"},
}
for _, test := range tests {
comment := fmt.Sprintf("%+v", test)
Expand Down Expand Up @@ -2761,6 +2827,13 @@ func TestConvertTz(t *testing.T) {
{"2021-03-28 02:30:00", "Europe/Amsterdam", "UTC", true, "2021-03-28 01:00:00"},
{"2021-10-22 10:00:00", "Europe/Tallinn", "SYSTEM", true, t1.In(loc2).Format("2006-01-02 15:04:00")},
{"2021-10-22 10:00:00", "SYSTEM", "Europe/Tallinn", true, t2.In(loc1).Format("2006-01-02 15:04:00")},

// TestIssue30081
{"2007-03-11 2:00:00", "US/Eastern", "US/Central", true, "2007-03-11 01:00:00"},
{"2007-03-11 3:00:00", "US/Eastern", "US/Central", true, "2007-03-11 01:00:00"},

{"2004-10-00 12:00:00", "GMT", "MET", true, ""},
{"2004-00-01 12:00:00", "GMT", "MET", true, ""},
}
fc := funcs[ast.ConvertTz]
for _, test := range tests {
Expand All @@ -2774,11 +2847,11 @@ func TestConvertTz(t *testing.T) {
d, err := evalBuiltinFunc(f, chunk.Row{})
if test.Success {
require.NoError(t, err)
result, _ := d.ToString()
require.Equalf(t, test.expect, result, "convert_tz(\"%v\", \"%s\", \"%s\")", test.t, test.fromTz, test.toTz)
} else {
require.Error(t, err)
}
result, _ := d.ToString()
require.Equalf(t, test.expect, result, "convert_tz(\"%v\", \"%s\", \"%s\")", test.t, test.fromTz, test.toTz)
}
}

Expand Down
Loading

0 comments on commit 60b1019

Please sign in to comment.