funcs_datetime_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. // Copyright 2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package function
  15. import (
  16. "errors"
  17. "fmt"
  18. "reflect"
  19. "testing"
  20. "time"
  21. "github.com/lf-edge/ekuiper/internal/conf"
  22. kctx "github.com/lf-edge/ekuiper/internal/topo/context"
  23. "github.com/lf-edge/ekuiper/internal/topo/state"
  24. "github.com/lf-edge/ekuiper/pkg/api"
  25. "github.com/lf-edge/ekuiper/pkg/ast"
  26. "github.com/lf-edge/ekuiper/pkg/cast"
  27. )
  28. // TestDateTimeFunctions test the date and time functions.
  29. func TestDateTimeFunctions(t *testing.T) {
  30. contextLogger := conf.Log.WithField("rule", "testExec")
  31. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  32. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  33. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  34. tests := []struct {
  35. // testCaseName represent the name of the test case
  36. testCaseName string
  37. // funcName represent the SQL function name to be tested
  38. funcName string
  39. // execArgs represent the arguments to be passed to the function
  40. execArgs []interface{}
  41. // valFunc represent the function to validate the result
  42. valFunc func(t interface{}) error
  43. // execTest represent whether to test the exec function
  44. execTest bool
  45. // valArgs represent the arguments to be passed to the builtinFunc.val
  46. valArgs []ast.Expr
  47. }{
  48. {
  49. testCaseName: "test now() with no args",
  50. funcName: "now",
  51. execArgs: []interface{}{},
  52. valFunc: func(t interface{}) error {
  53. _, err := cast.ParseTime(t.(string), "yyyy-MM-dd HH:mm:ss")
  54. return err
  55. },
  56. execTest: true,
  57. },
  58. {
  59. testCaseName: "test now() with fsp set to 1",
  60. funcName: "now",
  61. execArgs: []interface{}{1},
  62. valFunc: func(t interface{}) error {
  63. _, err := cast.ParseTime(t.(string), "yyyy-MM-dd HH:mm:ss.S")
  64. return err
  65. },
  66. execTest: true,
  67. },
  68. {
  69. testCaseName: "test now() with fsp set to 2",
  70. funcName: "now",
  71. execArgs: []interface{}{2},
  72. valFunc: func(t interface{}) error {
  73. _, err := cast.ParseTime(t.(string), "yyyy-MM-dd HH:mm:ss.SS")
  74. return err
  75. },
  76. execTest: true,
  77. },
  78. {
  79. testCaseName: "test now() with fsp set to 3",
  80. funcName: "now",
  81. execArgs: []interface{}{3},
  82. valFunc: func(t interface{}) error {
  83. _, err := cast.ParseTime(t.(string), "yyyy-MM-dd HH:mm:ss.SSS")
  84. return err
  85. },
  86. execTest: true,
  87. },
  88. {
  89. testCaseName: "test now() with fsp set to 4",
  90. funcName: "now",
  91. execArgs: []interface{}{4},
  92. valFunc: func(t interface{}) error {
  93. _, err := cast.ParseTime(t.(string), "yyyy-MM-dd HH:mm:ss.SSSS")
  94. return err
  95. },
  96. execTest: true,
  97. },
  98. {
  99. testCaseName: "test now() with fsp set to 5",
  100. funcName: "now",
  101. execArgs: []interface{}{5},
  102. valFunc: func(t interface{}) error {
  103. _, err := cast.ParseTime(t.(string), "yyyy-MM-dd HH:mm:ss.SSSSS")
  104. return err
  105. },
  106. execTest: true,
  107. },
  108. {
  109. testCaseName: "test now() with fsp set to 6",
  110. funcName: "now",
  111. execArgs: []interface{}{6},
  112. valFunc: func(t interface{}) error {
  113. _, err := cast.ParseTime(t.(string), "yyyy-MM-dd HH:mm:ss.SSSSSS")
  114. return err
  115. },
  116. execTest: true,
  117. },
  118. {
  119. testCaseName: "test now() with too many args",
  120. funcName: "now",
  121. valFunc: func(t interface{}) error {
  122. if !reflect.DeepEqual(t, errTooManyArguments) {
  123. return errors.New("mismatch error")
  124. }
  125. return nil
  126. },
  127. execTest: false,
  128. valArgs: []ast.Expr{&ast.IntegerLiteral{Val: 1}, &ast.IntegerLiteral{Val: 2}},
  129. },
  130. {
  131. testCaseName: "test cur_date() with no args",
  132. funcName: "cur_date",
  133. execArgs: []interface{}{},
  134. valFunc: func(t interface{}) error {
  135. _, err := cast.ParseTime(t.(string), "yyyy-MM-dd")
  136. return err
  137. },
  138. execTest: true,
  139. },
  140. {
  141. testCaseName: "test cur_date() with too many args",
  142. funcName: "cur_date",
  143. execTest: false,
  144. valFunc: func(t interface{}) error {
  145. if !reflect.DeepEqual(t, errors.New("Expect 0 arguments but found 1.")) {
  146. return errors.New("mismatch error")
  147. }
  148. return nil
  149. },
  150. valArgs: []ast.Expr{&ast.IntegerLiteral{Val: 1}},
  151. },
  152. {
  153. testCaseName: "test cur_time() with no args",
  154. funcName: "cur_time",
  155. execTest: true,
  156. execArgs: []interface{}{},
  157. valFunc: func(t interface{}) error {
  158. _, err := cast.ParseTime(t.(string), "HH:mm:ss")
  159. return err
  160. },
  161. },
  162. {
  163. testCaseName: "test cur_time() with fsp set to 1",
  164. funcName: "cur_time",
  165. execTest: true,
  166. execArgs: []interface{}{1},
  167. valFunc: func(t interface{}) error {
  168. _, err := cast.ParseTime(t.(string), "HH:mm:ss.S")
  169. return err
  170. },
  171. },
  172. {
  173. testCaseName: "test format_time() with 'yyyy-MM-dd' format",
  174. funcName: "format_time",
  175. execTest: true,
  176. execArgs: []interface{}{time.Now(), "yyyy-MM-dd"},
  177. valFunc: func(t interface{}) error {
  178. _, err := cast.ParseTime(t.(string), "yyyy-MM-dd")
  179. return err
  180. },
  181. },
  182. {
  183. testCaseName: "test format_time() with 1 arg",
  184. funcName: "format_time",
  185. execTest: false,
  186. valArgs: []ast.Expr{
  187. &ast.IntegerLiteral{Val: 1},
  188. },
  189. valFunc: func(t interface{}) error {
  190. if !reflect.DeepEqual(t, errors.New("Expect 2 arguments but found 1.")) {
  191. return errors.New("mismatch error")
  192. }
  193. return nil
  194. },
  195. },
  196. {
  197. testCaseName: "test format_time() with invalid date time arg",
  198. funcName: "format_time",
  199. execTest: false,
  200. valArgs: []ast.Expr{
  201. &ast.IntegerLiteral{Val: 1},
  202. &ast.StringLiteral{Val: "yyyy-MM-dd"},
  203. },
  204. valFunc: func(t interface{}) error {
  205. expect := errors.New("Expect datetime type for parameter 1")
  206. if !reflect.DeepEqual(t, expect) {
  207. return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
  208. }
  209. return nil
  210. },
  211. },
  212. {
  213. testCaseName: "test date_calc() for add 1 day (24h)",
  214. funcName: "date_calc",
  215. execTest: true,
  216. execArgs: []interface{}{"2019-01-01 00:00:00", "24h"},
  217. valFunc: func(t interface{}) error {
  218. parsed, err := cast.ParseTime(t.(string), "yyyy-MM-dd HH:mm:ss")
  219. if err != nil {
  220. return err
  221. }
  222. if parsed.Day() != 2 {
  223. return fmt.Errorf("mismatch days, expect %d, got %d", 2, parsed.Day())
  224. }
  225. return nil
  226. },
  227. },
  228. {
  229. testCaseName: "test date_calc() with invalid date time arg",
  230. funcName: "date_calc",
  231. execTest: false,
  232. valArgs: []ast.Expr{
  233. &ast.IntegerLiteral{Val: 1},
  234. &ast.StringLiteral{Val: "24h"},
  235. },
  236. valFunc: func(t interface{}) error {
  237. expect := errors.New("Expect datetime type for parameter 1")
  238. if !reflect.DeepEqual(t, expect) {
  239. return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
  240. }
  241. return nil
  242. },
  243. },
  244. {
  245. testCaseName: "test date_calc() with no args",
  246. funcName: "date_calc",
  247. execTest: false,
  248. valArgs: []ast.Expr{},
  249. valFunc: func(t interface{}) error {
  250. expect := errors.New("Expect 2 arguments but found 0.")
  251. if !reflect.DeepEqual(t, expect) {
  252. return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
  253. }
  254. return nil
  255. },
  256. },
  257. {
  258. testCaseName: "test date_calc() for sub 1 day (24h)",
  259. funcName: "date_calc",
  260. execTest: true,
  261. execArgs: []interface{}{"2019-01-01 00:00:00", "-24h"},
  262. valFunc: func(t interface{}) error {
  263. parsed, err := cast.ParseTime(t.(string), "yyyy-MM-dd HH:mm:ss")
  264. if err != nil {
  265. return err
  266. }
  267. if parsed.Day() != 31 {
  268. return fmt.Errorf("mismatch days, expect %d, got %d", 31, parsed.Day())
  269. }
  270. return nil
  271. },
  272. },
  273. {
  274. testCaseName: "test date_diff with 2 args",
  275. funcName: "date_diff",
  276. execTest: true,
  277. execArgs: []interface{}{"2019-01-01 00:00:00", "2019-01-02 00:00:00"},
  278. valFunc: func(t interface{}) error {
  279. result := t.(time.Duration)
  280. if result.Milliseconds() != 24*3600*1000 {
  281. return fmt.Errorf("mismatch result, expect %d, got %d", 26*3600*1000, result.Milliseconds())
  282. }
  283. return nil
  284. },
  285. },
  286. {
  287. testCaseName: "test date_diff with no arg",
  288. funcName: "date_diff",
  289. execTest: false,
  290. valArgs: []ast.Expr{},
  291. valFunc: func(t interface{}) error {
  292. expect := errors.New("Expect 2 arguments but found 0.")
  293. if !reflect.DeepEqual(t, expect) {
  294. return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
  295. }
  296. return nil
  297. },
  298. },
  299. {
  300. testCaseName: "test date_diff with invalid date time arg",
  301. funcName: "date_diff",
  302. execTest: false,
  303. valArgs: []ast.Expr{
  304. &ast.IntegerLiteral{Val: 1},
  305. &ast.IntegerLiteral{Val: 2},
  306. },
  307. valFunc: func(t interface{}) error {
  308. expect := errors.New("Expect datetime type for parameter 1")
  309. if !reflect.DeepEqual(t, expect) {
  310. return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
  311. }
  312. return nil
  313. },
  314. },
  315. {
  316. testCaseName: "test day_name with 1 arg",
  317. funcName: "day_name",
  318. execTest: true,
  319. execArgs: []interface{}{"2019-01-01 00:00:00"},
  320. valFunc: func(t interface{}) error {
  321. if t.(string) != "Tuesday" {
  322. return fmt.Errorf("mismatch day name, expect %s, got %s", "Tuesday", t.(string))
  323. }
  324. return nil
  325. },
  326. },
  327. {
  328. testCaseName: "test day_name with no arg",
  329. funcName: "day_name",
  330. execTest: false,
  331. valArgs: []ast.Expr{},
  332. valFunc: func(t interface{}) error {
  333. expect := errors.New("Expect 1 arguments but found 0.")
  334. if !reflect.DeepEqual(t, expect) {
  335. return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
  336. }
  337. return nil
  338. },
  339. },
  340. {
  341. testCaseName: "test day_name with invalid date time arg",
  342. funcName: "day_name",
  343. execTest: false,
  344. valArgs: []ast.Expr{
  345. &ast.IntegerLiteral{Val: 1},
  346. },
  347. valFunc: func(t interface{}) error {
  348. expect := errors.New("Expect datetime type for parameter 1")
  349. if !reflect.DeepEqual(t, expect) {
  350. return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
  351. }
  352. return nil
  353. },
  354. },
  355. {
  356. testCaseName: "test day_of_month with 1 arg",
  357. funcName: "day_of_month",
  358. execTest: true,
  359. execArgs: []interface{}{"2019-01-01 00:00:00"},
  360. valFunc: func(t interface{}) error {
  361. if t.(int) != 1 {
  362. return fmt.Errorf("mismatch day of month, expect %d, got %d", 1, t.(int))
  363. }
  364. return nil
  365. },
  366. },
  367. {
  368. testCaseName: "test day_of_month with no arg",
  369. funcName: "day_of_month",
  370. execTest: false,
  371. valArgs: []ast.Expr{},
  372. valFunc: func(t interface{}) error {
  373. expect := errors.New("Expect 1 arguments but found 0.")
  374. if !reflect.DeepEqual(t, expect) {
  375. return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
  376. }
  377. return nil
  378. },
  379. },
  380. {
  381. testCaseName: "test day_of_month with invalid date time arg",
  382. funcName: "day_of_month",
  383. execTest: false,
  384. valArgs: []ast.Expr{
  385. &ast.IntegerLiteral{Val: 1},
  386. },
  387. valFunc: func(t interface{}) error {
  388. expect := errors.New("Expect datetime type for parameter 1")
  389. if !reflect.DeepEqual(t, expect) {
  390. return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
  391. }
  392. return nil
  393. },
  394. },
  395. {
  396. testCaseName: "test day_of_week with 1 arg",
  397. funcName: "day_of_week",
  398. execTest: true,
  399. execArgs: []interface{}{"2019-01-01 00:00:00"},
  400. valFunc: func(t interface{}) error {
  401. if t.(time.Weekday) != time.Tuesday {
  402. return fmt.Errorf("mismatch day of week, expect %d, got %d", time.Tuesday, t.(time.Weekday))
  403. }
  404. return nil
  405. },
  406. },
  407. {
  408. testCaseName: "test day_of_week with no arg",
  409. funcName: "day_of_week",
  410. execTest: false,
  411. valArgs: []ast.Expr{},
  412. valFunc: func(t interface{}) error {
  413. expect := errors.New("Expect 1 arguments but found 0.")
  414. if !reflect.DeepEqual(t, expect) {
  415. return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
  416. }
  417. return nil
  418. },
  419. },
  420. {
  421. testCaseName: "test day_of_week with invalid date time arg",
  422. funcName: "day_of_week",
  423. execTest: false,
  424. valArgs: []ast.Expr{
  425. &ast.IntegerLiteral{Val: 1},
  426. },
  427. valFunc: func(t interface{}) error {
  428. expect := errors.New("Expect datetime type for parameter 1")
  429. if !reflect.DeepEqual(t, expect) {
  430. return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
  431. }
  432. return nil
  433. },
  434. },
  435. {
  436. testCaseName: "test day_of_year with 1 arg",
  437. funcName: "day_of_year",
  438. execTest: true,
  439. execArgs: []interface{}{"2019-01-01 00:00:00"},
  440. valFunc: func(t interface{}) error {
  441. if t.(int) != 1 {
  442. return fmt.Errorf("mismatch day of year, expect %d, got %d", 1, t.(int))
  443. }
  444. return nil
  445. },
  446. },
  447. {
  448. testCaseName: "test day_of_year with no arg",
  449. funcName: "day_of_year",
  450. execTest: false,
  451. valArgs: []ast.Expr{},
  452. valFunc: func(t interface{}) error {
  453. expect := errors.New("Expect 1 arguments but found 0.")
  454. if !reflect.DeepEqual(t, expect) {
  455. return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
  456. }
  457. return nil
  458. },
  459. },
  460. {
  461. testCaseName: "test day_of_year with invalid date time arg",
  462. funcName: "day_of_year",
  463. execTest: false,
  464. valArgs: []ast.Expr{
  465. &ast.IntegerLiteral{Val: 1},
  466. },
  467. valFunc: func(t interface{}) error {
  468. expect := errors.New("Expect datetime type for parameter 1")
  469. if !reflect.DeepEqual(t, expect) {
  470. return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
  471. }
  472. return nil
  473. },
  474. },
  475. {
  476. testCaseName: "test from_days with no arg",
  477. funcName: "from_days",
  478. execTest: false,
  479. valArgs: []ast.Expr{},
  480. valFunc: func(t interface{}) error {
  481. expect := errors.New("Expect 1 arguments but found 0.")
  482. if !reflect.DeepEqual(t, expect) {
  483. return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
  484. }
  485. return nil
  486. },
  487. },
  488. {
  489. testCaseName: "test from_days with invalid date time arg",
  490. funcName: "from_days",
  491. execTest: false,
  492. valArgs: []ast.Expr{
  493. &ast.IntegerLiteral{Val: 1},
  494. },
  495. valFunc: func(t interface{}) error {
  496. expect := errors.New("Expect int type for parameter 1")
  497. if !reflect.DeepEqual(t, expect) {
  498. return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
  499. }
  500. return nil
  501. },
  502. },
  503. {
  504. testCaseName: "test from_days with 100",
  505. funcName: "from_days",
  506. execTest: true,
  507. execArgs: []interface{}{100},
  508. valFunc: func(t interface{}) error {
  509. if t.(string) != "1970-04-10" {
  510. return fmt.Errorf("mismatch date, expect %s, got %s", "2019-01-01", t.(string))
  511. }
  512. return nil
  513. },
  514. },
  515. {
  516. testCaseName: "test from_unix_time with no arg",
  517. funcName: "from_unix_time",
  518. execTest: false,
  519. valArgs: []ast.Expr{},
  520. valFunc: func(t interface{}) error {
  521. expect := errors.New("Expect 1 arguments but found 0.")
  522. if !reflect.DeepEqual(t, expect) {
  523. return fmt.Errorf("mismatch error, expect: %s, got: %s", expect, t)
  524. }
  525. return nil
  526. },
  527. },
  528. {
  529. testCaseName: "test from_unix_time with invalid arg",
  530. funcName: "from_unix_time",
  531. execTest: false,
  532. valArgs: []ast.Expr{
  533. &ast.NumberLiteral{Val: 0.1},
  534. },
  535. valFunc: func(t interface{}) error {
  536. expect := errors.New("Expect int type for parameter 1")
  537. if !reflect.DeepEqual(t, expect) {
  538. return fmt.Errorf("mismatch error, expect: %s, got: %v", expect, t)
  539. }
  540. return nil
  541. },
  542. },
  543. {
  544. testCaseName: "test from_unix_time with 100",
  545. funcName: "from_unix_time",
  546. execTest: true,
  547. execArgs: []interface{}{100},
  548. valFunc: func(t interface{}) error {
  549. expect := time.Unix(100, 0)
  550. expectStr, err := cast.FormatTime(expect, "yyyy-MM-dd HH:mm:ss")
  551. if err != nil {
  552. return err
  553. }
  554. if t.(string) != expectStr {
  555. return fmt.Errorf("mismatch date, expect %s, got %s", expectStr, t.(string))
  556. }
  557. return nil
  558. },
  559. },
  560. {
  561. testCaseName: "test hour with no arg",
  562. funcName: "hour",
  563. execTest: false,
  564. valArgs: []ast.Expr{},
  565. valFunc: func(t interface{}) error {
  566. expect := errors.New("Expect 1 arguments but found 0.")
  567. if !reflect.DeepEqual(t, expect) {
  568. return fmt.Errorf("mismatch error, expect: %s, got: %v", expect, t)
  569. }
  570. return nil
  571. },
  572. },
  573. {
  574. testCaseName: "test hour with invalid date time arg",
  575. funcName: "hour",
  576. execTest: false,
  577. valArgs: []ast.Expr{
  578. &ast.IntegerLiteral{Val: 1},
  579. },
  580. valFunc: func(t interface{}) error {
  581. expect := errors.New("Expect datetime type for parameter 1")
  582. if !reflect.DeepEqual(t, expect) {
  583. return fmt.Errorf("mismatch error, expect: %s, got: %v", expect, t)
  584. }
  585. return nil
  586. },
  587. },
  588. {
  589. testCaseName: "test hour with 1 arg",
  590. funcName: "hour",
  591. execTest: true,
  592. execArgs: []interface{}{"2019-01-01 01:00:00"},
  593. valFunc: func(t interface{}) error {
  594. if t.(int) != 1 {
  595. return fmt.Errorf("mismatch hour, expect %d, got %d", 1, t.(int))
  596. }
  597. return nil
  598. },
  599. },
  600. {
  601. testCaseName: "test last_day with no arg",
  602. funcName: "last_day",
  603. execTest: false,
  604. valArgs: []ast.Expr{},
  605. valFunc: func(t interface{}) error {
  606. expect := errors.New("Expect 1 arguments but found 0.")
  607. if !reflect.DeepEqual(t, expect) {
  608. return fmt.Errorf("mismatch error, expect: %s, got: %v", expect, t)
  609. }
  610. return nil
  611. },
  612. },
  613. {
  614. testCaseName: "test last_day with invalid date time arg",
  615. funcName: "last_day",
  616. execTest: false,
  617. valArgs: []ast.Expr{
  618. &ast.IntegerLiteral{Val: 1},
  619. },
  620. valFunc: func(t interface{}) error {
  621. expect := errors.New("Expect datetime type for parameter 1")
  622. if !reflect.DeepEqual(t, expect) {
  623. return fmt.Errorf("mismatch error, expect: %s, got: %v", expect, t)
  624. }
  625. return nil
  626. },
  627. },
  628. {
  629. testCaseName: "test last_day with 1 arg",
  630. funcName: "last_day",
  631. execTest: true,
  632. execArgs: []interface{}{"2019-01-01 01:00:00"},
  633. valFunc: func(t interface{}) error {
  634. if t.(string) != "2019-01-31" {
  635. return fmt.Errorf("mismatch date, expect %s, got %s", "2019-01-31", t.(string))
  636. }
  637. return nil
  638. },
  639. },
  640. {
  641. testCaseName: "test microsecond with no arg",
  642. funcName: "microsecond",
  643. execTest: false,
  644. valArgs: []ast.Expr{},
  645. valFunc: func(t interface{}) error {
  646. expect := errors.New("Expect 1 arguments but found 0.")
  647. if !reflect.DeepEqual(t, expect) {
  648. return fmt.Errorf("mismatch error, expect: %s, got: %v", expect, t)
  649. }
  650. return nil
  651. },
  652. },
  653. {
  654. testCaseName: "test microsecond with invalid date time arg",
  655. funcName: "microsecond",
  656. execTest: false,
  657. valArgs: []ast.Expr{
  658. &ast.IntegerLiteral{Val: 1},
  659. },
  660. valFunc: func(t interface{}) error {
  661. expect := errors.New("Expect datetime type for parameter 1")
  662. if !reflect.DeepEqual(t, expect) {
  663. return fmt.Errorf("mismatch error, expect: %s, got: %v", expect, t)
  664. }
  665. return nil
  666. },
  667. },
  668. {
  669. testCaseName: "test microsecond with 1 arg",
  670. funcName: "microsecond",
  671. execTest: true,
  672. execArgs: []interface{}{"2019-01-01 01:00:00.123456"},
  673. valFunc: func(t interface{}) error {
  674. if t.(int) != 123456 {
  675. return fmt.Errorf("mismatch microsecond, expect %d, got %d", 123456, t.(int))
  676. }
  677. return nil
  678. },
  679. },
  680. {
  681. testCaseName: "test minute with no arg",
  682. funcName: "minute",
  683. execTest: false,
  684. valArgs: []ast.Expr{},
  685. valFunc: func(t interface{}) error {
  686. expect := errors.New("Expect 1 arguments but found 0.")
  687. if !reflect.DeepEqual(t, expect) {
  688. return fmt.Errorf("mismatch error, expect: %s, got: %v", expect, t)
  689. }
  690. return nil
  691. },
  692. },
  693. {
  694. testCaseName: "test minute with invalid date time arg",
  695. funcName: "minute",
  696. execTest: false,
  697. valArgs: []ast.Expr{
  698. &ast.IntegerLiteral{Val: 1},
  699. },
  700. valFunc: func(t interface{}) error {
  701. expect := errors.New("Expect datetime type for parameter 1")
  702. if !reflect.DeepEqual(t, expect) {
  703. return fmt.Errorf("mismatch error, expect: %s, got: %v", expect, t)
  704. }
  705. return nil
  706. },
  707. },
  708. {
  709. testCaseName: "test minute with 1 arg",
  710. funcName: "minute",
  711. execTest: true,
  712. execArgs: []interface{}{"2019-01-01 01:23:45"},
  713. valFunc: func(t interface{}) error {
  714. if t.(int) != 23 {
  715. return fmt.Errorf("mismatch minute, expect %d, got %d", 23, t.(int))
  716. }
  717. return nil
  718. },
  719. },
  720. {
  721. testCaseName: "test month with no arg",
  722. funcName: "month",
  723. execTest: false,
  724. valArgs: []ast.Expr{},
  725. valFunc: func(t interface{}) error {
  726. expect := errors.New("Expect 1 arguments but found 0.")
  727. if !reflect.DeepEqual(t, expect) {
  728. return fmt.Errorf("mismatch error, expect: %s, got: %v", expect, t)
  729. }
  730. return nil
  731. },
  732. },
  733. {
  734. testCaseName: "test month with invalid date time arg",
  735. funcName: "month",
  736. execTest: false,
  737. valArgs: []ast.Expr{
  738. &ast.IntegerLiteral{Val: 1},
  739. },
  740. valFunc: func(t interface{}) error {
  741. expect := errors.New("Expect datetime type for parameter 1")
  742. if !reflect.DeepEqual(t, expect) {
  743. return fmt.Errorf("mismatch error, expect: %s, got %v", expect, t)
  744. }
  745. return nil
  746. },
  747. },
  748. {
  749. testCaseName: "test month with 1 arg",
  750. funcName: "month",
  751. execTest: true,
  752. execArgs: []interface{}{"2019-01-01 01:23:45"},
  753. valFunc: func(t interface{}) error {
  754. if t.(int) != 1 {
  755. return fmt.Errorf("mismatch month, expect %d, got %d", 1, t.(int))
  756. }
  757. return nil
  758. },
  759. },
  760. {
  761. testCaseName: "test month_name with no arg",
  762. funcName: "month_name",
  763. execTest: false,
  764. valArgs: []ast.Expr{},
  765. valFunc: func(t interface{}) error {
  766. expect := errors.New("Expect 1 arguments but found 0.")
  767. if !reflect.DeepEqual(t, expect) {
  768. return fmt.Errorf("mismatch error, expect %s, got %v", expect, t)
  769. }
  770. return nil
  771. },
  772. },
  773. {
  774. testCaseName: "test month_name with invalid date time arg",
  775. funcName: "month_name",
  776. execTest: false,
  777. valArgs: []ast.Expr{
  778. &ast.IntegerLiteral{Val: 1},
  779. },
  780. valFunc: func(t interface{}) error {
  781. expect := errors.New("Expect datetime type for parameter 1")
  782. if !reflect.DeepEqual(t, t) {
  783. return fmt.Errorf("mismatch error, expect %s, got %v", expect, t)
  784. }
  785. return nil
  786. },
  787. },
  788. {
  789. testCaseName: "test month_name with 1 arg",
  790. funcName: "month_name",
  791. execTest: true,
  792. execArgs: []interface{}{"2019-01-01 01:23:45"},
  793. valFunc: func(t interface{}) error {
  794. if t.(string) != "January" {
  795. return fmt.Errorf("mismatch month name, expect %s, got %s", "January", t.(string))
  796. }
  797. return nil
  798. },
  799. },
  800. {
  801. testCaseName: "test second with no arg",
  802. funcName: "second",
  803. execTest: false,
  804. valArgs: []ast.Expr{},
  805. valFunc: func(t interface{}) error {
  806. expect := errors.New("Expect 1 arguments but found 0.")
  807. if !reflect.DeepEqual(t, t) {
  808. return fmt.Errorf("mismatch error, expect %s, got %v", expect, t)
  809. }
  810. return nil
  811. },
  812. },
  813. {
  814. testCaseName: "test second with invalid date time arg",
  815. funcName: "second",
  816. execTest: false,
  817. valArgs: []ast.Expr{
  818. &ast.IntegerLiteral{Val: 1},
  819. },
  820. valFunc: func(t interface{}) error {
  821. expect := errors.New("Expect datetime type for parameter 1")
  822. if !reflect.DeepEqual(t, t) {
  823. return fmt.Errorf("mismatch error, expect %s, got %v", expect, t)
  824. }
  825. return nil
  826. },
  827. },
  828. {
  829. testCaseName: "test second with 1 arg",
  830. funcName: "second",
  831. execTest: true,
  832. execArgs: []interface{}{"2019-01-01 01:23:45"},
  833. valFunc: func(t interface{}) error {
  834. if t.(int) != 45 {
  835. return fmt.Errorf("mismatch second, expect %d, got %d", 45, t.(int))
  836. }
  837. return nil
  838. },
  839. },
  840. }
  841. for _, test := range tests {
  842. f, ok := builtins[test.funcName]
  843. if !ok {
  844. t.Fatalf("builtin '%s' not found", test.funcName)
  845. }
  846. var result interface{}
  847. if test.execTest {
  848. result, _ = f.exec(fctx, test.execArgs)
  849. } else {
  850. result = f.val(fctx, test.valArgs)
  851. }
  852. if err := test.valFunc(result); err != nil {
  853. t.Errorf("\n%s: %q", test.testCaseName, err)
  854. }
  855. }
  856. }