From 0fb9c1d3180116a953aaab61ba05495446a7c968 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Fri, 27 Apr 2018 15:15:55 +0100 Subject: [PATCH 1/7] Add notification_type to query --- app/billing/rest.py | 29 ++++++++ app/dao/fact_billing_dao.py | 12 +++- tests/app/dao/test_ft_billing_dao.py | 101 +++++++++++++++++++++------ 3 files changed, 119 insertions(+), 23 deletions(-) diff --git a/app/billing/rest.py b/app/billing/rest.py index cd7bc3864..82474014f 100644 --- a/app/billing/rest.py +++ b/app/billing/rest.py @@ -3,6 +3,7 @@ import json from flask import Blueprint, jsonify, request +from app.dao.fact_billing_dao import fetch_monthly_billing_for_year from app.dao.monthly_billing_dao import ( get_billing_data_for_financial_year, get_monthly_billing_by_notification_type @@ -30,6 +31,16 @@ billing_blueprint = Blueprint( register_errors(billing_blueprint) +@billing_blueprint.route('/ft-monthly-usage') +def get_yearly_usage_by_monthy_from_ft_billing(service_id): + try: + year = int(request.args.get('year')) + results = fetch_monthly_billing_for_year(service_id=service_id, year=year) + serialize_ft_billing(results) + except TypeError: + return jsonify(result='error', message='No valid year provided'), 400 + + @billing_blueprint.route('/monthly-usage') def get_yearly_usage_by_month(service_id): try: @@ -192,3 +203,21 @@ def update_free_sms_fragment_limit_data(service_id, free_sms_fragment_limit, fin free_sms_fragment_limit, financial_year_start ) + + +def serialize_ft_billing(data): + results = [] + + for d in data: + j = { + "Month": d.month, + "service_id": d.service_id, + "notifications_type": d.notification_type, + "notifications_sent": d.notifications_sent, + "billable_units": d.billable_units, + "rate": d.rate, + "rate_multiplier": d.rate_multiplier, + "international": d.international, + } + results.append(j) + return results diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index bc6f7d357..538d23ef2 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -20,7 +20,7 @@ from app.models import ( from app.utils import convert_utc_to_bst, convert_bst_to_utc -def fetch_montly_billing_for_year(service_id, year): +def fetch_monthly_billing_for_year(service_id, year): year_start_date, year_end_date = get_financial_year(year) utcnow = datetime.utcnow() today = convert_utc_to_bst(utcnow).date() @@ -39,7 +39,8 @@ def fetch_montly_billing_for_year(service_id, year): FactBilling.service_id, FactBilling.rate, FactBilling.rate_multiplier, - FactBilling.international + FactBilling.international, + FactBilling.notification_type ).filter( FactBilling.service_id == service_id, FactBilling.bst_date >= year_start_date, @@ -49,7 +50,12 @@ def fetch_montly_billing_for_year(service_id, year): FactBilling.service_id, FactBilling.rate, FactBilling.rate_multiplier, - FactBilling.international + FactBilling.international, + FactBilling.notification_type + ).order_by( + FactBilling.service_id, + 'Month', + FactBilling.notification_type ).all() return yearly_data diff --git a/tests/app/dao/test_ft_billing_dao.py b/tests/app/dao/test_ft_billing_dao.py index 4ed43ffc5..8c0d60f94 100644 --- a/tests/app/dao/test_ft_billing_dao.py +++ b/tests/app/dao/test_ft_billing_dao.py @@ -5,7 +5,7 @@ from freezegun import freeze_time from app import db from app.dao.fact_billing_dao import ( - fetch_montly_billing_for_year, fetch_billing_data_for_day, get_rates_for_billing, + fetch_monthly_billing_for_year, fetch_billing_data_for_day, get_rates_for_billing, get_rate ) from app.models import FactBilling @@ -132,6 +132,25 @@ def test_fetch_billing_data_for_day_is_grouped_by_international(notify_db_sessio assert results[1].notifications_sent == 1 +def test_fetch_billing_data_for_day_is_grouped_by_notification_type(notify_db_session): + service = create_service() + sms_template = create_template(service=service, template_type='sms') + email_template = create_template(service=service, template_type='email') + letter_template = create_template(service=service, template_type='letter') + create_notification(template=sms_template, status='delivered') + create_notification(template=sms_template, status='delivered') + create_notification(template=sms_template, status='delivered') + create_notification(template=email_template, status='delivered') + create_notification(template=email_template, status='delivered') + create_notification(template=letter_template, status='delivered') + + today = convert_utc_to_bst(datetime.utcnow()) + results = fetch_billing_data_for_day(today) + assert len(results) == 3 + notification_types = [x[2] for x in results if x[2] in ['email', 'sms', 'letter']] + assert len(notification_types) == 3 + + def test_fetch_billing_data_for_day_returns_empty_list(notify_db_session): today = convert_utc_to_bst(datetime.utcnow()) results = fetch_billing_data_for_day(today) @@ -181,7 +200,7 @@ def test_get_rate(notify_db_session): assert letter_rate == Decimal('4.4') -def test_fetch_annual_billing_for_year(notify_db_session): +def test_fetch_monthly_billing_for_year(notify_db_session): service = create_service() template = create_template(service=service, template_type="sms") for i in range(1, 31): @@ -197,28 +216,30 @@ def test_fetch_annual_billing_for_year(notify_db_session): notification_type='sms', rate=0.158) - results = fetch_montly_billing_for_year(service_id=service.id, year=2018) + results = fetch_monthly_billing_for_year(service_id=service.id, year=2018) assert len(results) == 2 - assert results[0][0] == 6.0 - assert results[0][1] == 30 - assert results[0][2] == Decimal('30') - assert results[0][3] == service.id - assert results[0][4] == Decimal('0.162') - assert results[0][5] == Decimal('1') - assert results[0][6] is False + assert results[0].Month == 6.0 + assert results[0].notifications_sent == 30 + assert results[0].billable_units == Decimal('30') + assert results[0].service_id == service.id + assert results[0].rate == Decimal('0.162') + assert results[0].rate_multiplier == Decimal('1') + assert results[0].international is False + assert results[0].notification_type == 'sms' - assert results[1][0] == 7.0 - assert results[1][1] == 31 - assert results[1][2] == Decimal('31') - assert results[1][3] == service.id - assert results[1][4] == Decimal('0.158') - assert results[1][5] == Decimal('1') - assert results[1][6] is False + assert results[1].Month == 7.0 + assert results[1].notifications_sent == 31 + assert results[1].billable_units == Decimal('31') + assert results[1].service_id == service.id + assert results[1].rate == Decimal('0.158') + assert results[1].rate_multiplier == Decimal('1') + assert results[1].international is False + assert results[1].notification_type == 'sms' @freeze_time('2018-08-01 13:30:00') -def test_fetch_annual_billing_for_year_adds_data_for_today(notify_db_session): +def test_fetch_monthly_billing_for_year_adds_data_for_today(notify_db_session): service = create_service() template = create_template(service=service, template_type="email") for i in range(1, 32): @@ -230,7 +251,47 @@ def test_fetch_annual_billing_for_year_adds_data_for_today(notify_db_session): create_notification(template=template, status='delivered') assert db.session.query(FactBilling.bst_date).count() == 31 - results = fetch_montly_billing_for_year(service_id=service.id, - year=2018) + results = fetch_monthly_billing_for_year(service_id=service.id, + year=2018) assert db.session.query(FactBilling.bst_date).count() == 32 assert len(results) == 2 + + +def test_fetch_monthly_billing_for_year(notify_db_session): + service = create_service() + sms_template = create_template(service=service, template_type="email") + email_template = create_template(service=service, template_type="email") + letter_template = create_template(service=service, template_type="email") + for month in range(1, 13): + mon = str(month).zfill(2) + days_in_month = {1: 32, 2: 30, 3: 32, 4: 31, 5: 32, 6: 31, 7: 32, 8: 32, 9: 31, 10: 32, 11: 31, 12: 32} + for day in range(1, days_in_month[month]): + d = str(day).zfill(2) + create_ft_billing(bst_date='2016-{}-{}'.format(mon, d), + service=service, + template=sms_template, + notification_type='sms', + rate=0.162) + create_ft_billing(bst_date='2016-{}-{}'.format(mon, d), + service=service, + template=email_template, + notification_type='email', + rate=0) + create_ft_billing(bst_date='2016-{}-{}'.format(mon, d), + service=service, + template=letter_template, + notification_type='letter', + rate=0.33) + + results = fetch_monthly_billing_for_year(service.id, 2016) + # returns 3 rows, per month, returns financial year april to december + # Orders by Month + assert len(results) == 27 + assert results[0][0] == 4 + assert results[1][0] == 4 + assert results[2][0] == 4 + assert results[3][0] == 5 + assert results[26][0] == 12 + + + From 18c2b9a56df8b26addc3d8f11a4082a4dafd792e Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Mon, 30 Apr 2018 16:34:40 +0100 Subject: [PATCH 2/7] Use better date function to get the first of each month. Build the json object to return for the new endpoint. --- app/billing/rest.py | 21 +++++++------ app/dao/fact_billing_dao.py | 8 ++--- tests/app/billing/test_billing.py | 46 +++++++++++++++++++++++++++- tests/app/dao/test_ft_billing_dao.py | 27 ++++++++-------- 4 files changed, 74 insertions(+), 28 deletions(-) diff --git a/app/billing/rest.py b/app/billing/rest.py index 82474014f..6034e1ad4 100644 --- a/app/billing/rest.py +++ b/app/billing/rest.py @@ -32,14 +32,16 @@ register_errors(billing_blueprint) @billing_blueprint.route('/ft-monthly-usage') -def get_yearly_usage_by_monthy_from_ft_billing(service_id): +def get_yearly_usage_by_monthly_from_ft_billing(service_id): try: year = int(request.args.get('year')) - results = fetch_monthly_billing_for_year(service_id=service_id, year=year) - serialize_ft_billing(results) except TypeError: return jsonify(result='error', message='No valid year provided'), 400 + results = fetch_monthly_billing_for_year(service_id=service_id, year=year) + data = serialize_ft_billing(results) + return jsonify(monthly_usage=data) + @billing_blueprint.route('/monthly-usage') def get_yearly_usage_by_month(service_id): @@ -207,16 +209,15 @@ def update_free_sms_fragment_limit_data(service_id, free_sms_fragment_limit, fin def serialize_ft_billing(data): results = [] - for d in data: j = { - "Month": d.month, - "service_id": d.service_id, + "month": (datetime.strftime(d.month, "%B")), + "service_id": str(d.service_id), "notifications_type": d.notification_type, - "notifications_sent": d.notifications_sent, - "billable_units": d.billable_units, - "rate": d.rate, - "rate_multiplier": d.rate_multiplier, + "notifications_sent": int(d.notifications_sent), + "billable_units": int(d.billable_units), + "rate": float(d.rate), + "rate_multiplier": int(d.rate_multiplier), "international": d.international, } results.append(j) diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index 538d23ef2..7cc1919c0 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -1,7 +1,7 @@ from datetime import datetime, timedelta, time from flask import current_app -from sqlalchemy import func, case, desc, extract +from sqlalchemy import func, case, desc from app import db from app.dao.date_util import get_financial_year @@ -33,7 +33,7 @@ def fetch_monthly_billing_for_year(service_id, year): update_fact_billing(data=d, process_day=day) yearly_data = db.session.query( - extract('month', FactBilling.bst_date).label("Month"), + func.date_trunc('month', FactBilling.bst_date).label("month"), func.sum(FactBilling.notifications_sent).label("notifications_sent"), func.sum(FactBilling.billable_units).label("billable_units"), FactBilling.service_id, @@ -46,7 +46,7 @@ def fetch_monthly_billing_for_year(service_id, year): FactBilling.bst_date >= year_start_date, FactBilling.bst_date <= year_end_date ).group_by( - 'Month', + 'month', FactBilling.service_id, FactBilling.rate, FactBilling.rate_multiplier, @@ -54,7 +54,7 @@ def fetch_monthly_billing_for_year(service_id, year): FactBilling.notification_type ).order_by( FactBilling.service_id, - 'Month', + 'month', FactBilling.notification_type ).all() diff --git a/tests/app/billing/test_billing.py b/tests/app/billing/test_billing.py index 8250eaf40..c0cae0a1a 100644 --- a/tests/app/billing/test_billing.py +++ b/tests/app/billing/test_billing.py @@ -17,7 +17,9 @@ from tests.app.db import ( create_monthly_billing_entry, create_annual_billing, create_letter_rate, - create_template + create_template, + create_service, + create_ft_billing ) from app.billing.rest import update_free_sms_fragment_limit_data @@ -409,3 +411,45 @@ def test_update_free_sms_fragment_limit_data(client, sample_service): annual_billing = dao_get_free_sms_fragment_limit_for_year(sample_service.id, current_year) assert annual_billing.free_sms_fragment_limit == 9999 + + +def test_get_yearly_usage_by_monthly_from_ft_billing(client, notify_db_session): + service = create_service() + sms_template = create_template(service=service, template_type="sms") + email_template = create_template(service=service, template_type="email") + letter_template = create_template(service=service, template_type="letter") + for month in range(1, 13): + mon = str(month).zfill(2) + days_in_month = {1: 32, 2: 30, 3: 32, 4: 31, 5: 32, 6: 31, 7: 32, 8: 32, 9: 31, 10: 32, 11: 31, 12: 32} + for day in range(1, days_in_month[month]): + d = str(day).zfill(2) + create_ft_billing(bst_date='2016-{}-{}'.format(mon, d), + service=service, + template=sms_template, + notification_type='sms', + rate=0.162) + create_ft_billing(bst_date='2016-{}-{}'.format(mon, d), + service=service, + template=email_template, + notification_type='email', + rate=0) + create_ft_billing(bst_date='2016-{}-{}'.format(mon, d), + service=service, + template=letter_template, + notification_type='letter', + rate=0.33) + + response = client.get('service/{}/billing/ft-monthly-usage?year=2016'.format(service.id), + headers=[('Content-Type', 'application/json'), create_authorization_header()]) + + json_resp = json.loads(response.get_data(as_text=True)) + + assert json_resp["monthly_usage"][0] == {"month": "April", + "service_id": str(service.id), + "notifications_type": 'email', + "notifications_sent": 30, + "billable_units": 30, + "rate": 0.0, + "rate_multiplier": 1, + "international": False, + } diff --git a/tests/app/dao/test_ft_billing_dao.py b/tests/app/dao/test_ft_billing_dao.py index 8c0d60f94..5230e72ec 100644 --- a/tests/app/dao/test_ft_billing_dao.py +++ b/tests/app/dao/test_ft_billing_dao.py @@ -219,7 +219,7 @@ def test_fetch_monthly_billing_for_year(notify_db_session): results = fetch_monthly_billing_for_year(service_id=service.id, year=2018) assert len(results) == 2 - assert results[0].Month == 6.0 + assert str(results[0].month) == "2018-06-01 00:00:00+01:00" assert results[0].notifications_sent == 30 assert results[0].billable_units == Decimal('30') assert results[0].service_id == service.id @@ -228,7 +228,7 @@ def test_fetch_monthly_billing_for_year(notify_db_session): assert results[0].international is False assert results[0].notification_type == 'sms' - assert results[1].Month == 7.0 + assert str(results[1].month) == "2018-07-01 00:00:00+01:00" assert results[1].notifications_sent == 31 assert results[1].billable_units == Decimal('31') assert results[1].service_id == service.id @@ -257,11 +257,11 @@ def test_fetch_monthly_billing_for_year_adds_data_for_today(notify_db_session): assert len(results) == 2 -def test_fetch_monthly_billing_for_year(notify_db_session): +def test_fetch_monthly_billing_for_year_return_financial_year(notify_db_session): service = create_service() - sms_template = create_template(service=service, template_type="email") + sms_template = create_template(service=service, template_type="sms") email_template = create_template(service=service, template_type="email") - letter_template = create_template(service=service, template_type="email") + letter_template = create_template(service=service, template_type="letter") for month in range(1, 13): mon = str(month).zfill(2) days_in_month = {1: 32, 2: 30, 3: 32, 4: 31, 5: 32, 6: 31, 7: 32, 8: 32, 9: 31, 10: 32, 11: 31, 12: 32} @@ -286,12 +286,13 @@ def test_fetch_monthly_billing_for_year(notify_db_session): results = fetch_monthly_billing_for_year(service.id, 2016) # returns 3 rows, per month, returns financial year april to december # Orders by Month + assert len(results) == 27 - assert results[0][0] == 4 - assert results[1][0] == 4 - assert results[2][0] == 4 - assert results[3][0] == 5 - assert results[26][0] == 12 - - - + assert str(results[0].month) == "2016-04-01 00:00:00+01:00" + assert results[0].notification_type == 'email' + assert str(results[1].month) == "2016-04-01 00:00:00+01:00" + assert results[1].notification_type == 'letter' + assert str(results[2].month) == "2016-04-01 00:00:00+01:00" + assert results[2].notification_type == 'sms' + assert str(results[3].month) == "2016-05-01 00:00:00+01:00" + assert str(results[26].month) == "2016-12-01 00:00:00+00:00" From ea3523199a6b61493230004274b74ecaa27f74c5 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Fri, 4 May 2018 13:09:14 +0100 Subject: [PATCH 3/7] New endpoint to get monthly billing usage from the ft_billing table. New command to compare the results of monthly billing to ft_billing. --- app/billing/rest.py | 22 ++-- app/commands.py | 40 ++++++- app/dao/fact_billing_dao.py | 12 +-- app/dao/monthly_billing_dao.py | 3 +- tests/app/billing/test_billing.py | 150 ++++++++++++++++++++++----- tests/app/dao/test_ft_billing_dao.py | 7 +- 6 files changed, 182 insertions(+), 52 deletions(-) diff --git a/app/billing/rest.py b/app/billing/rest.py index 6034e1ad4..cb6eb4cc6 100644 --- a/app/billing/rest.py +++ b/app/billing/rest.py @@ -37,10 +37,9 @@ def get_yearly_usage_by_monthly_from_ft_billing(service_id): year = int(request.args.get('year')) except TypeError: return jsonify(result='error', message='No valid year provided'), 400 - results = fetch_monthly_billing_for_year(service_id=service_id, year=year) data = serialize_ft_billing(results) - return jsonify(monthly_usage=data) + return jsonify(data) @billing_blueprint.route('/monthly-usage') @@ -49,13 +48,13 @@ def get_yearly_usage_by_month(service_id): year = int(request.args.get('year')) results = [] for month in get_months_for_financial_year(year): - billing_for_month = get_monthly_billing_by_notification_type(service_id, month, SMS_TYPE) - if billing_for_month: - results.append(_transform_billing_for_month_sms(billing_for_month)) letter_billing_for_month = get_monthly_billing_by_notification_type(service_id, month, LETTER_TYPE) if letter_billing_for_month: results.extend(_transform_billing_for_month_letters(letter_billing_for_month)) - return json.dumps(results) + billing_for_month = get_monthly_billing_by_notification_type(service_id, month, SMS_TYPE) + if billing_for_month: + results.append(_transform_billing_for_month_sms(billing_for_month)) + return jsonify(results) except TypeError: return jsonify(result='error', message='No valid year provided'), 400 @@ -209,16 +208,13 @@ def update_free_sms_fragment_limit_data(service_id, free_sms_fragment_limit, fin def serialize_ft_billing(data): results = [] - for d in data: + no_emails = [x for x in data if x.notification_type != 'email'] + for d in no_emails: j = { "month": (datetime.strftime(d.month, "%B")), - "service_id": str(d.service_id), - "notifications_type": d.notification_type, - "notifications_sent": int(d.notifications_sent), - "billable_units": int(d.billable_units), + "notification_type": d.notification_type, + "billing_units": int(d.billable_units), "rate": float(d.rate), - "rate_multiplier": int(d.rate_multiplier), - "international": d.international, } results.append(j) return results diff --git a/app/commands.py b/app/commands.py index d68df2cd1..1a804679a 100644 --- a/app/commands.py +++ b/app/commands.py @@ -7,16 +7,18 @@ from decimal import Decimal import click import flask from click_datetime import Datetime as click_dt -from flask import current_app +from flask import current_app, json from sqlalchemy.orm.exc import NoResultFound from sqlalchemy import func from notifications_utils.statsd_decorators import statsd from app import db, DATETIME_FORMAT, encryption, redis_store +from app.billing.rest import get_yearly_usage_by_month, get_yearly_usage_by_monthly_from_ft_billing from app.celery.scheduled_tasks import send_total_sent_notifications_to_performance_platform from app.celery.service_callback_tasks import send_delivery_status_to_service from app.celery.letters_pdf_tasks import create_letters_pdf from app.config import QueueNames +from app.dao.date_util import get_financial_year from app.dao.fact_billing_dao import fetch_billing_data_for_day, update_fact_billing from app.dao.monthly_billing_dao import ( create_or_update_monthly_billing, @@ -562,3 +564,39 @@ def rebuild_ft_billing_for_month_and_service(service_id, day): transit_data = fetch_billing_data_for_day(process_day=day, service_id=service_id) for data in transit_data: update_fact_billing(data, day) + + +@notify_command(name='compare-ft-billing-to-monthly-billing') +@click.option('-y', '--year', required=True) +@click.option('-s', '--service_id', required=False, type=click.UUID) +def compare_ft_billing_to_monthly_billing(year, service_id=None): + """ + This command checks the results of monthly_billing to ft_billing for the given year. + If service id is not included all services are compared for the given year. + """ + + def compare_monthly_billing_to_ft_billing(ft_billing_response, monthly_billing_response): + # Remove the rows with 0 billing_units and rate, ft_billing doesn't populate those rows. + mo_json = json.loads(monthly_billing_response.get_data(as_text=True)) + rm_zero_rows = [x for x in mo_json if x['billing_units'] != 0 and x['rate'] != 0] + assert rm_zero_rows == json.loads(ft_billing_response.get_data(as_text=True)) + + if not service_id: + start_date, end_date = get_financial_year(year=int(year)) + services = get_service_ids_that_need_billing_populated(start_date, end_date) + for service_id in services: + with current_app.test_request_context( + path='/service/{}/billing/monthly-usage?year={}'.format(service_id, year)): + monthly_billing_response = get_yearly_usage_by_month(service_id) + with current_app.test_request_context( + path='/service/{}/billing/ft-monthly-usage?year={}'.format(service_id, year)): + ft_billing_response = get_yearly_usage_by_monthly_from_ft_billing(service_id) + compare_monthly_billing_to_ft_billing(ft_billing_response, monthly_billing_response) + else: + with current_app.test_request_context( + path='/service/{}/billing/monthly-usage?year={}'.format(service_id, year)): + monthly_billing_response = get_yearly_usage_by_month(service_id) + with current_app.test_request_context( + path='/service/{}/billing/ft-monthly-usage?year={}'.format(service_id, year)): + ft_billing_response = get_yearly_usage_by_monthly_from_ft_billing(service_id) + compare_ft_billing_to_monthly_billing(ft_billing_response, monthly_billing_response) diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index 7cc1919c0..5fc8a7904 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -23,9 +23,9 @@ from app.utils import convert_utc_to_bst, convert_bst_to_utc def fetch_monthly_billing_for_year(service_id, year): year_start_date, year_end_date = get_financial_year(year) utcnow = datetime.utcnow() - today = convert_utc_to_bst(utcnow).date() + today = convert_utc_to_bst(utcnow) # if year end date is less than today, we are calculating for data in the past and have no need for deltas. - if year_end_date.date() >= today: + if year_end_date >= today: yesterday = today - timedelta(days=1) for day in [yesterday, today]: data = fetch_billing_data_for_day(process_day=day, service_id=service_id) @@ -35,11 +35,9 @@ def fetch_monthly_billing_for_year(service_id, year): yearly_data = db.session.query( func.date_trunc('month', FactBilling.bst_date).label("month"), func.sum(FactBilling.notifications_sent).label("notifications_sent"), - func.sum(FactBilling.billable_units).label("billable_units"), + func.sum(FactBilling.billable_units * FactBilling.rate_multiplier).label("billable_units"), FactBilling.service_id, FactBilling.rate, - FactBilling.rate_multiplier, - FactBilling.international, FactBilling.notification_type ).filter( FactBilling.service_id == service_id, @@ -49,8 +47,6 @@ def fetch_monthly_billing_for_year(service_id, year): 'month', FactBilling.service_id, FactBilling.rate, - FactBilling.rate_multiplier, - FactBilling.international, FactBilling.notification_type ).order_by( FactBilling.service_id, @@ -125,7 +121,7 @@ def update_fact_billing(data, process_day): inserted_records = 0 updated_records = 0 non_letter_rates, letter_rates = get_rates_for_billing() - + print("process_day: {} {}".format(type(process_day), process_day)) update_count = FactBilling.query.filter( FactBilling.bst_date == datetime.date(process_day), FactBilling.template_id == data.template_id, diff --git a/app/dao/monthly_billing_dao.py b/app/dao/monthly_billing_dao.py index dafbbb104..715cd164b 100644 --- a/app/dao/monthly_billing_dao.py +++ b/app/dao/monthly_billing_dao.py @@ -100,7 +100,8 @@ def get_yearly_billing_data_for_date_range( MonthlyBilling.end_date <= end_date, MonthlyBilling.notification_type.in_(notification_types) ).order_by( - MonthlyBilling.notification_type + MonthlyBilling.start_date, + MonthlyBilling.notification_type, ).all() return results diff --git a/tests/app/billing/test_billing.py b/tests/app/billing/test_billing.py index c0cae0a1a..94de414c8 100644 --- a/tests/app/billing/test_billing.py +++ b/tests/app/billing/test_billing.py @@ -8,8 +8,8 @@ from app.dao.monthly_billing_dao import ( create_or_update_monthly_billing, get_monthly_billing_by_notification_type, ) -from app.models import SMS_TYPE, EMAIL_TYPE, LETTER_TYPE -from app.dao.date_util import get_current_financial_year_start_year +from app.models import SMS_TYPE, EMAIL_TYPE, LETTER_TYPE, FactBilling +from app.dao.date_util import get_current_financial_year_start_year, get_month_start_and_end_date_in_utc from app.dao.annual_billing_dao import dao_get_free_sms_fragment_limit_for_year from tests.app.db import ( create_notification, @@ -143,30 +143,29 @@ def test_get_yearly_usage_by_month_returns_correctly(client, sample_template): resp_json = json.loads(response.get_data(as_text=True)) _assert_dict_equals(resp_json[0], { + 'billing_units': 0, + 'month': 'May', + 'notification_type': LETTER_TYPE, + 'rate': 0 + }) + _assert_dict_equals(resp_json[1], { 'billing_units': 2, 'month': 'May', 'notification_type': SMS_TYPE, 'rate': 0.12 }) - _assert_dict_equals(resp_json[1], { + _assert_dict_equals(resp_json[2], { 'billing_units': 0, - 'month': 'May', + 'month': 'June', 'notification_type': LETTER_TYPE, 'rate': 0 }) - - _assert_dict_equals(resp_json[2], { + _assert_dict_equals(resp_json[3], { 'billing_units': 6, 'month': 'June', 'notification_type': SMS_TYPE, 'rate': 0.12 }) - _assert_dict_equals(resp_json[3], { - 'billing_units': 0, - 'month': 'June', - 'notification_type': LETTER_TYPE, - 'rate': 0 - }) def test_transform_billing_for_month_returns_empty_if_no_monthly_totals(sample_service): @@ -413,6 +412,25 @@ def test_update_free_sms_fragment_limit_data(client, sample_service): assert annual_billing.free_sms_fragment_limit == 9999 +def test_get_yearly_usage_by_monthly_from_ft_billing_populates_deltas(client, notify_db_session): + service = create_service() + sms_template = create_template(service=service, template_type="sms") + create_rate(start_date=datetime.utcnow() - timedelta(days=1), value=0.158, notification_type='sms') + + create_notification(template=sms_template, status='delivered') + + assert FactBilling.query.count() == 0 + + response = client.get('service/{}/billing/ft-monthly-usage?year=2018'.format(service.id), + headers=[('Content-Type', 'application/json'), create_authorization_header()]) + + assert response.status_code == 200 + assert len(json.loads(response.get_data(as_text=True))) == 1 + fact_billing = FactBilling.query.all() + assert len(fact_billing) == 1 + assert fact_billing[0].notification_type == 'sms' + + def test_get_yearly_usage_by_monthly_from_ft_billing(client, notify_db_session): service = create_service() sms_template = create_template(service=service, template_type="sms") @@ -427,29 +445,113 @@ def test_get_yearly_usage_by_monthly_from_ft_billing(client, notify_db_session): service=service, template=sms_template, notification_type='sms', + billable_unit=1, rate=0.162) create_ft_billing(bst_date='2016-{}-{}'.format(mon, d), service=service, template=email_template, notification_type='email', rate=0) + create_ft_billing(bst_date='2016-{}-{}'.format(mon, d), + service=service, + template=letter_template, + notification_type='letter', + billable_unit=1, + rate=0.33) + + response = client.get('service/{}/billing/ft-monthly-usage?year=2016'.format(service.id), + headers=[('Content-Type', 'application/json'), create_authorization_header()]) + + json_resp = json.loads(response.get_data(as_text=True)) + ft_letters = [x for x in json_resp if x['notification_type'] == 'letter'] + ft_sms = [x for x in json_resp if x['notification_type'] == 'sms'] + ft_email = [x for x in json_resp if x['notification_type'] == 'email'] + keys = [x.keys() for x in ft_sms][0] + expected_sms_april = {"month": "April", + "notification_type": "sms", + "billing_units": 30, + "rate": 0.162 + } + expected_letter_april = {"month": "April", + "notification_type": "letter", + "billing_units": 30, + "rate": 0.33 + } + + for k in keys: + assert ft_sms[0][k] == expected_sms_april[k] + assert ft_letters[0][k] == expected_letter_april[k] + assert len(ft_email) == 0 + + +def test_compare_ft_billing_to_monthly_billing(client, notify_db_session): + service = create_service() + sms_template = create_template(service=service, template_type="sms") + email_template = create_template(service=service, template_type="email") + letter_template = create_template(service=service, template_type="letter") + for month in range(4, 9): + mon = str(month).zfill(2) + days_in_month = {1: 32, 2: 30, 3: 32, 4: 31, 5: 32, 6: 31, 7: 32, 8: 32, 9: 31, 10: 32, 11: 31, 12: 32} + for day in range(1, days_in_month[month]): + d = str(day).zfill(2) + create_ft_billing(bst_date='2016-{}-{}'.format(mon, d), + service=service, + template=sms_template, + notification_type='sms', + rate=0.0162) + create_ft_billing(bst_date='2016-{}-{}'.format(mon, d), + service=service, + template=sms_template, + notification_type='sms', + rate_multiplier=2, + rate=0.0162) + create_ft_billing(bst_date='2016-{}-{}'.format(mon, d), + service=service, + template=email_template, + notification_type='email', + rate=0) create_ft_billing(bst_date='2016-{}-{}'.format(mon, d), service=service, template=letter_template, notification_type='letter', rate=0.33) - response = client.get('service/{}/billing/ft-monthly-usage?year=2016'.format(service.id), - headers=[('Content-Type', 'application/json'), create_authorization_header()]) + start_date, end_date = get_month_start_and_end_date_in_utc(datetime(2016, int(mon), 1)) + create_monthly_billing_entry(service=service, start_date=start_date, + end_date=end_date, + notification_type='sms', + monthly_totals=[ + {"rate": 0.0162, "international": False, + "rate_multiplier": 1, "billing_units": int(d), + "total_cost": 0.0162 * int(d)}, + {"rate": 0.0162, "international": False, + "rate_multiplier": 2, "billing_units": int(d), + "total_cost": 0.0162 * int(d)}] + ) + create_monthly_billing_entry(service=service, start_date=start_date, + end_date=end_date, + notification_type='email', + monthly_totals=[ + {"rate": 0, "international": False, + "rate_multiplier": 1, "billing_units": int(d), + "total_cost": 0}] + ) + create_monthly_billing_entry(service=service, start_date=start_date, + end_date=end_date, + notification_type='letter', + monthly_totals=[ + {"rate": 0.33, "international": False, + "rate_multiplier": 1, "billing_units": int(d), + "total_cost": 0.33 * int(d)}] + ) - json_resp = json.loads(response.get_data(as_text=True)) + monthly_billing_response = client.get('/service/{}/billing/monthly-usage?year=2016'.format(service.id), + headers=[create_authorization_header()]) - assert json_resp["monthly_usage"][0] == {"month": "April", - "service_id": str(service.id), - "notifications_type": 'email', - "notifications_sent": 30, - "billable_units": 30, - "rate": 0.0, - "rate_multiplier": 1, - "international": False, - } + ft_billing_response = client.get('service/{}/billing/ft-monthly-usage?year=2016'.format(service.id), + headers=[('Content-Type', 'application/json'), create_authorization_header()]) + + monthly_billing_json_resp = json.loads(monthly_billing_response.get_data(as_text=True)) + ft_billing_json_resp = json.loads(ft_billing_response.get_data(as_text=True)) + + assert monthly_billing_json_resp == ft_billing_json_resp diff --git a/tests/app/dao/test_ft_billing_dao.py b/tests/app/dao/test_ft_billing_dao.py index 5230e72ec..eac9a13ac 100644 --- a/tests/app/dao/test_ft_billing_dao.py +++ b/tests/app/dao/test_ft_billing_dao.py @@ -208,6 +208,7 @@ def test_fetch_monthly_billing_for_year(notify_db_session): service=service, template=template, notification_type='sms', + rate_multiplier=2, rate=0.162) for i in range(1, 32): create_ft_billing(bst_date='2018-07-{}'.format(i), @@ -221,11 +222,9 @@ def test_fetch_monthly_billing_for_year(notify_db_session): assert len(results) == 2 assert str(results[0].month) == "2018-06-01 00:00:00+01:00" assert results[0].notifications_sent == 30 - assert results[0].billable_units == Decimal('30') + assert results[0].billable_units == Decimal('60') assert results[0].service_id == service.id assert results[0].rate == Decimal('0.162') - assert results[0].rate_multiplier == Decimal('1') - assert results[0].international is False assert results[0].notification_type == 'sms' assert str(results[1].month) == "2018-07-01 00:00:00+01:00" @@ -233,8 +232,6 @@ def test_fetch_monthly_billing_for_year(notify_db_session): assert results[1].billable_units == Decimal('31') assert results[1].service_id == service.id assert results[1].rate == Decimal('0.158') - assert results[1].rate_multiplier == Decimal('1') - assert results[1].international is False assert results[1].notification_type == 'sms' From fd6e5f39cfbb76bc6c8da1be2171ed2e10c26766 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Tue, 8 May 2018 12:09:29 +0100 Subject: [PATCH 4/7] Changes as per requested from code review Move the serialize method to the billing_schema Update variable names Improve tests Fix bug in command --- app/billing/billing_schemas.py | 16 ++++++++ app/billing/rest.py | 41 ++++++++------------ app/commands.py | 2 +- tests/app/billing/test_billing.py | 10 ++--- tests/app/dao/test_ft_billing_dao.py | 57 +++++++++++++++++----------- 5 files changed, 71 insertions(+), 55 deletions(-) diff --git a/app/billing/billing_schemas.py b/app/billing/billing_schemas.py index 00d92f1c3..572228439 100644 --- a/app/billing/billing_schemas.py +++ b/app/billing/billing_schemas.py @@ -1,3 +1,5 @@ +from datetime import datetime + create_or_update_free_sms_fragment_limit_schema = { "$schema": "http://json-schema.org/draft-04/schema#", "description": "POST annual billing schema", @@ -8,3 +10,17 @@ create_or_update_free_sms_fragment_limit_schema = { }, "required": ["free_sms_fragment_limit"] } + + +def serialize_ft_billing_remove_emails(data): + results = [] + billed_notifications = [x for x in data if x.notification_type != 'email'] + for notifications in billed_notifications: + json_result = { + "month": (datetime.strftime(notifications.month, "%B")), + "notification_type": notifications.notification_type, + "billing_units": int(notifications.billable_units), + "rate": float(notifications.rate), + } + results.append(json_result) + return results diff --git a/app/billing/rest.py b/app/billing/rest.py index cb6eb4cc6..77203c80c 100644 --- a/app/billing/rest.py +++ b/app/billing/rest.py @@ -1,25 +1,30 @@ -from datetime import datetime import json +from datetime import datetime from flask import Blueprint, jsonify, request +from app.billing.billing_schemas import ( + create_or_update_free_sms_fragment_limit_schema, + serialize_ft_billing_remove_emails +) +from app.dao.annual_billing_dao import ( + dao_get_free_sms_fragment_limit_for_year, + dao_get_all_free_sms_fragment_limit, + dao_create_or_update_annual_billing_for_year, + dao_update_annual_billing_for_future_years +) +from app.dao.date_util import get_current_financial_year_start_year +from app.dao.date_util import get_months_for_financial_year from app.dao.fact_billing_dao import fetch_monthly_billing_for_year from app.dao.monthly_billing_dao import ( get_billing_data_for_financial_year, get_monthly_billing_by_notification_type ) -from app.dao.date_util import get_months_for_financial_year +from app.errors import InvalidRequest from app.errors import register_errors from app.models import SMS_TYPE, EMAIL_TYPE, LETTER_TYPE -from app.utils import convert_utc_to_bst -from app.dao.annual_billing_dao import (dao_get_free_sms_fragment_limit_for_year, - dao_get_all_free_sms_fragment_limit, - dao_create_or_update_annual_billing_for_year, - dao_update_annual_billing_for_future_years) -from app.billing.billing_schemas import create_or_update_free_sms_fragment_limit_schema -from app.errors import InvalidRequest from app.schema_validation import validate -from app.dao.date_util import get_current_financial_year_start_year +from app.utils import convert_utc_to_bst billing_blueprint = Blueprint( 'billing', @@ -38,7 +43,7 @@ def get_yearly_usage_by_monthly_from_ft_billing(service_id): except TypeError: return jsonify(result='error', message='No valid year provided'), 400 results = fetch_monthly_billing_for_year(service_id=service_id, year=year) - data = serialize_ft_billing(results) + data = serialize_ft_billing_remove_emails(results) return jsonify(data) @@ -204,17 +209,3 @@ def update_free_sms_fragment_limit_data(service_id, free_sms_fragment_limit, fin free_sms_fragment_limit, financial_year_start ) - - -def serialize_ft_billing(data): - results = [] - no_emails = [x for x in data if x.notification_type != 'email'] - for d in no_emails: - j = { - "month": (datetime.strftime(d.month, "%B")), - "notification_type": d.notification_type, - "billing_units": int(d.billable_units), - "rate": float(d.rate), - } - results.append(j) - return results diff --git a/app/commands.py b/app/commands.py index 1a804679a..45cb2cabd 100644 --- a/app/commands.py +++ b/app/commands.py @@ -599,4 +599,4 @@ def compare_ft_billing_to_monthly_billing(year, service_id=None): with current_app.test_request_context( path='/service/{}/billing/ft-monthly-usage?year={}'.format(service_id, year)): ft_billing_response = get_yearly_usage_by_monthly_from_ft_billing(service_id) - compare_ft_billing_to_monthly_billing(ft_billing_response, monthly_billing_response) + compare_monthly_billing_to_ft_billing(ft_billing_response, monthly_billing_response) diff --git a/tests/app/billing/test_billing.py b/tests/app/billing/test_billing.py index 94de414c8..06a7c7a06 100644 --- a/tests/app/billing/test_billing.py +++ b/tests/app/billing/test_billing.py @@ -1,3 +1,4 @@ +from calendar import monthrange from datetime import datetime, timedelta import json @@ -438,8 +439,7 @@ def test_get_yearly_usage_by_monthly_from_ft_billing(client, notify_db_session): letter_template = create_template(service=service, template_type="letter") for month in range(1, 13): mon = str(month).zfill(2) - days_in_month = {1: 32, 2: 30, 3: 32, 4: 31, 5: 32, 6: 31, 7: 32, 8: 32, 9: 31, 10: 32, 11: 31, 12: 32} - for day in range(1, days_in_month[month]): + for day in range(1, monthrange(2016, month)[1] + 1): d = str(day).zfill(2) create_ft_billing(bst_date='2016-{}-{}'.format(mon, d), service=service, @@ -489,10 +489,9 @@ def test_compare_ft_billing_to_monthly_billing(client, notify_db_session): sms_template = create_template(service=service, template_type="sms") email_template = create_template(service=service, template_type="email") letter_template = create_template(service=service, template_type="letter") - for month in range(4, 9): + for month in range(1, 13): mon = str(month).zfill(2) - days_in_month = {1: 32, 2: 30, 3: 32, 4: 31, 5: 32, 6: 31, 7: 32, 8: 32, 9: 31, 10: 32, 11: 31, 12: 32} - for day in range(1, days_in_month[month]): + for day in range(1, monthrange(2016, month)[1] + 1): d = str(day).zfill(2) create_ft_billing(bst_date='2016-{}-{}'.format(mon, d), service=service, @@ -515,7 +514,6 @@ def test_compare_ft_billing_to_monthly_billing(client, notify_db_session): template=letter_template, notification_type='letter', rate=0.33) - start_date, end_date = get_month_start_and_end_date_in_utc(datetime(2016, int(mon), 1)) create_monthly_billing_entry(service=service, start_date=start_date, end_date=end_date, diff --git a/tests/app/dao/test_ft_billing_dao.py b/tests/app/dao/test_ft_billing_dao.py index eac9a13ac..45ad4cc0a 100644 --- a/tests/app/dao/test_ft_billing_dao.py +++ b/tests/app/dao/test_ft_billing_dao.py @@ -1,3 +1,4 @@ +from calendar import monthrange from decimal import Decimal from datetime import datetime, timedelta @@ -259,37 +260,47 @@ def test_fetch_monthly_billing_for_year_return_financial_year(notify_db_session) sms_template = create_template(service=service, template_type="sms") email_template = create_template(service=service, template_type="email") letter_template = create_template(service=service, template_type="letter") - for month in range(1, 13): - mon = str(month).zfill(2) - days_in_month = {1: 32, 2: 30, 3: 32, 4: 31, 5: 32, 6: 31, 7: 32, 8: 32, 9: 31, 10: 32, 11: 31, 12: 32} - for day in range(1, days_in_month[month]): - d = str(day).zfill(2) - create_ft_billing(bst_date='2016-{}-{}'.format(mon, d), - service=service, - template=sms_template, - notification_type='sms', - rate=0.162) - create_ft_billing(bst_date='2016-{}-{}'.format(mon, d), - service=service, - template=email_template, - notification_type='email', - rate=0) - create_ft_billing(bst_date='2016-{}-{}'.format(mon, d), - service=service, - template=letter_template, - notification_type='letter', - rate=0.33) + + for year in (2016, 2017): + for month in range(1, 13): + mon = str(month).zfill(2) + for day in range(1, monthrange(year, month)[1] + 1): + d = str(day).zfill(2) + create_ft_billing(bst_date='{}-{}-{}'.format(year, mon, d), + service=service, + template=sms_template, + notification_type='sms', + rate=0.162) + create_ft_billing(bst_date='{}-{}-{}'.format(year, mon, d), + service=service, + template=email_template, + notification_type='email', + rate=0) + create_ft_billing(bst_date='{}-{}-{}'.format(year, mon, d), + service=service, + template=letter_template, + notification_type='letter', + rate=0.33) results = fetch_monthly_billing_for_year(service.id, 2016) - # returns 3 rows, per month, returns financial year april to december + # returns 3 rows, per month, returns financial year april to end of march # Orders by Month - assert len(results) == 27 + assert len(results) == 36 assert str(results[0].month) == "2016-04-01 00:00:00+01:00" assert results[0].notification_type == 'email' + assert results[0].notifications_sent == 30 + assert results[0].billable_units == 30 + assert results[0].rate == Decimal('0') assert str(results[1].month) == "2016-04-01 00:00:00+01:00" assert results[1].notification_type == 'letter' + assert results[1].notifications_sent == 30 + assert results[1].billable_units == 30 + assert results[1].rate == Decimal('0.33') assert str(results[2].month) == "2016-04-01 00:00:00+01:00" assert results[2].notification_type == 'sms' + assert results[2].notifications_sent == 30 + assert results[2].billable_units == 30 + assert results[2].rate == Decimal('0.162') assert str(results[3].month) == "2016-05-01 00:00:00+01:00" - assert str(results[26].month) == "2016-12-01 00:00:00+00:00" + assert str(results[35].month) == "2017-03-01 00:00:00+00:00" From dd113a8e86e4724811197dda573ceffce36cf3bc Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Tue, 8 May 2018 13:44:06 +0100 Subject: [PATCH 5/7] Update the tests so that they ignore timezone in the month returned by query. The timezone added is that of the database - locally a db will get timezone=GB, but the servers are using UTC (which is right) This date is trucated to month, and is BST, here time and timezone is irrelevant. --- tests/app/dao/test_ft_billing_dao.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/app/dao/test_ft_billing_dao.py b/tests/app/dao/test_ft_billing_dao.py index 45ad4cc0a..2f7eef368 100644 --- a/tests/app/dao/test_ft_billing_dao.py +++ b/tests/app/dao/test_ft_billing_dao.py @@ -221,14 +221,14 @@ def test_fetch_monthly_billing_for_year(notify_db_session): results = fetch_monthly_billing_for_year(service_id=service.id, year=2018) assert len(results) == 2 - assert str(results[0].month) == "2018-06-01 00:00:00+01:00" + assert str(results[0].month.date()) == "2018-06-01" assert results[0].notifications_sent == 30 assert results[0].billable_units == Decimal('60') assert results[0].service_id == service.id assert results[0].rate == Decimal('0.162') assert results[0].notification_type == 'sms' - assert str(results[1].month) == "2018-07-01 00:00:00+01:00" + assert str(results[1].month.date()) == "2018-07-01" assert results[1].notifications_sent == 31 assert results[1].billable_units == Decimal('31') assert results[1].service_id == service.id @@ -287,20 +287,20 @@ def test_fetch_monthly_billing_for_year_return_financial_year(notify_db_session) # Orders by Month assert len(results) == 36 - assert str(results[0].month) == "2016-04-01 00:00:00+01:00" + assert str(results[0].month.date()) == "2016-04-01" assert results[0].notification_type == 'email' assert results[0].notifications_sent == 30 assert results[0].billable_units == 30 assert results[0].rate == Decimal('0') - assert str(results[1].month) == "2016-04-01 00:00:00+01:00" + assert str(results[1].month.date()) == "2016-04-01" assert results[1].notification_type == 'letter' assert results[1].notifications_sent == 30 assert results[1].billable_units == 30 assert results[1].rate == Decimal('0.33') - assert str(results[2].month) == "2016-04-01 00:00:00+01:00" + assert str(results[2].month.date()) == "2016-04-01" assert results[2].notification_type == 'sms' assert results[2].notifications_sent == 30 assert results[2].billable_units == 30 assert results[2].rate == Decimal('0.162') - assert str(results[3].month) == "2016-05-01 00:00:00+01:00" - assert str(results[35].month) == "2017-03-01 00:00:00+00:00" + assert str(results[3].month.date()) == "2016-05-01" + assert str(results[35].month.date()) == "2017-03-01" From 3e3b885bdc82eb084828e575b04514c1637b17b1 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Tue, 8 May 2018 13:53:44 +0100 Subject: [PATCH 6/7] Realised that it's best to cast the Month as date. --- app/dao/fact_billing_dao.py | 4 ++-- tests/app/dao/test_ft_billing_dao.py | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index 5fc8a7904..f69df16f3 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -1,7 +1,7 @@ from datetime import datetime, timedelta, time from flask import current_app -from sqlalchemy import func, case, desc +from sqlalchemy import func, case, desc, Date from app import db from app.dao.date_util import get_financial_year @@ -33,7 +33,7 @@ def fetch_monthly_billing_for_year(service_id, year): update_fact_billing(data=d, process_day=day) yearly_data = db.session.query( - func.date_trunc('month', FactBilling.bst_date).label("month"), + func.date_trunc('month', FactBilling.bst_date).cast(Date).label("month"), func.sum(FactBilling.notifications_sent).label("notifications_sent"), func.sum(FactBilling.billable_units * FactBilling.rate_multiplier).label("billable_units"), FactBilling.service_id, diff --git a/tests/app/dao/test_ft_billing_dao.py b/tests/app/dao/test_ft_billing_dao.py index 2f7eef368..216caa447 100644 --- a/tests/app/dao/test_ft_billing_dao.py +++ b/tests/app/dao/test_ft_billing_dao.py @@ -221,14 +221,14 @@ def test_fetch_monthly_billing_for_year(notify_db_session): results = fetch_monthly_billing_for_year(service_id=service.id, year=2018) assert len(results) == 2 - assert str(results[0].month.date()) == "2018-06-01" + assert str(results[0].month) == "2018-06-01" assert results[0].notifications_sent == 30 assert results[0].billable_units == Decimal('60') assert results[0].service_id == service.id assert results[0].rate == Decimal('0.162') assert results[0].notification_type == 'sms' - assert str(results[1].month.date()) == "2018-07-01" + assert str(results[1].month) == "2018-07-01" assert results[1].notifications_sent == 31 assert results[1].billable_units == Decimal('31') assert results[1].service_id == service.id @@ -287,20 +287,20 @@ def test_fetch_monthly_billing_for_year_return_financial_year(notify_db_session) # Orders by Month assert len(results) == 36 - assert str(results[0].month.date()) == "2016-04-01" + assert str(results[0].month) == "2016-04-01" assert results[0].notification_type == 'email' assert results[0].notifications_sent == 30 assert results[0].billable_units == 30 assert results[0].rate == Decimal('0') - assert str(results[1].month.date()) == "2016-04-01" + assert str(results[1].month) == "2016-04-01" assert results[1].notification_type == 'letter' assert results[1].notifications_sent == 30 assert results[1].billable_units == 30 assert results[1].rate == Decimal('0.33') - assert str(results[2].month.date()) == "2016-04-01" + assert str(results[2].month) == "2016-04-01" assert results[2].notification_type == 'sms' assert results[2].notifications_sent == 30 assert results[2].billable_units == 30 assert results[2].rate == Decimal('0.162') - assert str(results[3].month.date()) == "2016-05-01" - assert str(results[35].month.date()) == "2017-03-01" + assert str(results[3].month) == "2016-05-01" + assert str(results[35].month) == "2017-03-01" From 844f5b2e5d35c2e6273c765e9934eb8c3bbe923e Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Tue, 8 May 2018 16:32:28 +0100 Subject: [PATCH 7/7] Added logging message if the comparison failed. --- app/commands.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/commands.py b/app/commands.py index 45cb2cabd..6c4d1f659 100644 --- a/app/commands.py +++ b/app/commands.py @@ -579,7 +579,10 @@ def compare_ft_billing_to_monthly_billing(year, service_id=None): # Remove the rows with 0 billing_units and rate, ft_billing doesn't populate those rows. mo_json = json.loads(monthly_billing_response.get_data(as_text=True)) rm_zero_rows = [x for x in mo_json if x['billing_units'] != 0 and x['rate'] != 0] - assert rm_zero_rows == json.loads(ft_billing_response.get_data(as_text=True)) + try: + assert rm_zero_rows == json.loads(ft_billing_response.get_data(as_text=True)) + except AssertionError: + print("Comparison failed for service: {} and year: {}".format(service_id, year)) if not service_id: start_date, end_date = get_financial_year(year=int(year))