Filter and navigate usage by financial year

Right now we tell people that the usage page is for the current
financial year. This is a lie – it’s for all time.

So this commit calls through to the API to get the stats for (by
default) the current financial year.

We already do this for the monthly breakdown, this just does the same
thing for the yearly totals.

It also adds navigation to show the data for other financial years:
- previous so you can go back and see your usage and verify that the
  bill you’re about to pay is correct
- next so that you can check what your SMS allowance is going to be
  before you actually get into it
This commit is contained in:
Chris Hill-Scott
2017-01-25 15:59:06 +00:00
parent 8b2ea28111
commit 2a502753a4
8 changed files with 112 additions and 75 deletions

View File

@@ -14,6 +14,7 @@ from app.main.views.dashboard import (
from tests import validate_route_permission
from tests.conftest import SERVICE_ONE_ID
from tests.app.test_utils import normalize_spaces
stub_template_stats = [
{
@@ -233,33 +234,34 @@ def test_should_show_recent_jobs_on_dashboard(
assert table_rows[index].find_all('td')[column_index].text.strip() == str(count)
@freeze_time("2016-12-31 11:09:00.061258")
@freeze_time("2012-03-31 12:12:12")
def test_usage_page(
logged_in_client,
api_user_active,
mock_get_service,
mock_get_user,
mock_has_permissions,
mock_get_usage,
mock_get_billable_units,
):
response = logged_in_client.get(url_for('main.usage', service_id=SERVICE_ONE_ID, year=2000))
response = logged_in_client.get(url_for('main.usage', service_id=SERVICE_ONE_ID))
assert response.status_code == 200
mock_get_billable_units.assert_called_once_with(SERVICE_ONE_ID, 2000)
mock_get_billable_units.assert_called_once_with(SERVICE_ONE_ID, 2011)
mock_get_usage.assert_called_once_with(SERVICE_ONE_ID, 2011)
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
cols = page.find_all('div', {'class': 'column-half'})
nav = page.find('nav', {'class': 'pill'})
nav_links = nav.find_all('a')
assert cols[1].text.strip() == 'Financial year 2000 to 2001'
assert normalize_spaces(nav_links[0].text) == '2010 to 2011 financial year'
assert normalize_spaces(nav.find('span').text) == '2011 to 2012 financial year'
assert normalize_spaces(nav_links[1].text) == '2012 to 2013 financial year'
assert '123' in cols[2].text
assert 'Emails' in cols[2].text
assert '123' in cols[0].text
assert 'Emails' in cols[0].text
assert '456,123' in cols[3].text
assert 'Text messages' in cols[3].text
assert '456,123' in cols[1].text
assert 'Text messages' in cols[1].text
table = page.find('table').text.strip()
@@ -271,13 +273,18 @@ def test_usage_page(
assert '206,246 text messages at 1.65p' in table
@freeze_time("2016-12-31 11:09:00.061258")
def test_usage_page_with_year_argument(
logged_in_client,
mock_get_usage,
mock_get_billable_units
):
assert logged_in_client.get(url_for('main.usage', service_id=SERVICE_ONE_ID, year=2000)).status_code == 200
mock_get_billable_units.assert_called_once_with(SERVICE_ONE_ID, 2000)
mock_get_usage.assert_called_once_with(SERVICE_ONE_ID, 2000)
def test_usage_page_for_invalid_year(
logged_in_client,
api_user_active,
mock_get_service,
mock_get_user,
mock_has_permissions,
):
assert logged_in_client.get(url_for('main.usage', service_id=SERVICE_ONE_ID, year='abcd')).status_code == 404

View File

@@ -148,3 +148,7 @@ def test_generate_notifications_csv_formats_row_number_correctly(mocker, row_num
assert len(csv_rows) == 1
assert csv_rows[0].get('Row number') == expected_result
def normalize_spaces(string):
return ' '.join(string.split())

View File

@@ -1256,7 +1256,7 @@ def mock_get_template_statistics_for_template(mocker, service_one):
@pytest.fixture(scope='function')
def mock_get_usage(mocker, service_one, fake_uuid):
def _get_usage(service_id):
def _get_usage(service_id, year=None):
return {'data': {
"sms_count": 456123,
"email_count": 123