From 88e1e58f24d26b69c4ee00d77dbfcf78dde0b847 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Mon, 9 Jan 2017 14:54:25 +0000 Subject: [PATCH] Fix the get_april_fools method to use March 31 23:00 as the timestamp. The financial year start April 1, 00:00 BST and our dates are stored as UTC. Added a test for get_april_fools. Added some test more test data for get_billable_unit_count_per_month. --- app/dao/notifications_dao.py | 29 +++++++++++++++----------- tests/app/dao/test_notification_dao.py | 27 +++++++++++++----------- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index b5a1b6591..4295eb48b 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -216,14 +216,27 @@ def get_notification_billable_unit_count_per_month(service_id, year): start, end = get_financial_year(year) notifications = db.session.query( - func.date_trunc("month", (NotificationHistory.created_at + extract("timezone", func.timezone("Europe/London", NotificationHistory.created_at)) * timedelta(seconds=1))), + func.date_trunc("month", + (NotificationHistory.created_at + + extract("timezone", func.timezone("Europe/London", + NotificationHistory.created_at)) * timedelta(seconds=1))), func.sum(NotificationHistory.billable_units) ).filter( NotificationHistory.billable_units != 0, NotificationHistory.service_id == service_id, NotificationHistory.created_at >= start, NotificationHistory.created_at < end - ).group_by(func.date_trunc("month", (NotificationHistory.created_at + extract("timezone", func.timezone("Europe/London", NotificationHistory.created_at)) * timedelta(seconds=1)))).all() + ).group_by(func.date_trunc("month", + (NotificationHistory.created_at + + extract("timezone", + func.timezone("Europe/London", + NotificationHistory.created_at)) * timedelta(seconds=1))) + ).order_by(func.date_trunc("month", + (NotificationHistory.created_at + + extract("timezone", + func.timezone("Europe/London", + NotificationHistory.created_at)) * timedelta( + seconds=1)))).all() return [(datetime.strftime(x[0], "%B"), x[1]) for x in notifications] @@ -376,13 +389,5 @@ def get_financial_year(year): def get_april_fools(year): - return datetime( - year, 4, 1, 0, 0, 0, 0, - pytz.timezone("Europe/London") - ).astimezone(pytz.utc) - - -def get_bst_month(datetime): - return pytz.utc.localize(datetime).astimezone( - pytz.timezone("Europe/London") - ).strftime('%B') + return pytz.timezone('Europe/London').localize(datetime(year, 4, 1, 0, 0, 0)).astimezone(pytz.UTC).replace( + tzinfo=None) diff --git a/tests/app/dao/test_notification_dao.py b/tests/app/dao/test_notification_dao.py index 029435775..e686deca8 100644 --- a/tests/app/dao/test_notification_dao.py +++ b/tests/app/dao/test_notification_dao.py @@ -39,7 +39,8 @@ from app.dao.notifications_dao import ( update_notification_status_by_reference, dao_delete_notifications_and_history_by_id, dao_timeout_notifications, - get_financial_year) + get_financial_year, + get_april_fools) from tests.app.conftest import (sample_notification, sample_template, sample_email_template, sample_service, sample_job, sample_api_key) @@ -702,15 +703,15 @@ def test_get_notification_billable_unit_count_per_month(notify_db, notify_db_ses for year, month, day, hour, minute, second in ( (2017, 1, 15, 23, 59, 59), # ↓ 2016 financial year - (2016, 9, 30, 23, 59, 59), # counts in October (BST conversion) + (2016, 9, 30, 23, 59, 59), # counts in October with BST conversion (2016, 6, 30, 23, 50, 20), (2016, 7, 15, 9, 20, 25), - (2016, 4, 15, 12, 30, 00), (2016, 4, 1, 1, 1, 00), - (2016, 4, 1, 0, 0, 00), # ↓ 2015 financial year - (2016, 3, 20, 22, 40, 45), + (2016, 4, 1, 0, 0, 00), + (2016, 3, 31, 23, 00, 1), # counts in April with BST conversion + (2015, 4, 1, 13, 8, 59), # ↓ 2015 financial year (2015, 11, 20, 22, 40, 45), - (2016, 1, 15, 2, 30, 40) + (2016, 1, 31, 23, 30, 40) # counts in January no BST conversion in winter ): sample_notification( notify_db, notify_db_session, service=sample_service, @@ -726,11 +727,11 @@ def test_get_notification_billable_unit_count_per_month(notify_db, notify_db_ses ), ( 2016, - [('April', 2), ('July', 2), ('October', 1), ('January', 1)] + [('April', 3), ('July', 2), ('October', 1), ('January', 1)] ), ( 2015, - [('November', 1), ('January', 1), ('March', 1), ('April', 1)] + [('April', 1), ('November', 1), ('January', 1)] ), ( 2014, @@ -1203,7 +1204,9 @@ def test_should_exclude_test_key_notifications_by_default( def test_get_financial_year(): start, end = get_financial_year(2000) - assert start.tzinfo == pytz.utc - assert start.isoformat() == '2000-04-01T00:01:00+00:00' - assert end.tzinfo == pytz.utc - assert end.isoformat() == '2001-04-01T00:01:00+00:00' + assert str(start) == '2000-03-31 23:00:00' + assert str(end) == '2001-03-31 23:00:00' + + +def test_get_april_fools(): + assert str(get_april_fools(2016)) == '2016-03-31 23:00:00'