From c0bf037d2e58775a1c2eb0cdf655b384b59916a0 Mon Sep 17 00:00:00 2001 From: Thomas LEVEIL Date: Fri, 21 Aug 2020 23:17:50 +0200 Subject: [PATCH 1/2] add tests for midnight in Paris --- tests/conftest.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++ tests/test_maya.py | 56 ++++++++++++++++++++++++++++------- 2 files changed, 119 insertions(+), 10 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 521ffdd..0ed1267 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,3 +1,6 @@ +import datetime + +import pytz from freezegun import freeze_time import pytest @@ -23,3 +26,73 @@ def frozen_now(request): now_string, tz_offset = request.param with freeze_time(now_string, tz_offset=tz_offset): yield + + +@pytest.fixture(params=[ + datetime.datetime(2020, 8, 10, 22, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 8, 10, 23, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 8, 11, 0, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 8, 11, 1, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 8, 11, 2, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 8, 11, 3, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 8, 11, 4, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 8, 11, 5, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 8, 11, 6, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 8, 11, 7, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 8, 11, 8, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 8, 11, 9, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 8, 11, 10, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 8, 11, 11, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 8, 11, 12, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 8, 11, 13, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 8, 11, 14, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 8, 11, 15, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 8, 11, 16, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 8, 11, 17, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 8, 11, 18, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 8, 11, 19, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 8, 11, 20, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 8, 11, 21, 2, 0, tzinfo=pytz.timezone('UTC')), +], ids=str) +def frozen_2020_08_11_in_paris(request): + """ + fixture setting datetime.now() to every hour of the 11th of august 2020 in Paris + (summer time, GMT+2) + """ + with freeze_time(request.param): + yield + + +@pytest.fixture(params=[ + datetime.datetime(2020, 2, 10, 23, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 2, 11, 0, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 2, 11, 1, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 2, 11, 2, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 2, 11, 3, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 2, 11, 4, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 2, 11, 5, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 2, 11, 6, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 2, 11, 7, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 2, 11, 8, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 2, 11, 9, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 2, 11, 10, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 2, 11, 11, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 2, 11, 12, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 2, 11, 13, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 2, 11, 14, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 2, 11, 15, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 2, 11, 16, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 2, 11, 17, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 2, 11, 18, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 2, 11, 19, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 2, 11, 20, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 2, 11, 21, 2, 0, tzinfo=pytz.timezone('UTC')), + datetime.datetime(2020, 2, 11, 22, 2, 0, tzinfo=pytz.timezone('UTC')), +], ids=str) +def frozen_2020_02_11_in_paris(request): + """ + fixture setting datetime.now() to every hour of the 11th of february 2020 in Paris + (winter time, GMT+1) + """ + with freeze_time(request.param): + yield diff --git a/tests/test_maya.py b/tests/test_maya.py index a528309..e43dba1 100644 --- a/tests/test_maya.py +++ b/tests/test_maya.py @@ -93,6 +93,42 @@ def test_human_when(): assert (r2.day - r1.day) in (1, -30, -29, -28, -27) +@pytest.mark.usefixtures("frozen_2020_08_11_in_paris") +def test_human_when_today_with_timezone_summer_time(): + d = maya.when("today", timezone='Europe/Paris') + assert str(d.datetime(to_timezone="Europe/Paris").date()) == '2020-08-11' + + +@pytest.mark.usefixtures("frozen_2020_02_11_in_paris") +def test_human_when_today_with_timezone_winter_time(): + d = maya.when("today", timezone='Europe/Paris') + assert str(d.datetime(to_timezone="Europe/Paris").date()) == '2020-02-11' + + +@pytest.mark.usefixtures("frozen_2020_08_11_in_paris") +def test_human_when_yesterday_with_timezone_summer_time(): + d = maya.when("yesterday", timezone='Europe/Paris') + assert str(d.datetime(to_timezone="Europe/Paris").date()) == '2020-08-10' + + +@pytest.mark.usefixtures("frozen_2020_02_11_in_paris") +def test_human_when_yesterday_with_timezone_winter_time(): + d = maya.when("yesterday", timezone='Europe/Paris') + assert str(d.datetime(to_timezone="Europe/Paris").date()) == '2020-02-10' + + +@pytest.mark.usefixtures("frozen_2020_08_11_in_paris") +def test_human_when_midnight_with_timezone_summer_time(): + d = maya.when("midnight", timezone='Europe/Paris') + assert str(d.datetime(to_timezone="Europe/Paris")) == '2020-08-11 00:00:00+02:00' + + +@pytest.mark.usefixtures("frozen_2020_02_11_in_paris") +def test_human_when_midnight_with_timezone_winter_time(): + d = maya.when("midnight", timezone='Europe/Paris') + assert str(d.datetime(to_timezone="Europe/Paris")) == '2020-02-11 00:00:00+01:00' + + def test_machine_parse(): r1 = maya.parse("August 14, 2015") assert r1.day == 14 @@ -181,14 +217,14 @@ def test_slang_time_locale(): ("01/05/2016", {}, "2016-01-05 00:00:00+00:00"), ("01/05/2016", dict(day_first=True), "2016-05-01 00:00:00+00:00"), ( - "2016/05/01", - dict(year_first=True, day_first=False), - "2016-05-01 00:00:00+00:00", + "2016/05/01", + dict(year_first=True, day_first=False), + "2016-05-01 00:00:00+00:00", ), ( - "2016/01/05", - dict(year_first=True, day_first=True), - "2016-05-01 00:00:00+00:00", + "2016/01/05", + dict(year_first=True, day_first=True), + "2016-05-01 00:00:00+00:00", ), ("01/05/2016", dict(timezone="UTC"), "2016-01-05 00:00:00+00:00"), ("01/05/2016", dict(timezone="US/Central"), "2016-01-05 06:00:00+00:00"), @@ -389,10 +425,10 @@ def test_snaptime(when_str, snap_str, expected_when): "when_str,snap_str,timezone,expected_when", [ ( - "Mon, 21 Feb 1994 21:21:42 GMT", - "@d", - "Australia/Perth", - "Mon, 21 Feb 1994 16:00:00 GMT", + "Mon, 21 Feb 1994 21:21:42 GMT", + "@d", + "Australia/Perth", + "Mon, 21 Feb 1994 16:00:00 GMT", ) ], ) From 882a6aead7a7bf269ba2a0a888c099b3bc4b9455 Mon Sep 17 00:00:00 2001 From: Thomas LEVEIL Date: Fri, 21 Aug 2020 23:24:42 +0200 Subject: [PATCH 2/2] flake8 fixes --- src/maya/core.py | 6 +++--- tests/test_maya.py | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/maya/core.py b/src/maya/core.py index 1b5eb8d..6d59ccf 100644 --- a/src/maya/core.py +++ b/src/maya/core.py @@ -294,15 +294,15 @@ def long_count(self): """Returns a Mayan Long Count representation of the Maya DT.""" # Creation (0.0.0.0.0) occurred on -3114-08-11 # 1856305 is distance (in days) between Creation and UNIX epoch - days_since_creation = int(1856305 + self._epoch / (3600*24)) + days_since_creation = int(1856305 + self._epoch / (3600 * 24)) caps = (0, 20, 20, 18, 20) lc_date = [0, 0, 0, 0, days_since_creation] for i in range(4, 0, -1): if lc_date[i] >= caps[i]: - lc_date[i-1] += int(lc_date[i]/caps[i]) + lc_date[i - 1] += int(lc_date[i] / caps[i]) lc_date[i] %= caps[i] elif lc_date[i] < 0: - lc_date[i-1] += int(lc_date[i]/caps[i]) + lc_date[i - 1] += int(lc_date[i] / caps[i]) lc_date[i] = 0 return '.'.join(str(i) for i in lc_date) diff --git a/tests/test_maya.py b/tests/test_maya.py index e43dba1..3664445 100644 --- a/tests/test_maya.py +++ b/tests/test_maya.py @@ -217,14 +217,14 @@ def test_slang_time_locale(): ("01/05/2016", {}, "2016-01-05 00:00:00+00:00"), ("01/05/2016", dict(day_first=True), "2016-05-01 00:00:00+00:00"), ( - "2016/05/01", - dict(year_first=True, day_first=False), - "2016-05-01 00:00:00+00:00", + "2016/05/01", + dict(year_first=True, day_first=False), + "2016-05-01 00:00:00+00:00", ), ( - "2016/01/05", - dict(year_first=True, day_first=True), - "2016-05-01 00:00:00+00:00", + "2016/01/05", + dict(year_first=True, day_first=True), + "2016-05-01 00:00:00+00:00", ), ("01/05/2016", dict(timezone="UTC"), "2016-01-05 00:00:00+00:00"), ("01/05/2016", dict(timezone="US/Central"), "2016-01-05 06:00:00+00:00"), @@ -425,10 +425,10 @@ def test_snaptime(when_str, snap_str, expected_when): "when_str,snap_str,timezone,expected_when", [ ( - "Mon, 21 Feb 1994 21:21:42 GMT", - "@d", - "Australia/Perth", - "Mon, 21 Feb 1994 16:00:00 GMT", + "Mon, 21 Feb 1994 21:21:42 GMT", + "@d", + "Australia/Perth", + "Mon, 21 Feb 1994 16:00:00 GMT", ) ], )