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

@@ -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):