clean up usage of dates/datetimes in performance platform tasks

* call variables unambiguous things like `start_time` or `bst_date` to
  reduce risk of passing in the wrong thing
* simplify the count_dict object - remove nested dict and start_date
  fields as superfluous
* use static datetime objects in tests rather than calculating them
  each time
This commit is contained in:
Leo Hemsted
2019-04-02 11:49:20 +01:00
parent 4abbb7137a
commit 3739d9055d
7 changed files with 64 additions and 79 deletions

View File

@@ -1,4 +1,4 @@
from datetime import datetime, timedelta
from datetime import datetime, timedelta, date
from functools import partial
from unittest.mock import call, patch, PropertyMock
@@ -251,7 +251,7 @@ def test_send_daily_performance_stats_calls_does_not_send_if_inactive(client, mo
assert send_mock.call_count == 0
@freeze_time("2016-01-11 12:30:00")
@freeze_time("2016-06-11 02:00:00")
def test_send_total_sent_notifications_to_performance_platform_calls_with_correct_totals(
notify_db_session,
sample_template,
@@ -261,18 +261,14 @@ def test_send_total_sent_notifications_to_performance_platform_calls_with_correc
perf_mock = mocker.patch(
'app.celery.nightly_tasks.total_sent_notifications.send_total_notifications_sent_for_day_stats') # noqa
today = datetime.utcnow()
create_ft_notification_status(bst_date=today, notification_type='sms', service=sample_template.service,
template=sample_template)
create_ft_notification_status(bst_date=today, notification_type='email', service=sample_email_template.service,
template=sample_email_template)
today = date(2016, 6, 11)
create_ft_notification_status(bst_date=today, template=sample_template)
create_ft_notification_status(bst_date=today, template=sample_email_template)
# Create some notifications for the day before
yesterday = today - timedelta(days=1)
create_ft_notification_status(bst_date=yesterday, notification_type='sms', service=sample_template.service,
template=sample_template, count=2)
create_ft_notification_status(bst_date=yesterday, notification_type='email', service=sample_email_template.service,
template=sample_email_template, count=3)
yesterday = date(2016, 6, 10)
create_ft_notification_status(bst_date=yesterday, template=sample_template, count=2)
create_ft_notification_status(bst_date=yesterday, template=sample_email_template, count=3)
with patch.object(
PerformancePlatformClient,
@@ -283,9 +279,9 @@ def test_send_total_sent_notifications_to_performance_platform_calls_with_correc
send_total_sent_notifications_to_performance_platform(yesterday)
perf_mock.assert_has_calls([
call(yesterday.date(), 'sms', 2),
call(yesterday.date(), 'email', 3),
call(yesterday.date(), 'letter', 0)
call(datetime(2016, 6, 9, 23, 0), 'sms', 2),
call(datetime(2016, 6, 9, 23, 0), 'email', 3),
call(datetime(2016, 6, 9, 23, 0), 'letter', 0)
])

View File

@@ -29,7 +29,7 @@ def test_send_processing_time_to_performance_platform_creates_correct_call_to_pe
send_stats = mocker.patch('app.performance_platform.total_sent_notifications.performance_platform_client.send_stats_to_performance_platform') # noqa
send_processing_time_data(
date=datetime(2016, 10, 15, 23, 0, 0),
start_time=datetime(2016, 10, 15, 23, 0, 0),
status='foo',
count=142
)

View File

@@ -1,4 +1,6 @@
from datetime import datetime, timedelta
from datetime import datetime, date
from freezegun import freeze_time
from app.performance_platform.total_sent_notifications import (
send_total_notifications_sent_for_day_stats,
@@ -12,7 +14,7 @@ def test_send_total_notifications_sent_for_day_stats_stats_creates_correct_call(
send_stats = mocker.patch('app.performance_platform.total_sent_notifications.performance_platform_client.send_stats_to_performance_platform') # noqa
send_total_notifications_sent_for_day_stats(
date=datetime(2016, 10, 15, 23, 0, 0),
start_time=datetime(2016, 10, 15, 23, 0, 0),
notification_type='sms',
count=142
)
@@ -30,34 +32,29 @@ def test_send_total_notifications_sent_for_day_stats_stats_creates_correct_call(
assert request_args['_id'] == expected_base64_id
@freeze_time('2018-06-10 01:00')
def test_get_total_sent_notifications_yesterday_returns_expected_totals_dict(sample_service):
sms = create_template(sample_service, template_type='sms')
email = create_template(sample_service, template_type='email')
letter = create_template(sample_service, template_type='letter')
today = datetime.utcnow().date()
yesterday = today - timedelta(days=1)
create_ft_notification_status(bst_date=today, notification_type='sms',
service=sms.service, template=sms)
create_ft_notification_status(bst_date=today, notification_type='email',
service=email.service, template=email)
create_ft_notification_status(bst_date=today, notification_type='letter',
service=letter.service, template=letter)
today = date(2018, 6, 10)
yesterday = date(2018, 6, 9)
create_ft_notification_status(bst_date=yesterday, notification_type='sms',
service=sms.service, template=sms, count=2)
create_ft_notification_status(bst_date=yesterday, notification_type='email',
service=email.service, template=email, count=3)
create_ft_notification_status(bst_date=yesterday, notification_type='letter',
service=letter.service, template=letter, count=1)
# todays is excluded
create_ft_notification_status(bst_date=today, template=sms)
create_ft_notification_status(bst_date=today, template=email)
create_ft_notification_status(bst_date=today, template=letter)
# yesterdays is included
create_ft_notification_status(bst_date=yesterday, template=sms, count=2)
create_ft_notification_status(bst_date=yesterday, template=email, count=3)
create_ft_notification_status(bst_date=yesterday, template=letter, count=1)
total_count_dict = get_total_sent_notifications_for_day(yesterday)
assert total_count_dict["email"] == {"count": 3}
assert total_count_dict["sms"] == {"count": 2}
assert total_count_dict["letter"] == {"count": 1}
# Should return a time around midnight depending on timezones
expected_start = datetime.combine(yesterday, datetime.min.time())
time_diff = abs(expected_start - total_count_dict["start_date"])
assert time_diff <= timedelta(minutes=60)
assert total_count_dict == {
"email": 3,
"sms": 2,
"letter": 1
}