123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865 |
- // Copyright 2023 EMQ Technologies Co., Ltd.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package function
- import (
- "errors"
- "fmt"
- "reflect"
- "testing"
- "time"
- "github.com/lf-edge/ekuiper/internal/conf"
- kctx "github.com/lf-edge/ekuiper/internal/topo/context"
- "github.com/lf-edge/ekuiper/internal/topo/state"
- "github.com/lf-edge/ekuiper/pkg/api"
- "github.com/lf-edge/ekuiper/pkg/ast"
- "github.com/lf-edge/ekuiper/pkg/cast"
- )
- // TestDateTimeFunctions test the date and time functions.
- func TestDateTimeFunctions(t *testing.T) {
- contextLogger := conf.Log.WithField("rule", "testExec")
- ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
- tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
- fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
- tests := []struct {
- // testCaseName represent the name of the test case
- testCaseName string
- // funcName represent the SQL function name to be tested
- funcName string
- // execArgs represent the arguments to be passed to the function
- execArgs []interface{}
- // valFunc represent the function to validate the result
- valFunc func(t interface{}) error
- // execTest represent whether to test the exec function
- execTest bool
- // valArgs represent the arguments to be passed to the builtinFunc.val
- valArgs []ast.Expr
- }{
- {
- testCaseName: "test now() with no args",
- funcName: "now",
- execArgs: []interface{}{},
- valFunc: func(t interface{}) error {
- _, err := cast.ParseTime(t.(string), "yyyy-MM-dd HH:mm:ss")
- return err
- },
- execTest: true,
- },
- {
- testCaseName: "test now() with fsp set to 1",
- funcName: "now",
- execArgs: []interface{}{1},
- valFunc: func(t interface{}) error {
- _, err := cast.ParseTime(t.(string), "yyyy-MM-dd HH:mm:ss.S")
- return err
- },
- execTest: true,
- },
- {
- testCaseName: "test now() with fsp set to 2",
- funcName: "now",
- execArgs: []interface{}{2},
- valFunc: func(t interface{}) error {
- _, err := cast.ParseTime(t.(string), "yyyy-MM-dd HH:mm:ss.SS")
- return err
- },
- execTest: true,
- },
- {
- testCaseName: "test now() with fsp set to 3",
- funcName: "now",
- execArgs: []interface{}{3},
- valFunc: func(t interface{}) error {
- _, err := cast.ParseTime(t.(string), "yyyy-MM-dd HH:mm:ss.SSS")
- return err
- },
- execTest: true,
- },
- {
- testCaseName: "test now() with fsp set to 4",
- funcName: "now",
- execArgs: []interface{}{4},
- valFunc: func(t interface{}) error {
- _, err := cast.ParseTime(t.(string), "yyyy-MM-dd HH:mm:ss.SSSS")
- return err
- },
- execTest: true,
- },
- {
- testCaseName: "test now() with fsp set to 5",
- funcName: "now",
- execArgs: []interface{}{5},
- valFunc: func(t interface{}) error {
- _, err := cast.ParseTime(t.(string), "yyyy-MM-dd HH:mm:ss.SSSSS")
- return err
- },
- execTest: true,
- },
- {
- testCaseName: "test now() with fsp set to 6",
- funcName: "now",
- execArgs: []interface{}{6},
- valFunc: func(t interface{}) error {
- _, err := cast.ParseTime(t.(string), "yyyy-MM-dd HH:mm:ss.SSSSSS")
- return err
- },
- execTest: true,
- },
- {
- testCaseName: "test now() with too many args",
- funcName: "now",
- valFunc: func(t interface{}) error {
- if !reflect.DeepEqual(t, errTooManyArguments) {
- return errors.New("mismatch error")
- }
- return nil
- },
- execTest: false,
- valArgs: []ast.Expr{&ast.IntegerLiteral{Val: 1}, &ast.IntegerLiteral{Val: 2}},
- },
- {
- testCaseName: "test cur_date() with no args",
- funcName: "cur_date",
- execArgs: []interface{}{},
- valFunc: func(t interface{}) error {
- _, err := cast.ParseTime(t.(string), "yyyy-MM-dd")
- return err
- },
- execTest: true,
- },
- {
- testCaseName: "test cur_date() with too many args",
- funcName: "cur_date",
- execTest: false,
- valFunc: func(t interface{}) error {
- if !reflect.DeepEqual(t, errors.New("Expect 0 arguments but found 1.")) {
- return errors.New("mismatch error")
- }
- return nil
- },
- valArgs: []ast.Expr{&ast.IntegerLiteral{Val: 1}},
- },
- {
- testCaseName: "test cur_time() with no args",
- funcName: "cur_time",
- execTest: true,
- execArgs: []interface{}{},
- valFunc: func(t interface{}) error {
- _, err := cast.ParseTime(t.(string), "HH:mm:ss")
- return err
- },
- },
- {
- testCaseName: "test cur_time() with fsp set to 1",
- funcName: "cur_time",
- execTest: true,
- execArgs: []interface{}{1},
- valFunc: func(t interface{}) error {
- _, err := cast.ParseTime(t.(string), "HH:mm:ss.S")
- return err
- },
- },
- {
- testCaseName: "test format_time() with 'yyyy-MM-dd' format",
- funcName: "format_time",
- execTest: true,
- execArgs: []interface{}{time.Now(), "yyyy-MM-dd"},
- valFunc: func(t interface{}) error {
- _, err := cast.ParseTime(t.(string), "yyyy-MM-dd")
- return err
- },
- },
- {
- testCaseName: "test format_time() with 1 arg",
- funcName: "format_time",
- execTest: false,
- valArgs: []ast.Expr{
- &ast.IntegerLiteral{Val: 1},
- },
- valFunc: func(t interface{}) error {
- if !reflect.DeepEqual(t, errors.New("Expect 2 arguments but found 1.")) {
- return errors.New("mismatch error")
- }
- return nil
- },
- },
- {
- testCaseName: "test format_time() with invalid date time arg",
- funcName: "format_time",
- execTest: false,
- valArgs: []ast.Expr{
- &ast.IntegerLiteral{Val: 1},
- &ast.StringLiteral{Val: "yyyy-MM-dd"},
- },
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect datetime type for parameter 1")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test date_calc() for add 1 day (24h)",
- funcName: "date_calc",
- execTest: true,
- execArgs: []interface{}{"2019-01-01 00:00:00", "24h"},
- valFunc: func(t interface{}) error {
- parsed, err := cast.ParseTime(t.(string), "yyyy-MM-dd HH:mm:ss")
- if err != nil {
- return err
- }
- if parsed.Day() != 2 {
- return fmt.Errorf("mismatch days, expect %d, got %d", 2, parsed.Day())
- }
- return nil
- },
- },
- {
- testCaseName: "test date_calc() with invalid date time arg",
- funcName: "date_calc",
- execTest: false,
- valArgs: []ast.Expr{
- &ast.IntegerLiteral{Val: 1},
- &ast.StringLiteral{Val: "24h"},
- },
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect datetime type for parameter 1")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test date_calc() with no args",
- funcName: "date_calc",
- execTest: false,
- valArgs: []ast.Expr{},
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect 2 arguments but found 0.")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test date_calc() for sub 1 day (24h)",
- funcName: "date_calc",
- execTest: true,
- execArgs: []interface{}{"2019-01-01 00:00:00", "-24h"},
- valFunc: func(t interface{}) error {
- parsed, err := cast.ParseTime(t.(string), "yyyy-MM-dd HH:mm:ss")
- if err != nil {
- return err
- }
- if parsed.Day() != 31 {
- return fmt.Errorf("mismatch days, expect %d, got %d", 31, parsed.Day())
- }
- return nil
- },
- },
- {
- testCaseName: "test date_diff with 2 args",
- funcName: "date_diff",
- execTest: true,
- execArgs: []interface{}{"2019-01-01 00:00:00", "2019-01-02 00:00:00"},
- valFunc: func(t interface{}) error {
- result := t.(time.Duration)
- if result.Milliseconds() != 24*3600*1000 {
- return fmt.Errorf("mismatch result, expect %d, got %d", 26*3600*1000, result.Milliseconds())
- }
- return nil
- },
- },
- {
- testCaseName: "test date_diff with no arg",
- funcName: "date_diff",
- execTest: false,
- valArgs: []ast.Expr{},
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect 2 arguments but found 0.")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test date_diff with invalid date time arg",
- funcName: "date_diff",
- execTest: false,
- valArgs: []ast.Expr{
- &ast.IntegerLiteral{Val: 1},
- &ast.IntegerLiteral{Val: 2},
- },
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect datetime type for parameter 1")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test day_name with 1 arg",
- funcName: "day_name",
- execTest: true,
- execArgs: []interface{}{"2019-01-01 00:00:00"},
- valFunc: func(t interface{}) error {
- if t.(string) != "Tuesday" {
- return fmt.Errorf("mismatch day name, expect %s, got %s", "Tuesday", t.(string))
- }
- return nil
- },
- },
- {
- testCaseName: "test day_name with no arg",
- funcName: "day_name",
- execTest: false,
- valArgs: []ast.Expr{},
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect 1 arguments but found 0.")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test day_name with invalid date time arg",
- funcName: "day_name",
- execTest: false,
- valArgs: []ast.Expr{
- &ast.IntegerLiteral{Val: 1},
- },
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect datetime type for parameter 1")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test day_of_month with 1 arg",
- funcName: "day_of_month",
- execTest: true,
- execArgs: []interface{}{"2019-01-01 00:00:00"},
- valFunc: func(t interface{}) error {
- if t.(int) != 1 {
- return fmt.Errorf("mismatch day of month, expect %d, got %d", 1, t.(int))
- }
- return nil
- },
- },
- {
- testCaseName: "test day_of_month with no arg",
- funcName: "day_of_month",
- execTest: false,
- valArgs: []ast.Expr{},
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect 1 arguments but found 0.")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test day_of_month with invalid date time arg",
- funcName: "day_of_month",
- execTest: false,
- valArgs: []ast.Expr{
- &ast.IntegerLiteral{Val: 1},
- },
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect datetime type for parameter 1")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test day_of_week with 1 arg",
- funcName: "day_of_week",
- execTest: true,
- execArgs: []interface{}{"2019-01-01 00:00:00"},
- valFunc: func(t interface{}) error {
- if t.(time.Weekday) != time.Tuesday {
- return fmt.Errorf("mismatch day of week, expect %d, got %d", time.Tuesday, t.(time.Weekday))
- }
- return nil
- },
- },
- {
- testCaseName: "test day_of_week with no arg",
- funcName: "day_of_week",
- execTest: false,
- valArgs: []ast.Expr{},
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect 1 arguments but found 0.")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test day_of_week with invalid date time arg",
- funcName: "day_of_week",
- execTest: false,
- valArgs: []ast.Expr{
- &ast.IntegerLiteral{Val: 1},
- },
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect datetime type for parameter 1")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test day_of_year with 1 arg",
- funcName: "day_of_year",
- execTest: true,
- execArgs: []interface{}{"2019-01-01 00:00:00"},
- valFunc: func(t interface{}) error {
- if t.(int) != 1 {
- return fmt.Errorf("mismatch day of year, expect %d, got %d", 1, t.(int))
- }
- return nil
- },
- },
- {
- testCaseName: "test day_of_year with no arg",
- funcName: "day_of_year",
- execTest: false,
- valArgs: []ast.Expr{},
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect 1 arguments but found 0.")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test day_of_year with invalid date time arg",
- funcName: "day_of_year",
- execTest: false,
- valArgs: []ast.Expr{
- &ast.IntegerLiteral{Val: 1},
- },
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect datetime type for parameter 1")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test from_days with no arg",
- funcName: "from_days",
- execTest: false,
- valArgs: []ast.Expr{},
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect 1 arguments but found 0.")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test from_days with invalid date time arg",
- funcName: "from_days",
- execTest: false,
- valArgs: []ast.Expr{
- &ast.IntegerLiteral{Val: 1},
- },
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect int type for parameter 1")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test from_days with 100",
- funcName: "from_days",
- execTest: true,
- execArgs: []interface{}{100},
- valFunc: func(t interface{}) error {
- if t.(string) != "1970-04-10" {
- return fmt.Errorf("mismatch date, expect %s, got %s", "2019-01-01", t.(string))
- }
- return nil
- },
- },
- {
- testCaseName: "test from_unix_time with no arg",
- funcName: "from_unix_time",
- execTest: false,
- valArgs: []ast.Expr{},
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect 1 arguments but found 0.")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test from_unix_time with invalid arg",
- funcName: "from_unix_time",
- execTest: false,
- valArgs: []ast.Expr{
- &ast.NumberLiteral{Val: 0.1},
- },
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect int type for parameter 1")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %v", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test from_unix_time with 100",
- funcName: "from_unix_time",
- execTest: true,
- execArgs: []interface{}{100},
- valFunc: func(t interface{}) error {
- expect := time.Unix(100, 0)
- expectStr, err := cast.FormatTime(expect, "yyyy-MM-dd HH:mm:ss")
- if err != nil {
- return err
- }
- if t.(string) != expectStr {
- return fmt.Errorf("mismatch date, expect %s, got %s", expectStr, t.(string))
- }
- return nil
- },
- },
- {
- testCaseName: "test hour with no arg",
- funcName: "hour",
- execTest: false,
- valArgs: []ast.Expr{},
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect 1 arguments but found 0.")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %v", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test hour with invalid date time arg",
- funcName: "hour",
- execTest: false,
- valArgs: []ast.Expr{
- &ast.IntegerLiteral{Val: 1},
- },
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect datetime type for parameter 1")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %v", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test hour with 1 arg",
- funcName: "hour",
- execTest: true,
- execArgs: []interface{}{"2019-01-01 01:00:00"},
- valFunc: func(t interface{}) error {
- if t.(int) != 1 {
- return fmt.Errorf("mismatch hour, expect %d, got %d", 1, t.(int))
- }
- return nil
- },
- },
- {
- testCaseName: "test last_day with no arg",
- funcName: "last_day",
- execTest: false,
- valArgs: []ast.Expr{},
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect 1 arguments but found 0.")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %v", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test last_day with invalid date time arg",
- funcName: "last_day",
- execTest: false,
- valArgs: []ast.Expr{
- &ast.IntegerLiteral{Val: 1},
- },
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect datetime type for parameter 1")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %v", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test last_day with 1 arg",
- funcName: "last_day",
- execTest: true,
- execArgs: []interface{}{"2019-01-01 01:00:00"},
- valFunc: func(t interface{}) error {
- if t.(string) != "2019-01-31" {
- return fmt.Errorf("mismatch date, expect %s, got %s", "2019-01-31", t.(string))
- }
- return nil
- },
- },
- {
- testCaseName: "test microsecond with no arg",
- funcName: "microsecond",
- execTest: false,
- valArgs: []ast.Expr{},
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect 1 arguments but found 0.")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %v", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test microsecond with invalid date time arg",
- funcName: "microsecond",
- execTest: false,
- valArgs: []ast.Expr{
- &ast.IntegerLiteral{Val: 1},
- },
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect datetime type for parameter 1")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %v", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test microsecond with 1 arg",
- funcName: "microsecond",
- execTest: true,
- execArgs: []interface{}{"2019-01-01 01:00:00.123456"},
- valFunc: func(t interface{}) error {
- if t.(int) != 123456 {
- return fmt.Errorf("mismatch microsecond, expect %d, got %d", 123456, t.(int))
- }
- return nil
- },
- },
- {
- testCaseName: "test minute with no arg",
- funcName: "minute",
- execTest: false,
- valArgs: []ast.Expr{},
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect 1 arguments but found 0.")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %v", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test minute with invalid date time arg",
- funcName: "minute",
- execTest: false,
- valArgs: []ast.Expr{
- &ast.IntegerLiteral{Val: 1},
- },
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect datetime type for parameter 1")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %v", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test minute with 1 arg",
- funcName: "minute",
- execTest: true,
- execArgs: []interface{}{"2019-01-01 01:23:45"},
- valFunc: func(t interface{}) error {
- if t.(int) != 23 {
- return fmt.Errorf("mismatch minute, expect %d, got %d", 23, t.(int))
- }
- return nil
- },
- },
- {
- testCaseName: "test month with no arg",
- funcName: "month",
- execTest: false,
- valArgs: []ast.Expr{},
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect 1 arguments but found 0.")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got: %v", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test month with invalid date time arg",
- funcName: "month",
- execTest: false,
- valArgs: []ast.Expr{
- &ast.IntegerLiteral{Val: 1},
- },
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect datetime type for parameter 1")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect: %s, got %v", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test month with 1 arg",
- funcName: "month",
- execTest: true,
- execArgs: []interface{}{"2019-01-01 01:23:45"},
- valFunc: func(t interface{}) error {
- if t.(int) != 1 {
- return fmt.Errorf("mismatch month, expect %d, got %d", 1, t.(int))
- }
- return nil
- },
- },
- {
- testCaseName: "test month_name with no arg",
- funcName: "month_name",
- execTest: false,
- valArgs: []ast.Expr{},
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect 1 arguments but found 0.")
- if !reflect.DeepEqual(t, expect) {
- return fmt.Errorf("mismatch error, expect %s, got %v", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test month_name with invalid date time arg",
- funcName: "month_name",
- execTest: false,
- valArgs: []ast.Expr{
- &ast.IntegerLiteral{Val: 1},
- },
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect datetime type for parameter 1")
- if !reflect.DeepEqual(t, t) {
- return fmt.Errorf("mismatch error, expect %s, got %v", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test month_name with 1 arg",
- funcName: "month_name",
- execTest: true,
- execArgs: []interface{}{"2019-01-01 01:23:45"},
- valFunc: func(t interface{}) error {
- if t.(string) != "January" {
- return fmt.Errorf("mismatch month name, expect %s, got %s", "January", t.(string))
- }
- return nil
- },
- },
- {
- testCaseName: "test second with no arg",
- funcName: "second",
- execTest: false,
- valArgs: []ast.Expr{},
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect 1 arguments but found 0.")
- if !reflect.DeepEqual(t, t) {
- return fmt.Errorf("mismatch error, expect %s, got %v", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test second with invalid date time arg",
- funcName: "second",
- execTest: false,
- valArgs: []ast.Expr{
- &ast.IntegerLiteral{Val: 1},
- },
- valFunc: func(t interface{}) error {
- expect := errors.New("Expect datetime type for parameter 1")
- if !reflect.DeepEqual(t, t) {
- return fmt.Errorf("mismatch error, expect %s, got %v", expect, t)
- }
- return nil
- },
- },
- {
- testCaseName: "test second with 1 arg",
- funcName: "second",
- execTest: true,
- execArgs: []interface{}{"2019-01-01 01:23:45"},
- valFunc: func(t interface{}) error {
- if t.(int) != 45 {
- return fmt.Errorf("mismatch second, expect %d, got %d", 45, t.(int))
- }
- return nil
- },
- },
- }
- for _, test := range tests {
- f, ok := builtins[test.funcName]
- if !ok {
- t.Fatalf("builtin '%s' not found", test.funcName)
- }
- var result interface{}
- if test.execTest {
- result, _ = f.exec(fctx, test.execArgs)
- } else {
- result = f.val(fctx, test.valArgs)
- }
- if err := test.valFunc(result); err != nil {
- t.Errorf("\n%s: %q", test.testCaseName, err)
- }
- }
- }
|