From 624acf9ffe734426d237c00044558ae4156f3eac Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Fri, 8 Dec 2017 15:01:19 +0000 Subject: [PATCH 1/2] Removed the events data for the service/{id}/history page. It does not make sense there. --- app/service/rest.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/app/service/rest.py b/app/service/rest.py index d71cd2b90..9c1763af0 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -286,12 +286,11 @@ def get_service_provider_aggregate_statistics(service_id): # tables. This is so product owner can pass stories as done @service_blueprint.route('//history', methods=['GET']) def get_service_history(service_id): - from app.models import (Service, ApiKey, TemplateHistory, Event) + from app.models import (Service, ApiKey, TemplateHistory) from app.schemas import ( service_history_schema, api_key_history_schema, - template_history_schema, - event_schema + template_history_schema ) service_history = Service.get_history_model().query.filter_by(id=service_id).all() @@ -302,14 +301,11 @@ def get_service_history(service_id): template_history = TemplateHistory.query.filter_by(service_id=service_id).all() template_data, errors = template_history_schema.dump(template_history, many=True) - events = Event.query.all() - events_data = event_schema.dump(events, many=True).data - data = { 'service_history': service_data, 'api_key_history': api_keys_data, 'template_history': template_data, - 'events': events_data} + 'events': []} return jsonify(data=data) From 4ea79d9eb148b53ed4e994bfb1db78f7830d1e42 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Tue, 12 Dec 2017 15:54:59 +0000 Subject: [PATCH 2/2] Monthly billing has been updated to populate for letters. --- app/dao/monthly_billing_dao.py | 5 +- app/dao/notification_usage_dao.py | 10 +++- tests/app/celery/test_scheduled_tasks.py | 17 +++++-- tests/app/dao/test_monthly_billing.py | 49 +++++++++++++++++--- tests/app/dao/test_notification_usage_dao.py | 6 +-- tests/app/db.py | 6 +-- 6 files changed, 73 insertions(+), 20 deletions(-) diff --git a/app/dao/monthly_billing_dao.py b/app/dao/monthly_billing_dao.py index 4528f414d..d3aebe9d6 100644 --- a/app/dao/monthly_billing_dao.py +++ b/app/dao/monthly_billing_dao.py @@ -9,8 +9,8 @@ from app.models import ( SMS_TYPE, EMAIL_TYPE, MonthlyBilling, - NotificationHistory -) + NotificationHistory, + LETTER_TYPE) from app.statsd_decorators import statsd from app.utils import convert_utc_to_bst @@ -31,6 +31,7 @@ def create_or_update_monthly_billing(service_id, billing_month): start_date, end_date = get_month_start_and_end_date_in_utc(billing_month) _update_monthly_billing(service_id, start_date, end_date, SMS_TYPE) _update_monthly_billing(service_id, start_date, end_date, EMAIL_TYPE) + _update_monthly_billing(service_id, start_date, end_date, LETTER_TYPE) def _monthly_billing_data_to_json(billing_data): diff --git a/app/dao/notification_usage_dao.py b/app/dao/notification_usage_dao.py index 7a892689b..22cdc5ada 100644 --- a/app/dao/notification_usage_dao.py +++ b/app/dao/notification_usage_dao.py @@ -50,6 +50,11 @@ def get_billing_data_for_month(service_id, start_date, end_date, notification_ty ) ) + if notification_type == LETTER_TYPE: + results.extend(billing_letter_data_per_month_query( + service_id=service_id, start_date=start_date, end_date=end_date) + ) + return results @@ -165,10 +170,11 @@ def billing_letter_data_per_month_query(service_id, start_date, end_date): NotificationHistory.notification_type, month, NotificationHistory.rate_multiplier, - NotificationHistory.international + NotificationHistory.international, + LetterRate.rate ).order_by( month, rate_multiplier() ).all() - return results \ No newline at end of file + return results diff --git a/tests/app/celery/test_scheduled_tasks.py b/tests/app/celery/test_scheduled_tasks.py index bf1e242f3..ff770d547 100644 --- a/tests/app/celery/test_scheduled_tasks.py +++ b/tests/app/celery/test_scheduled_tasks.py @@ -645,13 +645,12 @@ def test_populate_monthly_billing_populates_correctly(sample_template): monthly_billing = MonthlyBilling.query.order_by(MonthlyBilling.notification_type).all() - assert len(monthly_billing) == 2 + assert len(monthly_billing) == 3 assert monthly_billing[0].service_id == sample_template.service_id assert monthly_billing[0].start_date == jul_month_start assert monthly_billing[0].end_date == jul_month_end assert monthly_billing[0].notification_type == 'email' - assert monthly_billing[0].notification_type == 'email' assert monthly_billing[0].monthly_totals == [] assert monthly_billing[1].service_id == sample_template.service_id @@ -668,6 +667,12 @@ def test_populate_monthly_billing_populates_correctly(sample_template): } ) + assert monthly_billing[2].service_id == sample_template.service_id + assert monthly_billing[2].start_date == jul_month_start + assert monthly_billing[2].end_date == jul_month_end + assert monthly_billing[2].notification_type == 'letter' + assert monthly_billing[2].monthly_totals == [] + @freeze_time("2016-04-01 23:00:00") def test_populate_monthly_billing_updates_correct_month_in_bst(sample_template): @@ -680,7 +685,7 @@ def test_populate_monthly_billing_updates_correct_month_in_bst(sample_template): monthly_billing = MonthlyBilling.query.order_by(MonthlyBilling.notification_type).all() - assert len(monthly_billing) == 2 + assert len(monthly_billing) == 3 assert monthly_billing[0].service_id == sample_template.service_id assert monthly_billing[0].start_date == apr_month_start @@ -695,6 +700,12 @@ def test_populate_monthly_billing_updates_correct_month_in_bst(sample_template): assert monthly_billing[1].monthly_totals[0]['billing_units'] == 1 assert monthly_billing[1].monthly_totals[0]['total_cost'] == 0.0123 + assert monthly_billing[2].service_id == sample_template.service_id + assert monthly_billing[2].start_date == apr_month_start + assert monthly_billing[2].end_date == apr_month_end + assert monthly_billing[2].notification_type == 'letter' + assert monthly_billing[2].monthly_totals == [] + def test_run_letter_jobs(client, mocker, sample_letter_template): jobs = [create_job(template=sample_letter_template, job_status=JOB_STATUS_READY_TO_SEND), diff --git a/tests/app/dao/test_monthly_billing.py b/tests/app/dao/test_monthly_billing.py index e5813c9bf..fbfa737d4 100644 --- a/tests/app/dao/test_monthly_billing.py +++ b/tests/app/dao/test_monthly_billing.py @@ -11,13 +11,14 @@ from app.dao.monthly_billing_dao import ( get_billing_data_for_financial_year ) from app.models import MonthlyBilling, SMS_TYPE, EMAIL_TYPE +from tests.app.conftest import sample_letter_template from tests.app.db import ( create_notification, create_rate, create_service, create_template, - create_monthly_billing_entry -) + create_monthly_billing_entry, + create_letter_rate) FEB_2016_MONTH_START = datetime(2016, 2, 1) FEB_2016_MONTH_END = datetime(2016, 2, 29, 23, 59, 59, 99999) @@ -164,7 +165,7 @@ def test_add_monthly_billing_for_single_month_populates_correctly( monthly_billing = MonthlyBilling.query.order_by(MonthlyBilling.notification_type).all() - assert len(monthly_billing) == 2 + assert len(monthly_billing) == 3 _assert_monthly_billing( monthly_billing[0], sample_template.service.id, 'email', JAN_2017_MONTH_START, JAN_2017_MONTH_END ) @@ -181,6 +182,11 @@ def test_add_monthly_billing_for_single_month_populates_correctly( "total_cost": 1 * 2 * 0.0158 }) + _assert_monthly_billing( + monthly_billing[2], sample_template.service.id, 'letter', JAN_2017_MONTH_START, JAN_2017_MONTH_END + ) + assert monthly_billing[2].monthly_totals == [] + def test_add_monthly_billing_for_multiple_months_populate_correctly( sample_template, sample_email_template @@ -203,7 +209,7 @@ def test_add_monthly_billing_for_multiple_months_populate_correctly( MonthlyBilling.start_date ).all() - assert len(monthly_billing) == 4 + assert len(monthly_billing) == 6 _assert_monthly_billing( monthly_billing[0], sample_template.service.id, 'email', FEB_2016_MONTH_START, FEB_2016_MONTH_END ) @@ -236,10 +242,20 @@ def test_add_monthly_billing_for_multiple_months_populate_correctly( "total_cost": 0.72 }) + _assert_monthly_billing( + monthly_billing[4], sample_template.service.id, 'letter', FEB_2016_MONTH_START, FEB_2016_MONTH_END + ) + assert monthly_billing[4].monthly_totals == [] + _assert_monthly_billing( + monthly_billing[5], sample_template.service.id, 'letter', MAR_2016_MONTH_START, MAR_2016_MONTH_END + ) + assert monthly_billing[5].monthly_totals == [] + def test_add_monthly_billing_with_multiple_rates_populate_correctly( - sample_template + sample_template, sample_email_template ): + letter_template = sample_letter_template(sample_template.service) create_rate(start_date=JAN_2017_MONTH_START, value=0.0158, notification_type=SMS_TYPE) create_rate(start_date=JAN_2017_MONTH_START + timedelta(days=5), value=0.123, notification_type=SMS_TYPE) create_notification(template=sample_template, created_at=JAN_2017_MONTH_START, billable_units=1, status='delivered') @@ -248,15 +264,26 @@ def test_add_monthly_billing_with_multiple_rates_populate_correctly( billable_units=2, status='delivered' ) + create_notification(template=sample_email_template, created_at=JAN_2017_MONTH_START, status='delivered') + create_notification(template=letter_template, created_at=JAN_2017_MONTH_START, status='delivered', + billable_units=1) + create_letter_rate(start_date=JAN_2017_MONTH_START, crown=False) + create_or_update_monthly_billing(service_id=sample_template.service_id, billing_month=JAN_2017_MONTH_START) monthly_billing = MonthlyBilling.query.order_by(MonthlyBilling.notification_type).all() - assert len(monthly_billing) == 2 + assert len(monthly_billing) == 3 _assert_monthly_billing( monthly_billing[0], sample_template.service.id, 'email', JAN_2017_MONTH_START, JAN_2017_MONTH_END ) - assert monthly_billing[0].monthly_totals == [] + _assert_monthly_billing_totals(monthly_billing[0].monthly_totals[0], { + "billing_units": 1, + "rate_multiplier": 1, + "international": False, + "rate": 0.0, + "total_cost": 0.0 + }) _assert_monthly_billing( monthly_billing[1], sample_template.service.id, 'sms', JAN_2017_MONTH_START, JAN_2017_MONTH_END @@ -276,6 +303,14 @@ def test_add_monthly_billing_with_multiple_rates_populate_correctly( "total_cost": 0.246 }) + _assert_monthly_billing_totals(monthly_billing[2].monthly_totals[0], { + "billing_units": 1, + "rate_multiplier": 1, + "international": False, + "rate": 0.31, + "total_cost": 0.31 + }) + def test_update_monthly_billing_overwrites_old_totals(sample_template): create_rate(APR_2016_MONTH_START, 0.123, SMS_TYPE) diff --git a/tests/app/dao/test_notification_usage_dao.py b/tests/app/dao/test_notification_usage_dao.py index 6b7f95582..66a790365 100644 --- a/tests/app/dao/test_notification_usage_dao.py +++ b/tests/app/dao/test_notification_usage_dao.py @@ -1,5 +1,4 @@ import uuid -from _decimal import Decimal from datetime import datetime, timedelta from freezegun import freeze_time @@ -218,7 +217,7 @@ def test_get_monthly_billing_data_where_start_date_before_rate_returns_empty( def test_billing_letter_data_per_month_query( notify_db_session ): - rate = create_letter_rate() + create_letter_rate() service = create_service() template = create_template(service=service, template_type='letter') create_notification(template=template, billable_units=1, created_at=datetime(2017, 2, 1, 13, 21), @@ -233,4 +232,5 @@ def test_billing_letter_data_per_month_query( end_date=datetime(2017, 2, 28)) assert len(results) == 1 - assert results[0].rate == Decimal('0.31') + assert results[0].rate == 0.31 + assert results[0].billing_units == 3 diff --git a/tests/app/db.py b/tests/app/db.py index 1b12ec991..08a33e47a 100644 --- a/tests/app/db.py +++ b/tests/app/db.py @@ -70,7 +70,7 @@ def create_service( email_from=None, prefix_sms=True, message_limit=1000, - crown=True + organisation_type='central' ): service = Service( name=service_name, @@ -79,7 +79,7 @@ def create_service( email_from=email_from if email_from else service_name.lower().replace(' ', '.'), created_by=user or create_user(email='{}@digital.cabinet-office.gov.uk'.format(uuid.uuid4())), prefix_sms=prefix_sms, - crown=crown + organisation_type=organisation_type ) dao_create_service(service, service.created_by, service_id, service_permissions=service_permissions) @@ -459,7 +459,7 @@ def create_annual_billing( def create_letter_rate( - start_date=datetime(2017, 1,1, 00,00,00), + start_date=datetime(2017, 1, 1, 00, 00, 00), end_date=None, sheet_count=1, rate=0.31,