@@ -435,32 +435,150 @@ public void dayOfMonth() {
435435 assertEquals (integerValue (8 ), eval (expression ));
436436 }
437437
438+ private void dayOfWeekQuery (
439+ FunctionExpression dateExpression ,
440+ int dayOfWeek ,
441+ String testExpr ) {
442+ assertEquals (INTEGER , dateExpression .type ());
443+ assertEquals (integerValue (dayOfWeek ), eval (dateExpression ));
444+ assertEquals (testExpr , dateExpression .toString ());
445+ }
446+
438447 @ Test
439448 public void dayOfWeek () {
449+ lenient ().when (nullRef .valueOf (env )).thenReturn (nullValue ());
450+ lenient ().when (missingRef .valueOf (env )).thenReturn (missingValue ());
451+
452+ FunctionExpression expression1 = DSL .dayofweek (
453+ functionProperties ,
454+ DSL .literal (new ExprDateValue ("2020-08-07" )));
455+ FunctionExpression expression2 = DSL .dayofweek (
456+ functionProperties ,
457+ DSL .literal (new ExprDateValue ("2020-08-09" )));
458+ FunctionExpression expression3 = DSL .dayofweek (
459+ functionProperties ,
460+ DSL .literal ("2020-08-09" ));
461+ FunctionExpression expression4 = DSL .dayofweek (
462+ functionProperties ,
463+ DSL .literal ("2020-08-09 01:02:03" ));
464+
465+ assertAll (
466+ () -> dayOfWeekQuery (expression1 , 6 , "dayofweek(DATE '2020-08-07')" ),
467+
468+ () -> dayOfWeekQuery (expression2 , 1 , "dayofweek(DATE '2020-08-09')" ),
469+
470+ () -> dayOfWeekQuery (expression3 , 1 , "dayofweek(\" 2020-08-09\" )" ),
471+
472+ () -> dayOfWeekQuery (expression4 , 1 , "dayofweek(\" 2020-08-09 01:02:03\" )" )
473+ );
474+ }
475+
476+ private void dayOfWeekWithUnderscoresQuery (
477+ FunctionExpression dateExpression ,
478+ int dayOfWeek ,
479+ String testExpr ) {
480+ assertEquals (INTEGER , dateExpression .type ());
481+ assertEquals (integerValue (dayOfWeek ), eval (dateExpression ));
482+ assertEquals (testExpr , dateExpression .toString ());
483+ }
484+
485+ @ Test
486+ public void dayOfWeekWithUnderscores () {
487+ lenient ().when (nullRef .valueOf (env )).thenReturn (nullValue ());
488+ lenient ().when (missingRef .valueOf (env )).thenReturn (missingValue ());
489+
490+ FunctionExpression expression1 = DSL .day_of_week (
491+ functionProperties ,
492+ DSL .literal (new ExprDateValue ("2020-08-07" )));
493+ FunctionExpression expression2 = DSL .day_of_week (
494+ functionProperties ,
495+ DSL .literal (new ExprDateValue ("2020-08-09" )));
496+ FunctionExpression expression3 = DSL .day_of_week (
497+ functionProperties ,
498+ DSL .literal ("2020-08-09" ));
499+ FunctionExpression expression4 = DSL .day_of_week (
500+ functionProperties ,
501+ DSL .literal ("2020-08-09 01:02:03" ));
502+
503+ assertAll (
504+ () -> dayOfWeekWithUnderscoresQuery (expression1 , 6 , "day_of_week(DATE '2020-08-07')" ),
505+
506+ () -> dayOfWeekWithUnderscoresQuery (expression2 , 1 , "day_of_week(DATE '2020-08-09')" ),
507+
508+ () -> dayOfWeekWithUnderscoresQuery (expression3 , 1 , "day_of_week(\" 2020-08-09\" )" ),
509+
510+ () -> dayOfWeekWithUnderscoresQuery (
511+ expression4 , 1 , "day_of_week(\" 2020-08-09 01:02:03\" )" )
512+ );
513+ }
514+
515+ @ Test
516+ public void testDayOfWeekWithTimeType () {
517+ lenient ().when (nullRef .valueOf (env )).thenReturn (nullValue ());
518+ lenient ().when (missingRef .valueOf (env )).thenReturn (missingValue ());
519+ FunctionExpression expression = DSL .day_of_week (
520+ functionProperties , DSL .literal (new ExprTimeValue ("12:23:34" )));
521+
522+ assertAll (
523+ () -> assertEquals (INTEGER , eval (expression ).type ()),
524+ () -> assertEquals ((
525+ LocalDate .now (
526+ functionProperties .getQueryStartClock ()).getDayOfWeek ().getValue () % 7 ) + 1 ,
527+ eval (expression ).integerValue ()),
528+ () -> assertEquals ("day_of_week(TIME '12:23:34')" , expression .toString ())
529+ );
530+ }
531+
532+ private void testInvalidDayOfWeek (String date ) {
533+ FunctionExpression expression = DSL .day_of_week (
534+ functionProperties , DSL .literal (new ExprDateValue (date )));
535+ eval (expression );
536+ }
537+
538+ @ Test
539+ public void dayOfWeekWithUnderscoresLeapYear () {
540+ lenient ().when (nullRef .valueOf (env )).thenReturn (nullValue ());
541+ lenient ().when (missingRef .valueOf (env )).thenReturn (missingValue ());
542+
543+ assertAll (
544+ //Feb. 29 of a leap year
545+ () -> dayOfWeekWithUnderscoresQuery (DSL .day_of_week (
546+ functionProperties ,
547+ DSL .literal ("2020-02-29" )), 7 , "day_of_week(\" 2020-02-29\" )" ),
548+ //day after Feb. 29 of a leap year
549+ () -> dayOfWeekWithUnderscoresQuery (DSL .day_of_week (
550+ functionProperties ,
551+ DSL .literal ("2020-03-01" )), 1 , "day_of_week(\" 2020-03-01\" )" ),
552+ //Feb. 28 of a non-leap year
553+ () -> dayOfWeekWithUnderscoresQuery (DSL .day_of_week (
554+ functionProperties ,
555+ DSL .literal ("2021-02-28" )), 1 , "day_of_week(\" 2021-02-28\" )" ),
556+ //Feb. 29 of a non-leap year
557+ () -> assertThrows (
558+ SemanticCheckException .class , () -> testInvalidDayOfWeek ("2021-02-29" ))
559+ );
560+ }
561+
562+ @ Test
563+ public void dayOfWeekWithUnderscoresInvalidArgument () {
440564 when (nullRef .type ()).thenReturn (DATE );
441565 when (missingRef .type ()).thenReturn (DATE );
442- assertEquals (nullValue (), eval (DSL .dayofweek (nullRef )));
443- assertEquals (missingValue (), eval (DSL .dayofweek (missingRef )));
444-
445- FunctionExpression expression = DSL .dayofweek (DSL .literal (new ExprDateValue ("2020-08-07" )));
446- assertEquals (INTEGER , expression .type ());
447- assertEquals ("dayofweek(DATE '2020-08-07')" , expression .toString ());
448- assertEquals (integerValue (6 ), eval (expression ));
566+ assertEquals (nullValue (), eval (DSL .day_of_week (functionProperties , nullRef )));
567+ assertEquals (missingValue (), eval (DSL .day_of_week (functionProperties , missingRef )));
449568
450- expression = DSL . dayofweek ( DSL . literal ( new ExprDateValue ( "2020-08-09" )));
451- assertEquals ( INTEGER , expression . type ());
452- assertEquals ( "dayofweek(DATE '2020-08-09')" , expression . toString ());
453- assertEquals ( integerValue ( 1 ), eval ( expression ));
569+ assertAll (
570+ //40th day of the month
571+ () -> assertThrows ( SemanticCheckException . class ,
572+ () -> testInvalidDayOfWeek ( "2021-02-40" )),
454573
455- expression = DSL .dayofweek (DSL .literal ("2020-08-09" ));
456- assertEquals (INTEGER , expression .type ());
457- assertEquals ("dayofweek(\" 2020-08-09\" )" , expression .toString ());
458- assertEquals (integerValue (1 ), eval (expression ));
574+ //13th month of the year
575+ () -> assertThrows (SemanticCheckException .class ,
576+ () -> testInvalidDayOfWeek ("2021-13-29" )),
459577
460- expression = DSL . dayofweek ( DSL . literal ( "2020-08-09 01:02:03" ));
461- assertEquals ( INTEGER , expression . type ());
462- assertEquals ( "dayofweek( \" 2020-08-09 01:02:03 \" )" , expression . toString ());
463- assertEquals ( integerValue ( 1 ), eval ( expression ) );
578+ //incorrect format
579+ () -> assertThrows ( SemanticCheckException . class ,
580+ () -> testInvalidDayOfWeek ( "asdfasdf" ))
581+ );
464582 }
465583
466584 @ Test
@@ -486,7 +604,7 @@ public void dayOfYear() {
486604 assertEquals (integerValue (220 ), eval (expression ));
487605 }
488606
489- public void testDayOfYearWithUnderscores (String date , int dayOfYear ) {
607+ private void testDayOfYearWithUnderscores (String date , int dayOfYear ) {
490608 FunctionExpression expression = DSL .day_of_year (DSL .literal (new ExprDateValue (date )));
491609 assertEquals (INTEGER , expression .type ());
492610 assertEquals (integerValue (dayOfYear ), eval (expression ));
@@ -553,7 +671,7 @@ public void dayOfYearWithUnderscoresLeapYear() {
553671 );
554672 }
555673
556- public void testInvalidDayOfYear (String date ) {
674+ private void testInvalidDayOfYear (String date ) {
557675 FunctionExpression expression = DSL .day_of_year (DSL .literal (new ExprDateValue (date )));
558676 eval (expression );
559677 }
@@ -871,7 +989,7 @@ public void month() {
871989 assertEquals (integerValue (8 ), eval (expression ));
872990 }
873991
874- public void testInvalidDates (String date ) throws SemanticCheckException {
992+ private void testInvalidDates (String date ) throws SemanticCheckException {
875993 FunctionExpression expression = DSL .month_of_year (DSL .literal (new ExprDateValue (date )));
876994 eval (expression );
877995 }
0 commit comments