tests for last used message

also now parsing the datetime correctly and removing its UTC tz info to make comparisons work
This commit is contained in:
Leo Hemsted
2016-06-06 11:42:40 +01:00
parent 4831e068d9
commit 9db20819ef
3 changed files with 61 additions and 13 deletions

View File

@@ -3,6 +3,7 @@ from string import ascii_uppercase
from flask import request, render_template, redirect, url_for, flash, abort
from flask_login import login_required
from dateutil.parser import parse
from notifications_utils.template import Template
from notifications_utils.recipients import first_column_heading
@@ -187,7 +188,7 @@ def delete_service_template(service_id, template_id):
template_statistics = template_statistics_client.get_template_statistics_for_service(service_id)
last_use_message = get_last_use_message(form.name.data, template['id'], template_statistics)
flash('{}. Are you sure you want to it?'.format(last_use_message), 'delete')
flash('{}. Are you sure you want to delete it?'.format(last_use_message), 'delete')
return render_template(
'views/edit-{}-template.html'.format(template['template_type']),
h1='Edit template',
@@ -225,15 +226,16 @@ def view_template_versions(service_id, template_id):
def get_last_use_message(template_name, template_id, template_statistics):
try:
most_recent_use = max(
template_stats['updated_at']
parse(template_stats['updated_at']).replace(tzinfo=None)
for template_stats in template_statistics
if template_stats['id'] == template_id
if template_stats['template']['id'] == template_id
)
except ValueError:
return '{} has never been used'
return '{} has never been used'.format(template_name)
return '{} was last used {} ago'.format(
get_human_readable_delta(most_recent_use, datetime.now())
template_name,
get_human_readable_delta(most_recent_use, datetime.utcnow())
)
@@ -243,10 +245,10 @@ def get_human_readable_delta(from_time, until_time):
return 'under a minute'
elif delta < timedelta(hours=1):
minutes = int(delta.seconds / 60)
return '{} minute{}'.format(minutes, 's' if minutes == 1 else '')
return '{} minute{}'.format(minutes, '' if minutes == 1 else 's')
elif delta < timedelta(days=1):
hours = int(delta.seconds / 3600)
return '{} hour{}'.format(hours, 's' if hours == 1 else '')
return '{} hour{}'.format(hours, '' if hours == 1 else 's')
else:
days = delta.days
return '{} day{}'.format(days, 's' if days == 1 else '')
return '{} day{}'.format(days, '' if days == 1 else 's')

View File

@@ -1,9 +1,12 @@
import json
import uuid
from bs4 import BeautifulSoup
from tests import validate_route_permission
from datetime import datetime
import pytest
from bs4 import BeautifulSoup
from flask import url_for
from freezegun import freeze_time
from tests import validate_route_permission
from app.main.views.templates import get_last_use_message, get_human_readable_delta
def test_should_show_page_for_one_templates(app_,
@@ -362,3 +365,45 @@ def test_route_invalid_permissions(mocker,
['view_activity'],
api_user_active,
service_one)
@pytest.mark.parametrize('template_statistics', [
[{'template': {'id': 'bar'}, 'updated_at': '2000-01-01T12:00:00.000000+00:00'}],
[]
])
def test_get_last_use_message_returns_no_template_message(template_statistics):
assert get_last_use_message('My Template', 'foo', template_statistics) == 'My Template has never been used'
@freeze_time('2000-01-01T15:00')
def test_get_last_use_message_uses_most_recent_statistics_for_template():
template_statistics = [
{
'template': {'id': 'foo'},
'updated_at': '2000-01-01T12:00:00.000000+00:00'
},
{
'template': {'id': 'foo'},
'updated_at': '2000-01-01T09:00:00.000000+00:00'
},
{
'template': {'id': 'bar'},
'updated_at': '2000-01-01T15:00:00.000000+00:00'
},
]
assert get_last_use_message('My Template', 'foo', template_statistics) == 'My Template was last used 3 hours ago'
@pytest.mark.parametrize('from_time, until_time, message', [
(datetime(2000, 1, 1, 12, 0), datetime(2000, 1, 1, 12, 0, 59), 'under a minute'),
(datetime(2000, 1, 1, 12, 0), datetime(2000, 1, 1, 12, 1), '1 minute'),
(datetime(2000, 1, 1, 12, 0), datetime(2000, 1, 1, 12, 2, 35), '2 minutes'),
(datetime(2000, 1, 1, 12, 0), datetime(2000, 1, 1, 12, 59), '59 minutes'),
(datetime(2000, 1, 1, 12, 0), datetime(2000, 1, 1, 13, 0), '1 hour'),
(datetime(2000, 1, 1, 12, 0), datetime(2000, 1, 1, 14, 0), '2 hours'),
(datetime(2000, 1, 1, 12, 0), datetime(2000, 1, 2, 11, 59), '23 hours'),
(datetime(2000, 1, 1, 12, 0), datetime(2000, 1, 2, 12, 0), '1 day'),
(datetime(2000, 1, 1, 12, 0), datetime(2000, 1, 3, 14, 0), '2 days'),
])
def test_get_human_readable_delta(from_time, until_time, message):
assert get_human_readable_delta(from_time, until_time) == message

View File

@@ -981,7 +981,8 @@ def mock_get_template_statistics(mocker, service_one, fake_uuid):
},
"service": template['service'],
"id": str(generate_uuid()),
"day": "2016-04-04"
"day": "2016-04-04",
"updated_at": "2016-04-04T12:00:00.000000+00:00"
}
def _get_stats(service_id, limit_days=None):