villebro commented on code in PR #32781:
URL: https://github.com/apache/superset/pull/32781#discussion_r2006536341
##########
tests/unit_tests/jinja_context_test.py:
##########
@@ -429,6 +431,54 @@ def test_where_in_empty_list() -> None:
assert where_in([], default_to_none=True) is None
+def test_to_datetime() -> None:
+ """
+ Test the ``to_datetime`` Jinja2 filter.
+ """
+
+ result = to_datetime("2025-03-20 15:55:00")
+ assert result == datetime(2025, 3, 20, 15, 55)
+
+ assert to_datetime(None) is None
+
+
+def test_to_datetime_custom_format() -> None:
+ """
+ Test the ``to_datetime`` Jinja2 filter when specifying a format.
+ """
+ result = to_datetime("2025-03-20", format="%Y-%m-%d")
+ assert result == datetime(2025, 3, 20)
+
+
+def test_to_datetime_including_quotes() -> None:
+ """
+ Test the ``to_datetime`` Jinja2 when a string wrapped
+ in quotes is used.
+
+ This might happen when passing a value from a temporal macro.
+ """
+ result = to_datetime("'2025-03-20'", format="%Y-%m-%d")
+ assert result == datetime(2025, 3, 20)
+
+
+def test_to_datetime_raises() -> None:
+ """
+ Test the ``to_datetime`` Jinja2 raises with an incorrect
+ format.
+ """
+ with pytest.raises(
+ ValueError,
+ match="time data '2025-03-20' does not match format '%Y-%m-%d
%H:%M:%S'",
+ ):
+ to_datetime("2025-03-20")
+
+ with pytest.raises(
+ ValueError,
+ match="unconverted data remains: 15:55:00",
+ ):
+ to_datetime("2025-03-20 15:55:00", format="%Y-%m-%d")
+
+
Review Comment:
I'm a big fan of `pytest.mark.parametrize`, I think it would sit well here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]