Show template usage broken down by month

This follows on from:
- https://github.com/alphagov/notifications-admin/pull/1094
- https://github.com/alphagov/notifications-admin/pull/1109

It depends on:
- [ ] https://github.com/alphagov/notifications-api/pull/829

A year is too long. Month-by-month is a better timeframe for making
decisions or seeing patterns in your usage.
This commit is contained in:
Chris Hill-Scott
2017-02-16 15:08:16 +00:00
parent 7fc5d41531
commit 4a226a7a29
9 changed files with 209 additions and 60 deletions

View File

@@ -1,4 +1,5 @@
from datetime import datetime
from functools import partial
import copy
from flask import url_for
@@ -120,39 +121,40 @@ def test_should_show_recent_templates_on_dashboard(
assert '100' in table_rows[1].find_all('td')[0].text
def test_should_show_all_templates_on_template_statistics_page(
@freeze_time("2016-07-01 12:00") # 4 months into 2016 financial year
@pytest.mark.parametrize('partial_url', [
partial(url_for),
partial(url_for, year='2016'),
])
def test_should_show_monthly_breakdown_of_template_usage(
logged_in_client,
mocker,
api_user_active,
mock_get_service,
mock_get_service_templates,
mock_get_monthly_template_statistics,
mock_get_user,
mock_get_user_by_email,
mock_login,
mock_get_jobs,
mock_has_permissions,
partial_url,
):
mock_template_stats = mocker.patch('app.template_statistics_client.get_template_statistics_for_service',
return_value=copy.deepcopy(stub_template_stats))
response = logged_in_client.get(url_for('main.template_history', service_id=SERVICE_ONE_ID))
response = logged_in_client.get(
partial_url('main.template_history', service_id=SERVICE_ONE_ID, _external=True)
)
assert response.status_code == 200
response.get_data(as_text=True)
mock_template_stats.assert_called_once_with(SERVICE_ONE_ID)
mock_get_monthly_template_statistics.assert_called_once_with(SERVICE_ONE_ID, 2016)
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
table_rows = page.find_all('tbody')[0].find_all('tr')
table_rows = page.select('tbody tr')
assert len(table_rows) == 2
assert ' '.join(table_rows[0].text.split()) == (
'My first template '
'Text message template '
'2'
)
assert 'two' in table_rows[0].find_all('th')[0].text
assert 'Email template' in table_rows[0].find_all('th')[0].text
assert '200' in table_rows[0].find_all('td')[0].text
assert 'one' in table_rows[1].find_all('th')[0].text
assert 'Text message template' in table_rows[1].find_all('th')[0].text
assert '100' in table_rows[1].find_all('td')[0].text
assert len(table_rows) == len(['April'])
assert len(page.select('.table-no-data')) == len(['May', 'June', 'July'])
@freeze_time("2016-01-01 11:09:00.061258")

View File

@@ -1277,6 +1277,28 @@ def mock_get_template_statistics(mocker, service_one, fake_uuid):
'app.template_statistics_client.get_template_statistics_for_service', side_effect=_get_stats)
@pytest.fixture(scope='function')
def mock_get_monthly_template_statistics(mocker, service_one, fake_uuid):
def _stats(service_id, year):
return {
datetime.utcnow().strftime('%Y-%m'): {
fake_uuid: {
"counts": {
"sending": 1,
"delivered": 1,
},
"name": 'My first template',
"type": 'sms',
"id": fake_uuid,
}
}
}
return mocker.patch(
'app.template_statistics_client.get_monthly_template_statistics_for_service',
side_effect=_stats
)
@pytest.fixture(scope='function')
def mock_get_template_statistics_for_template(mocker, service_one):
def _get_stats(service_id, template_id):