From fad878b5bc0ddfc6d82013eeb5255b5f190d63b8 Mon Sep 17 00:00:00 2001 From: Mauro Schiavinato Date: Sun, 4 Aug 2024 17:05:25 -0300 Subject: [PATCH] fix weekday offset exception; add tests --- changedetectionio/jinja_extensions.py | 5 ++++ changedetectionio/tests/test_jinja2.py | 36 +++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/changedetectionio/jinja_extensions.py b/changedetectionio/jinja_extensions.py index 67dd2744..b5efe336 100644 --- a/changedetectionio/jinja_extensions.py +++ b/changedetectionio/jinja_extensions.py @@ -24,6 +24,11 @@ class TimeExtension(Extension): for param in offset.split(','): interval, value = param.split('=') shift_params[interval.strip()] = float(operator + value.strip()) + + # Fix weekday parameter can not be float + if 'weekday' in shift_params: + shift_params['weekday'] = int(shift_params['weekday']) + d = d.shift(**shift_params) if datetime_format is None: diff --git a/changedetectionio/tests/test_jinja2.py b/changedetectionio/tests/test_jinja2.py index b5cba3b0..6fc024db 100644 --- a/changedetectionio/tests/test_jinja2.py +++ b/changedetectionio/tests/test_jinja2.py @@ -59,6 +59,29 @@ def test_jinja2_security_url_query(client, live_server, measure_memory_usage): # Some of the spewed output from the subclasses assert b'dict_values' not in res.data +def test_timezone(mocker): + """Verify that timezone is parsed.""" + + timezone = 'America/Buenos Aires' + currentDate = arrow.now(timezone) + arrowNowMock = mocker.patch("arrow.now") + arrowNowMock.return_value = currentDate + finalRender = render(f"{{% now '{timezone}' %}}") + + assert finalRender == currentDate.strftime("%Y-%m-%d") + +def test_format(mocker): + """Verify that format is parsed.""" + + timezone = 'utc' + format = '%d %b %Y %H:%M:%S' + currentDate = arrow.now(timezone) + arrowNowMock = mocker.patch("arrow.now") + arrowNowMock.return_value = currentDate + finalRender = render(f"{{% now '{timezone}', '{format}' %}}") + + assert finalRender == currentDate.strftime(format) + def test_add_time(mocker): """Verify that added time offset can be parsed.""" @@ -70,6 +93,17 @@ def test_add_time(mocker): assert finalRender == currentDate.strftime("%Y-%m-%d") +def test_add_weekday(mocker): + """Verify that added weekday offset can be parsed.""" + + timezone = 'utc' + currentDate = arrow.now(timezone) + arrowNowMock = mocker.patch("arrow.now") + arrowNowMock.return_value = currentDate + finalRender = render(f"{{% now '{timezone}' + 'weekday=1' %}}") + + assert finalRender == currentDate.shift(weekday=1).strftime('%Y-%m-%d') + def test_substract_time(mocker): """Verify that substracted time offset can be parsed.""" @@ -92,7 +126,7 @@ def test_offset_with_format(mocker): arrowNowMock.return_value = currentDate format = '%d %b %Y %H:%M:%S' finalRender = render( - f"{{% now '{timezone}' - 'days=2,minutes=33,seconds=1', '%d %b %Y %H:%M:%S' %}}" + f"{{% now '{timezone}' - 'days=2,minutes=33,seconds=1', '{format}' %}}" ) assert finalRender == currentDate.shift(days=-2, minutes=-33, seconds=-1).strftime(format) \ No newline at end of file