From 56c7857a8ad24202c4079755dc70a6ffcb3affae Mon Sep 17 00:00:00 2001 From: Felix Claessen <30658763+Flix6x@users.noreply.github.com> Date: Thu, 14 Oct 2021 10:59:53 +0200 Subject: [PATCH] fix issue in naturaltime test due to humanize lib adding a year if date is older than 5 months (#211) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Nicolas Höning --- flexmeasures/utils/tests/test_time_utils.py | 28 ++++++++++++++++----- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/flexmeasures/utils/tests/test_time_utils.py b/flexmeasures/utils/tests/test_time_utils.py index f14d0a20f..0c15d68fd 100644 --- a/flexmeasures/utils/tests/test_time_utils.py +++ b/flexmeasures/utils/tests/test_time_utils.py @@ -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"), ], ) @@ -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(