Skip to content

Commit

Permalink
fix issue in naturaltime test due to humanize lib adding a year if da…
Browse files Browse the repository at this point in the history
…te is older than 5 months (#211)

Co-authored-by: Nicolas Höning <nicolas@seita.nl>
  • Loading branch information
Flix6x and nhoening committed Oct 14, 2021
1 parent fe10cef commit 56c7857
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions flexmeasures/utils/tests/test_time_utils.py
Expand Up @@ -12,11 +12,25 @@

@pytest.mark.parametrize(
"dt_tz,now,server_tz,delta_in_h,exp_result",
# there can be two results depending of today's date, due to humanize.
# Monekypatching was too hard.
[
(None, datetime.utcnow(), "UTC", 3, "3 hours ago"),
(None, datetime(2021, 5, 17, 3), "Europe/Amsterdam", 48, "May 15"),
(
None,
datetime(2021, 5, 17, 3),
"Europe/Amsterdam",
48,
("May 15", "May 15 2021"),
),
("Asia/Seoul", "server_now", "Europe/Amsterdam", 1, "an hour ago"),
("UTC", datetime(2021, 5, 17, 3), "Asia/Seoul", 24 * 7, "May 10"),
(
"UTC",
datetime(2021, 5, 17, 3),
"Asia/Seoul",
24 * 7,
("May 10", "May 10 2021"),
),
("UTC", datetime(2021, 5, 17, 3), "Asia/Seoul", None, "never"),
],
)
Expand All @@ -31,17 +45,19 @@ def test_naturalized_datetime_str(
):
monkeypatch.setitem(app.config, "FLEXMEASURES_TIMEZONE", server_tz)
if now == "server_now":
now = server_now() # done this way as it needs app context
now = server_now() # done this way as it needs (patched) app context
if now.tzinfo is None:
now.replace(tzinfo=pytz.utc) # assuming UTC
now = now.replace(tzinfo=pytz.utc) # assuming UTC
if delta_in_h is not None:
h_ago = now - timedelta(hours=delta_in_h)
if dt_tz is not None:
h_ago = h_ago.astimezone(pytz.timezone(dt_tz))
else:
h_ago = None
print(h_ago)
assert naturalized_datetime_str(h_ago, now=now) == exp_result
if isinstance(exp_result, tuple):
assert naturalized_datetime_str(h_ago, now=now) in exp_result
else:
assert naturalized_datetime_str(h_ago, now=now) == exp_result


@pytest.mark.parametrize(
Expand Down

0 comments on commit 56c7857

Please sign in to comment.