Skip to content

Commit

Permalink
Add more varchar to date conversion tests
Browse files Browse the repository at this point in the history
Add test cases for non-canonically formatted input, whitespace,
zero-padding, etc.
  • Loading branch information
findepi committed Jun 28, 2022
1 parent caa6ed6 commit a094360
Showing 1 changed file with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,24 @@ public void testIsDistinctFrom()
public void testDateLiteral()
{
assertFunction("DATE '2013-02-02'", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC)));
// one digit for month or day
assertFunction("DATE '2013-2-02'", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC)));
assertFunction("DATE '2013-02-2'", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC)));
// three digit for month or day
assertInvalidFunction("DATE '2013-02-002'", INVALID_LITERAL, "line 1:1: '2013-02-002' is not a valid date literal");
assertInvalidFunction("DATE '2013-002-02'", INVALID_LITERAL, "line 1:1: '2013-002-02' is not a valid date literal");
// zero-padded year
assertFunction("DATE '02013-02-02'", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC)));
assertFunction("DATE '0013-02-02'", DATE, toDate(new DateTime(13, 2, 2, 0, 0, 0, 0, UTC)));
// invalid date
assertInvalidFunction("DATE '2013-02-29'", INVALID_LITERAL, "line 1:1: '2013-02-29' is not a valid date literal");
// surrounding whitespace
assertFunction("DATE ' 2013-02-02 '", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC)));
assertFunction("DATE ' \t\n\u000B\f\r\u001C\u001D\u001E\u001F 2013-02-02 \t\n\u000B\f\n\u001C\u001D\u001E\u001F '", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC)));
// intra whitespace
assertInvalidFunction("DATE '2013 -02-02'", INVALID_LITERAL, "line 1:1: '2013 -02-02' is not a valid date literal");
assertInvalidFunction("DATE '2013- 2-02'", INVALID_LITERAL, "line 1:1: '2013- 2-02' is not a valid date literal");
// large year
assertInvalidFunction("DATE '5881580-07-12'", INVALID_LITERAL, "line 1:1: '5881580-07-12' is not a valid date literal");
assertInvalidFunction("DATE '392251590-07-12'", INVALID_LITERAL, "line 1:1: '392251590-07-12' is not a valid date literal");
}
Expand All @@ -276,6 +294,24 @@ public void testDateLiteral()
public void testDateCastFromVarchar()
{
assertFunction("CAST('2013-02-02' AS date)", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC)));
// one digit for month or day
assertFunction("CAST('2013-2-02' AS date)", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC)));
assertFunction("CAST('2013-02-2' AS date)", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC)));
// three digit for month or day
assertInvalidFunction("CAST('2013-02-002' AS date)", INVALID_CAST_ARGUMENT, "Value cannot be cast to date: 2013-02-002");
assertInvalidFunction("CAST('2013-002-02' AS date)", INVALID_CAST_ARGUMENT, "Value cannot be cast to date: 2013-002-02");
// zero-padded year
assertFunction("CAST('02013-02-02' AS date)", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC)));
assertFunction("CAST('0013-02-02' AS date)", DATE, toDate(new DateTime(13, 2, 2, 0, 0, 0, 0, UTC)));
// invalid date
assertInvalidFunction("CAST('2013-02-29' AS date)", INVALID_CAST_ARGUMENT, "Value cannot be cast to date: 2013-02-29");
// surrounding whitespace
assertFunction("CAST(' 2013-02-02 ' AS date)", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC)));
assertFunction("CAST(' \t\n\u000B\f\r\u001C\u001D\u001E\u001F 2013-02-02 \t\n\u000B\f\n\u001C\u001D\u001E\u001F ' AS date)", DATE, toDate(new DateTime(2013, 2, 2, 0, 0, 0, 0, UTC)));
// intra whitespace
assertInvalidFunction("CAST('2013 -02-02' AS date)", INVALID_CAST_ARGUMENT, "Value cannot be cast to date: 2013 -02-02");
assertInvalidFunction("CAST('2013- 2-02' AS date)", INVALID_CAST_ARGUMENT, "Value cannot be cast to date: 2013- 2-02");
// large year
assertInvalidFunction("CAST('5881580-07-12' AS date)", INVALID_CAST_ARGUMENT, "Value cannot be cast to date: 5881580-07-12");
assertInvalidFunction("CAST('392251590-07-12' AS date)", INVALID_CAST_ARGUMENT, "Value cannot be cast to date: 392251590-07-12");
}
Expand Down

0 comments on commit a094360

Please sign in to comment.