use a flash error message on the platform admin usage report

that makes more sense than attaching the error to a field

also fix tests and assert csv contents are correct
This commit is contained in:
Leo Hemsted
2019-08-27 16:53:08 +01:00
parent b69321bf7a
commit 6412a4ab33
2 changed files with 31 additions and 22 deletions

View File

@@ -305,10 +305,13 @@ def usage_for_all_services():
"sms_cost", "sms_fragments", "letter_cost", "letter_breakdown"] "sms_cost", "sms_fragments", "letter_cost", "letter_breakdown"]
result = billing_api_client.get_usage_for_all_services(start_date, end_date) result = billing_api_client.get_usage_for_all_services(start_date, end_date)
rows = [] rows = [
for r in result: [
rows.append([r['organisation_id'], r["organisation_name"], r["service_id"], r["service_name"], r['organisation_id'], r["organisation_name"], r["service_id"], r["service_name"],
r["sms_cost"], r['sms_fragments'], r["letter_cost"], r["letter_breakdown"].strip()]) r["sms_cost"], r['sms_fragments'], r["letter_cost"], r["letter_breakdown"].strip()
]
for r in result
]
if rows: if rows:
return Spreadsheet.from_rows([headers] + rows).as_csv_data, 200, { return Spreadsheet.from_rows([headers] + rows).as_csv_data, 200, {
'Content-Type': 'text/csv; charset=utf-8', 'Content-Type': 'text/csv; charset=utf-8',
@@ -317,7 +320,7 @@ def usage_for_all_services():
) )
} }
else: else:
form.errors['start_date'] = 'no results for date' flash('No results for dates')
return render_template('views/platform-admin/usage_for_all_services.html', form=form) return render_template('views/platform-admin/usage_for_all_services.html', form=form)

View File

@@ -19,11 +19,7 @@ from app.main.views.platform_admin import (
sum_service_usage, sum_service_usage,
) )
from tests import service_json from tests import service_json
from tests.conftest import ( from tests.conftest import SERVICE_ONE_ID, SERVICE_TWO_ID, normalize_spaces
SERVICE_ONE_ID,
SERVICE_TWO_ID,
normalize_spaces,
)
@pytest.mark.parametrize('endpoint', [ @pytest.mark.parametrize('endpoint', [
@@ -1018,9 +1014,8 @@ def test_usage_for_all_services_when_no_results_for_date(client_request, platfor
_expected_status=200, _expected_status=200,
_data={'start_date': '2019-01-01', 'end_date': '2019-03-31'}) _data={'start_date': '2019-01-01', 'end_date': '2019-03-31'})
errors = page.select('.error-message') error = page.select_one('.banner-dangerous')
for error in errors: assert normalize_spaces(error.text) == 'No results for dates'
assert normalize_spaces(error.text) == 'No results for date'
def test_usage_for_all_services_when_calls_api_and_download_data(platform_admin_client, mocker): def test_usage_for_all_services_when_calls_api_and_download_data(platform_admin_client, mocker):
@@ -1042,15 +1037,26 @@ def test_usage_for_all_services_when_calls_api_and_download_data(platform_admin_
assert response.headers['Content-Disposition'] == ( assert response.headers['Content-Disposition'] == (
'attachment; filename="Usage for all services from {} to {}.csv"'.format('2019-01-01', '2019-03-31') 'attachment; filename="Usage for all services from {} to {}.csv"'.format('2019-01-01', '2019-03-31')
) )
# I don't know how to get this assert to work
# assert response.get_data(as_text=True) == ( assert response.get_data(as_text=True) == (
# """organisation_id,organisation_name,service_id,service_name,sms_cost,sms_fragments, 'organisation_id,organisation_name,service_id,service_name,' +
# letter_cost,letter_breakdown 'sms_cost,sms_fragments,letter_cost,letter_breakdown' +
# 7832a1be-a1f0-4f2a-982f-05adfd3d6354,Org for a - with sms and letter,48e82ac0-c8c4-4e46-8712-c83c35a94006,
# a - with sms and letter,0,0,3.4,"6 second class letters at 45p\n '\r\n' +
# 2 first class letters at 35p"
# """ '7832a1be-a1f0-4f2a-982f-05adfd3d6354,' +
# ) 'Org for a - with sms and letter,' +
'48e82ac0-c8c4-4e46-8712-c83c35a94006,' +
'a - with sms and letter,' +
'0,' +
'0,' +
'3.4,' +
'"6 second class letters at 45p' +
'\n' +
'2 first class letters at 35p"' +
'\r\n'
)
def test_get_notifications_sent_by_service_calls_api_and_downloads_data( def test_get_notifications_sent_by_service_calls_api_and_downloads_data(